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