1 回答

TA貢獻(xiàn)1790條經(jīng)驗 獲得超9個贊
listener 里的 function 的 this 不對吧, 指向的應(yīng)該不是 vue 的 this, 換成() => 應(yīng)該就好了.
<script>
export default {
data: () => ({
isShowCover: false
}),
mounted() {
let self = this; // 這里
window.addEventListener(
"onorientationchange" in window ? "orientationchange" : "resize",
function() {
if (window.orientation === 90 || window.orientation === -90) {
//想把下面的alert換成能夠控制v-show的代碼
alert(
self.$options.methods.showCover() +
"橫屏可能導(dǎo)致頁面異常,建議豎屏操作!"
); // 這里用 this 作用域就不對了.
//alert("123");僅alert純文本可以正常運行
}
//window.location.reload();
},
false
);
},
methods:{
showCover() {
return "123";
},
}
</script>
添加回答
舉報