[英]calling grandparent method from inside the grandchild component
我有 3 个嵌套组件
<GrandParent />
<Parent />
<Child />
我的Child
组件中有一个按钮,双击时我想在GrandParent
组件中调用 function
<button @dblclick="this.$parent.callGrandParentFunction(model)"> call grandparent </button>
使用this.$parent
我只能访问Parent
方法...有没有办法将 go 提高一级并调用GrandParent
方法?
SO上有一个类似的问题,但它是关于vue2
尝试使用提供/注入模式:
祖父母:
export default {
methods: {
someMethod(){
//
}
},
provide() {
// use function syntax so that we can access `this`
return {
someMethod: this.someMethod
}
}
}
在 GrandChild 组件中:
export default {
inject: ['someMethod'],
created() {
//run the method
this.someMethod()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.