setup两种写法

57 阅读1分钟
<template>
  <view class="content">
    <van-button type="primary" @click="clickChild">发送数据到父组件</van-button>
    {{ title }}
  </view>
</template>
<script setup lang="ts">
import { ref } from "vue";
let num = ref(1)
const props = defineProps({
  title: {
    type: String,
    default: '',
  },
})
const emit = defineEmits<{
  (event: 'clickChild', num: number): void
}>()
const clickChild = () => {
  num.value += 1
  emit('clickChild', num.value)
}
</script> 

<script lang="ts">
import { ref, defineProps, defineEmits } from 'vue'
export default {
  emits: ['clickChild'],
  props:['title'],
  setup(props, { emit }) {
    let num = ref(1)
    const clickChild = () => {
      num.value += 1
      emit('clickChild', num.value)
    }
    return {
      num,
      clickChild,
    }
  },
}
</script>