vue全局注册

方法一:全局注册(注册全局方法)

创建global.js

export default {
    install (Vue){
        Vue.prototype.XXX = ()=> {
        }
        ...
    }
}

main里面引入:

import global from './utils/global'
Vue.use(global);

调用时候:

this.XXX()

方法二:定义单独的方法

export function fn1(){
}
export function fn2(){
}
...

在需要使用的页面引入即可。

方法三:vue 3.0的方法

export function global(){
    const str = ref("");
    const fn = () => {
        ...
    }
    
    return {
        str,
        fn
    }
}

猜你喜欢

转载自www.cnblogs.com/maizilili/p/12745909.html