uni-app、Vue组件的使用-引用子组件、传值
1 //子组件 bar.vue 2 34 56 7 23 24 // 父组件 foo.vue 25 262729 30 31:title="title" @helloWorld="helloWorld"> 28
1.引用:
①在需要使用的父组件中通过import
引入;
②在vue
的components
中注册;
③在父组件中直接引用,如:
2.传值给子组件
①在父组件通过v-bind
传入一个值,如:
②在子组件中,通过props
接收,如:
props:{ // 获取父组件传来的值 title:{ type:String, default:'' } } },
3.传值给父组件——通过this.$emit
将方法和数据传递给父组件
①子组件:this.$emit('helloWorld') // 传值给父组件
②父组件:
helloWorld(){ console.log('我接收到子组件传递的事件了') }