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


本篇内容介绍了“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中怎么定义全局方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!

返回大数据教程...