Vue 父组件调用子组件的方法 和 拿值 赋值
主组件
< template >
< div >
< uuu < span style="color: #ff0000;" data-mce-style="color: #ff0000;">ref='test_hanshu' > < span style="color: #ff0000;" data-mce-style="color: #ff0000;">//ref 给子组件命名一个名字
< p @click="clickme">点击我
< script >
import uuu from "../17/17-2copy"
export default {
data() {
return {
visible: false,
};
},
components: {
uuu
},
methods: {
clickme(){
const a="我是超人";
< span style="color: #ff0000;" data-mce-style="color: #ff0000;">this.$refs.test_hanshu.lucax(a) //调用子组件里面的方法
}
}
};
后计: $refs 也可以拿子组件的 值 修改
this.$refs.test_hanshu.值=123
子组件
< template >
< div >
我是17-2
< script >
export default {
data() {
return {
visible: false,
};
},
methods: {
< span style="color: #ff0000;" data-mce-style="color: #ff0000;">lucax(uir) {
console.log(uir)
}
}
};
参考 https://www.cnblogs.com/ygyy/p/13396213.html
子组件 调用父组件的方法
https://blog.csdn.net/zgrkaka/article/details/100528714
父组件
<template>
<div>
<child @fatherMethod="fatherMethod"></child>
</div>
</template>
<script>
import child from '~/components/dam/child';
export default {
components: {
child
},
methods: {
fatherMethod() {
console.log('测试');
}
}
};
</script>
子组件
<template>
<div>
<button @click="childMethod()">点击</button>
</div>
</template>
<script>
export default {
methods: {
childMethod() {
this.$emit('fatherMethod'); 传参数的话 这么写this.$emit('fatherMethod','要传的参数');
}
}
};
</script>

浙公网安备 33010602011771号