vue封装公共方法(export default install)

一、调用1:

1、首先,在assets文件夹下,新建common文件夹,创建util.js

2、定义相关方法

//对外接口
export default {    
    text(){
        console.log("测试,测试!!!")
    },

    throttle(fn){
        let canRun = true;
        return function () {
        if (!canRun) return;
        canRun = false;
        setTimeout(() => {
            fn()
            canRun = true;
        }, 3000);
       }
    }
}

3.1、单个.vue文件使用

import utils from '@/assets/common/utils.js'

相对应的地方调用:utils.text();或者utils.throttle()

3.1、多页面使用

将公共方法挂载到this

1.在main.js中引入 import utils from '@/assets/common/utils.js'

2.Vue.prototype.$utils =

你可能感兴趣的:(vue,vue.js,javascript,前端)