export default{
name: 'navigation',
components:{
menuIcon,
},
data() {
return {
mobile: null,
mobileNav: null,
windowwidth: null,
}
},
methods: {
checkScreen() {
this.windowwidth = window.innerWidth;
if(this.windowwidth <= 750) {
this.mobile = true;
return;
}
this.mobile = false;
this.mobileNav = false;
return;
},
toggleMobileNav() {
this.mobileNav = !this.mobileNav;
},
},
created() {
window.addEventListener("resize",checkScreen);
this.checkScreen();
},
};
在此,我在 created() 選項卡中使用了 checkScreen() 函式,然后當我保存程式時,編譯器顯示未定義 checkScreen 的錯誤,然后它指向我在 created() 中使用 checkScreen 的行。有人可以澄清為什么會這樣。

uj5u.com熱心網友回復:
您只是忘記了this偵聽器回呼中的關鍵字。
created() {
window.addEventListener("resize", this.checkScreen);
this.checkScreen();
}
Vue 中的this關鍵字使您可以輕松訪問所有資料和功能。無論您是要訪問資料屬性、計算屬性、道具組件還是函式,都可以直接在this關鍵字上找到它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/494908.html
上一篇:刮掉維基百科頁面的小節
下一篇:如何使用Powershell提取pom.xml中不在parent、build或dependencies標簽中的artifactId
