Vue中如何給Window對(duì)象添加方法
給Window對(duì)象添加方法
大家都知道vue中所有元素都是作用于Vue實(shí)例的,可是我使用DCloud的Wap2App打包App之后需要配置sitemap.json,配置onclick事件,但是該事件只能綁定Window下的方法,所以此時(shí)就需要在Vue中定義一個(gè)方法,并將其綁定在Windows對(duì)象下
首先在 App.vue methods 中定義一個(gè)方法
methods:{
? ? share(){
? ? ? ? //微信分享
? ? ? ? ...
? ? }
}然后在 mounted 中寫下如下代碼,將其綁定在 Window 對(duì)象下
mounted(){
? ?// 將分享方法綁定在window上
? ?window['share'] = () => {
? ? ?this.share()
? ?}
},此時(shí) Window 對(duì)象下就有了一個(gè) share 方法可以被調(diào)用
為window對(duì)象添加事件處理程序
以resize事件為例,要獲取窗口變化時(shí)的窗口大?。涸赾reated鉤子函數(shù)中為window對(duì)象添加事件處理程序
var app = new Vue({
el: '#app',
data: {
winWidth: {
type: Number
},
winHeight: {
type: Number
}
},
methods: {
viewWidth() {
return window.innerWidth || document.documentElement.clientWidth;
},
viewHeight() {
return window.innerHeight || document.documentElement.clientHeight;
},
updateWindow() {
this.winWidth = this.viewWidth();
this.winHeight = this.viewHeight();
}
},
created() {
this.updateWindow();
window.onresize = () => {
this.updateWindow();
}
}
});
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
VUE?axios每次請(qǐng)求添加時(shí)間戳問題
這篇文章主要介紹了VUE?axios每次請(qǐng)求添加時(shí)間戳問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
vue3中element-plus?icon圖標(biāo)的正確使用姿勢
element-plus官方提示,Icon圖標(biāo)正在向SVG?Icon遷移,之前使用的Font?Icon即將被棄用,下面這篇文章主要給大家介紹了關(guān)于vue3中element-plus?icon圖標(biāo)的正確使用姿勢,需要的朋友可以參考下2022-03-03

