mixins.uts 3.8 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
import MixinComp1 from './components/MixinComp1.uvue'
import ChildMixinComp1 from './components/ChildMixinComp1.uvue'
import MixinCompForMixin from './components/MixinCompForMixin.uvue'
import MixinCompForChildMixin from './components/MixinCompForChildMixin.uvue'

export const pageMixin = defineMixin({
  mixins: [childMixin],
  components: { MixinComp1, MixinComp: MixinCompForMixin },
  props: {
    mixinProp1: {
      type: String,
      default: '通过 defineMixin 定义非全局 mixin props'
    },
    namesakeMixinProp: {
      type: String,
      default: '通过 defineMixin 定义非全局同名 mixin props'
    }
  },
  data() {
    return {
      mixinDataMsg1: '通过 defineMixin 定义非全局 mixin data',
      namesakeMixinDataMsg: '通过 defineMixin 定义非全局同名 mixin data',
      mixinOnloadMsg1: '',
      mixinOnloadTime1: 0,
      mixinWatchMsg1: ''
    }
  },
  computed: {
    mixinComputed1(): string {
      const res = `通过 defineMixin 定义非全局 mixin computed, 更新后的 mixinOnloadMsg1: ${this.mixinOnloadMsg1}`
      console.log(res)
      return res
    },
    namesakeMixinComputed(): string {
      const res = '通过 defineMixin 定义非全局同名 mixin computed'
      console.log(res)
      return res
    }
  },
  watch: {
    globalMixinOnloadMsg1(newVal: string) {
      this.mixinWatchMsg1 = `通过 defineMixin 定义非全局 mixin watch, 更新后的 globalMixinOnloadMsg1: ${newVal}`
      console.log(this.mixinWatchMsg1)
    },
  },
  onLoad() {
    this.mixinOnloadTime1 = Date.now()
    const res = '通过 defineMixin 定义非全局 mixin onLoad'
    console.log(res)
    this.mixinOnloadMsg1 = res
  },
  methods: {
    mixinMethod1(): string {
      const res = '通过 defineMixin 定义非全局 mixin method'
      console.log(res)
      return res
    },
    namesakeMixinMethod(): string {
      const res = '通过 defineMixin 定义非全局同名 mixin method'
      console.log(res)
      return res
    }
  }
})

const childMixin = defineMixin({
  components: { ChildMixinComp1, MixinComp: MixinCompForChildMixin },
  props: {
    childMixinProp1: {
      type: String,
      default: '通过 defineMixin 定义非全局 child mixin props'
    },
    namesakeChildMixinProp: {
      type: String,
      default: '通过 defineMixin 定义非全局同名 child mixin props'
    }
  },
  data() {
    return {
      childMixinDataMsg1: '通过 defineMixin 定义非全局 child mixin data',
      namesakeChildMixinDataMsg: '通过 defineMixin 定义非全局同名 mixin data',
      childMixinOnloadMsg1: '',
      childMixinOnloadTime1: 0,
      childMixinWatchMsg1: ''
    }
  },
  computed: {
    childMixinComputed1(): string {
      const res = `通过 defineMixin 定义非全局 child mixin computed, 更新后的 childMixinOnloadMsg1: ${this.childMixinOnloadMsg1}`
      console.log(res)
      return res
    },
    namesakeChildMixinComputed(): string {
      const res = '通过 defineMixin 定义非全局同名 child computed'
      console.log(res)
      return res
    }
  },
  watch: {
    globalMixinOnloadMsg1(newVal: string) {
      this.childMixinWatchMsg1 = `通过 defineMixin 定义非全局 child mixin watch, 更新后的 globalMixinOnloadMsg1: ${newVal}`
      console.log(this.childMixinWatchMsg1)
    },
  },
  onLoad() {
    this.childMixinOnloadTime1 = Date.now()
    const res = '通过 defineMixin 定义非全局 child mixin onLoad'
    console.log(res)
    this.childMixinOnloadMsg1 = res
  },
  methods: {
    childMixinMethod1(): string {
      const res = '通过 defineMixin 定义非全局 child mixin method'
      console.log(res)
      return res
    },
    namesakeChildMixinMethod(): string {
      const res = '通过 defineMixin 定义非全局同名 child mixin method'
      console.log(res)
      return res
    },
  }
})