[英]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.