vue怎么定义全局
更新:HHH   时间:2023-1-7


这篇“vue怎么定义全局”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“vue怎么定义全局”文章吧。

1.利用全局混入mixin定义全局方法

Vue.mixin(mixin)

new Vue({

store,

router,

render: h => h(App),

}).$mount('#app')


2.通过prototype挂载定义全局方法

Object.keys(tools).forEach(key => {

Vue.prototype[key] = tools[key]

})


3.使用Plugin方法定义全局方法

const install = function (Vue, opts) {

Vue.prototype.$pluginDemo = function () {

console.log('')

}

}

export default {

install

}


4.直接在vue文件中定义全局方法

//定义全局方法

this.$root.$on('test',function(){

console.log("test")

})

//销毁全局方法

this.$root.$off("test')

//调用全局方法

this.$root.$emit("test")


以上就是关于“vue怎么定义全局”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注天达云行业资讯频道。

返回开发技术教程...