Mixin.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. export default {
  2. props: {
  3. dataVal: {
  4. type: Object | String,
  5. default: null
  6. },
  7. attrs: {
  8. type: Object | String,
  9. default: null
  10. },
  11. styles: {
  12. type: Object | String,
  13. default: null
  14. },
  15. },
  16. data() {
  17. return {
  18. dataMessage: 'network request',
  19. dataLists: [],
  20. };
  21. },
  22. methods: {
  23. setInit(data) {
  24. if (data instanceof Array) {
  25. this.dataLists = data.map(el => {
  26. const obj = {}
  27. for (let key in el) {
  28. obj[key] = el[key].value
  29. if (key === 'image' && !obj.image) {
  30. obj.image = '/'
  31. }
  32. }
  33. return obj
  34. })
  35. return
  36. }
  37. if (data instanceof Object) {
  38. const {
  39. dataList
  40. } = data;
  41. if (dataList) {
  42. this.setInit(dataList)
  43. return false
  44. }
  45. }
  46. this.dataLists = data
  47. }
  48. },
  49. computed: {
  50. // mOptions() {
  51. // return {
  52. // ...this.defaultOptions,
  53. // ...this.options
  54. // }
  55. // }
  56. },
  57. watch: {
  58. dataVal: {
  59. handler(newValue, oldValue) {
  60. if (newValue) {
  61. if (newValue.dataMsg) {
  62. const {
  63. dataMsg
  64. } = newValue;
  65. if (dataMsg.dataList === this.dataMessage) {
  66. const oldDataList = oldValue ? oldValue.dataMsg.dataList : null
  67. try {
  68. dataMsg.dataList !== oldDataList ? this.onNetworkRequest() : ''
  69. } catch (e) {
  70. //TODO handle the exception
  71. console.error('当前组件onNetworkRequest方法错误:' + e.message)
  72. }
  73. } else {
  74. try {
  75. this.setInit(dataMsg)
  76. } catch (e) {
  77. //TODO handle the exception
  78. }
  79. }
  80. } else {
  81. console.log('newValue = ', newValue)
  82. try {
  83. this.setInit(newValue.dataList)
  84. } catch (e) {
  85. //TODO handle the exception
  86. }
  87. }
  88. }
  89. },
  90. deep: true,
  91. immediate: true
  92. },
  93. },
  94. }