Vue3+ts中props和emit用法

阅读 58

2022-01-09

将子组件的值传到父组件中

1、emit

 setup(props, { emit }) {
    const foo = () => {
      emit("myevent", 123);
    };
    return {
      foo,
    };
  },
 <button class="child" @click="foo"></button>
  <Child @myevent="chuanCan" />

  setup() {
    const chuanCan = (a: number) => {
      console.log(a);
    };
    return {
      chuanCan,
    };
  },

2、props

 props:{
      foo:{
          type:Function,
          required:true
      }
  },
  setup(props){
      const print=()=>{
          props.foo(123)
      }
      return{
          print
      }
  }

<button class="child" @click="print"></button>
  <Child :foo="chuanCan" />

 setup(){
      const chuanCan=(a:number)=>{
          console.log(a);
          
      }
      return{
          chuanCan
      }
  }

输出结果

 

精彩评论(0)

0 0 举报