Comp2.uvue 2.0 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1
<template>
2
  <view class="mt-10" ref="mixin-comp-root">
D
DCloud_LXH 已提交
3 4
    <text class="bold">Comp2: inheritAttrs: true</text>
    <text class="mt-10" style="color:#ccc;">rootElementTitle should not be null</text >
5
    <text class="mt-10 root-element-title-2"
DCloud-WZF's avatar
DCloud-WZF 已提交
6 7
      >rootElementTitle: {{ rootElementTitle }}</text
    >
8 9 10 11 12 13
    <text class="mt-10">{{ namesakeMixinProp }}</text>
    <text class="mt-10">{{ namesakeMixinDataMsg }}</text>
    <text class="mt-10">{{ namesakeMixinComputed }}</text>
    <text class="mt-10 mixin-watch-msg">{{ mixinWatchMsg }}</text>
    <text class="mt-10">{{ namesakeMixinMethod() }}</text>
    <button class="mt-10" @click="changeGlobalMixinOnloadMsg1">
DCloud-WZF's avatar
DCloud-WZF 已提交
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
      change globalMixinOnloadMsg1
    </button>
    <MixinComp />
  </view>
</template>

<script lang="uts">
import CompForComp from './CompForComp.uvue'

export default {
  mixins:[{
    inheritAttrs: true,
  }],
  components: {MixinComp: CompForComp},
  props: {
    namesakeMixinProp: {
      type: String,
      default: '组件内部的同名 props'
    }
  },
  data(){
    return {
      namesakeMixinDataMsg: '组件内部的同名 data',
      mixinWatchMsg: '',
      rootElementTitle: '' as string | null
    }
  },
  computed: {
    namesakeMixinComputed(): string {
      const res = '组件内部的同名 computed'
      console.log(res)
      return res
    }
  },
  mounted(){
雪洛's avatar
雪洛 已提交
49
    const rootElement = this.$refs['mixin-comp-root'] as UniElement
D
DCloud_LXH 已提交
50
    this.rootElementTitle = JSON.stringify(rootElement.getAttribute('title'))
DCloud-WZF's avatar
DCloud-WZF 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  },
  watch: {
    globalMixinOnloadMsg1(newVal: string) {
      this.mixinWatchMsg = `组件内部定义的 watch, 更新后的 globalMixinOnloadMsg1: ${newVal}`
      console.log(this.mixinWatchMsg)
    },
  },
  methods: {
    namesakeMixinMethod(): string {
      const res = '组件内部的同名 method'
      console.log(res)
      return res
    },
    changeGlobalMixinOnloadMsg1(){
      this.globalMixinOnloadMsg1 = 'new globalMixinOnloadMsg1 changed in comp2'
    }
  }
}
</script>