我只是從 alpine.js 切換到 vue.js (cdn) 因為我嘗試了異步函式。我的目標是從組件中定義的父級呼叫一個方法。
但是當我想在我的 html 檔案中呼叫“test()”時,我收到錯誤“無法讀取未定義的屬性(讀取‘childRef’)” 當在“app”元素(父) 但我不想用不同的方法來弄亂這個元素。
預先感謝對我的代碼的每一個提示或評論。
代碼片段 Vue/Javascript
--------------------------------------
const app = Vue.createApp ({
data() {
return {
visible: false,
openTab: ''
}
},
methods: {
test() {
this.$resfs.childRef.getMyDevices();
}
}
})
--------------------------------------
app.component('child',{
data() {
return {
myDevices: {},
}
},
methods: {
async getMyDevices() {
const response = await foo.getMyDevices();
const data = await {...response.devices};
this.myDevices = data;
}
},
template: `<div ref="childRef"></div>`
})
代碼片段 HTML
<div @click="test()>
<child ref="childRef"></child>
</div>
uj5u.com熱心網友回復:
您的問題有錯別字,請更正 refs 拼寫
this.$refs.childRef.getMyDevices()
如果上述方法不起作用,請嘗試使用
this.$children[0].getMyDevices()
uj5u.com熱心網友回復:
您的$refs訪問有誤:
methods: {
test() {
this.$refs.childRef.getMyDevices(); // $resfs befores
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/348551.html
