mixins-web.uvue 2.6 KB
Newer Older
1 2 3
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
D
DCloud_LXH 已提交
4
    <!-- #endif -->
5
    <view class="page">
6 7 8 9
      <text id="mixin-prop" class="mb-10">mixinProp: {{mixinProp}}</text>
      <text id="mixin-data-msg" class="mb-10">mixinDataMsg: {{mixinDataMsg}}</text>
      <text id="mixin-onload-msg" class="mb-10">mixinOnloadMsg: {{mixinOnloadMsg}}</text>
      <text id="mixin-computed" class="mb-10">mixinComputed: {{mixinComputed}}</text>
D
DCloud_LXH 已提交
10 11 12 13 14 15 16 17 18 19 20

      <Comp1 title="title" @globalMixinEmit1="(arg: string) => handleMixinEmitter('globalMixinEmit1', arg)"
        @globalChildMixinEmit1="(arg: string) => handleMixinEmitter('globalChildMixinEmit1', arg)"
        @globalMixinEmit2="(arg: string) => handleMixinEmitter('globalMixinEmit2', arg)"
        @globalChildMixinEmit2="(arg: string) => handleMixinEmitter('globalChildMixinEmit2', arg)"
        @mixinEmit="(arg: string) => handleMixinEmitter('mixinEmit', arg)"
        @childMixinEmit="(arg: string) => handleMixinEmitter('childMixinEmit', arg)" />
      <text v-if="handleMixinEmitterMsg" class="mt-10 handle-mixin-emitter-msg">
        handleMixinEmitterMsg: {{ handleMixinEmitterMsg }}
      </text>
      <Comp2 class='comp2' title="title" />
21
    </view>
D
DCloud_LXH 已提交
22
    <!-- #ifdef APP -->
23 24 25 26 27
  </scroll-view>
  <!-- #endif -->
</template>

<script lang="uts">
D
DCloud_LXH 已提交
28 29
  import Comp1 from './components/Comp1.uvue'
  import Comp2 from './components/Comp2.uvue'
30
  export default {
D
DCloud_LXH 已提交
31 32 33 34
    components: {
      Comp1,
      Comp2,
    },
35 36 37 38 39 40 41 42 43
    mixins: [defineMixin({
      props: {
        mixinProp: {
          type: String,
          default: '通过字面量定义非全局 mixin props'
        }
      },
      data() {
        return {
D
DCloud_LXH 已提交
44
          handleMixinEmitterMsg: '',
45 46 47 48 49
          mixinDataMsg: '通过字面量定义非全局 mixin data',
          mixinOnloadMsg: ''
        }
      },
      computed: {
D
DCloud_LXH 已提交
50
        mixinComputed(): string {
51 52 53 54 55 56 57 58 59
          const res = `通过字面量定义非全局 mixin computed, 更新后的 mixinOnloadMsg: ${this.mixinOnloadMsg}`
          console.log(res)
          return res
        }
      },
      onLoad() {
        this.mixinOnloadMsg = 'mixin onLoad msg in onLoad'
      },
      methods: {
D
DCloud_LXH 已提交
60
        mixinMethod(): string {
61 62 63
          const res = '通过字面量定义非全局 mixin method'
          console.log(res)
          return res
D
DCloud_LXH 已提交
64 65 66 67
        },
        handleMixinEmitter(emit: string, arg: string) {
          this.handleMixinEmitterMsg = `触发 ${emit}, 参数为 ${arg}`
          console.log(this.handleMixinEmitterMsg)
68 69 70 71
        }
      },
    })]
  }
D
DCloud_LXH 已提交
72
</script>