vue + ts 项目中Emit的用法

要使vue支持ts写法,我们需要用到vue-property-decorator这个组件完全依赖于vue-class-componet

首先安装:

npm i -D vue-property-decorator

 @Emit(event?: string)

@Emit装饰器接收一个可选参数,作为事件名;如果没有提供这个参数,$emit会将回调函数的camelCase(驼峰式)转为kebab-case(短横线命名),并将其作为事件名;

@Emit会将回调函数的返回值作为第二个参数,如果返回值是一个Promise对象,$emit会将Promise对象状态为resolved之后触发;

@Emit的回调函数的参数,会放在其返回值之后,一起被$emit当作参数使用;

看下面例子:

import { Vue, Component, Emit } from 'vue-property-decorator'

@Component
export default class YourComponent extends Vue {
  count = 0

  @Emit()
  addToCount(n: number) {
    this.count += n
  }

  @Emit('reset')
  resetCount() {
    this.count = 0
  }

  @Emit()
  returnValue() {
    return 10
  }

  @Emit()
  onInputChange(e) {
    return e.target.value
  }

  @Emit()
  promise() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(20)
      }, 0)
    })
  }
}

以上代码等同于:

export default {
  data() {
    return {
      count: 0,
    }
  },
  methods: {
    addToCount(n) {
      this.count += n
      // 将addToCount转成add-to-count
      this.$emit('add-to-count', n)
    },
    resetCount() {
      this.count = 0
      this.$emit('reset')
    },
    returnValue() {
      this.$emit('return-value', 10)
    },
    onInputChange(e) {
      this.$emit('on-input-change', e.target.value, e)
    },
    promise() {
      const promise = new Promise((resolve) => {
        setTimeout(() => {
          resolve(20)
        }, 0)
      })

      promise.then((value) => {
        this.$emit('promise', value)
      })
    },
  },
}

参考资料:https://github.com/kaorun343/vue-property-decorator#Emit

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3,通过使用v-model实现父子组件的双向数据绑定的方式与Vue2有所不同。在Vue3,我们可以使用`emit`和`on`来实现父子组件间的双向绑定数据。 在父组件,我们可以通过`v-model`指令绑定一个值,并使用`@update:modelValue`事件和`$emit`方法来更新这个值。在子组件,我们可以通过`props`接收父组件传递的值,并在需要更新该值的时候使用`$emit`触发`update:modelValue`事件。 以下是一个使用Vue3和TypeScript实现父子组件间双向绑定数据的示例: 父组件: ```html <template> <div class="father"> <div class="context"> <h1>这是父组件fatherNum:{{ fatherNum }}</h1> <Son v-model="fatherNum"></Son> </div> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; import Son from '../components/Son.vue'; export default defineComponent({ components: { Son }, setup() { const fatherNum = ref(0); return { fatherNum }; } }); </script> <style scoped> .father { width: 100vw; height: 100vh; } .context { width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; flex-direction: column; } </style> ``` 子组件: ```html <template> <div class="son"> <button @click="increment">增加</button> <button @click="decrement">减少</button> <p>这是子组件sonNum: {{ modelValue }}</p> </div> </template> <script lang="ts"> import { defineComponent, computed, emit } from 'vue'; export default defineComponent({ props: { modelValue: { type: Number, required: true } }, setup(props, { emit }) { const increment = () => { emit('update:modelValue', props.modelValue + 1); }; const decrement = () => { emit('update:modelValue', props.modelValue - 1); }; return { increment, decrement }; } }); </script> <style scoped> .son { display: flex; } </style> ``` 在这个示例,父组件的`fatherNum`通过`v-model`指令与子组件的`modelValue`进行双向数据绑定。父组件的值变化时,会触发`update:modelValue`事件,子组件监听该事件并根据需要更新自己的值。 希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue3+vite+ts--组件使用v-model实现双向绑定(vue2&vue3+ts的详细讲解)](https://blog.csdn.net/m0_60893808/article/details/131244155)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值