- 创建一个组件,并在页面上调用引用
- 在页面引用组件时,通过@绑定父组件内定义好的方法
- 在子组件内绑定@tap触发子组件内定义的方法,同时在组件内通过this.$emit触发父组件内绑定的方法
父页面
//父页面
<template>
<view class="father">
<child @ToChange="change"></child>
</view>
</template>
<script>
import child from '../../components/child'
export default {
...
methods: {
change(值1,值2){
console.log("触发了父页面内的方法");
},
},
components:{
child
}
}
</script>
子组件
// 子组件
<template>
<view @tap="change()">
子组件
</view>
</template>
<script>
export default {
...
methods:{
change(){
this.$emit("Tochange",需要传的值1,需要传的值2);
console.log("触发了子组件内的方法");
}
}
}
</script>