Vue封装工具类以及使用

1.在vue项目中src/common/common.js/创建js文件 例:common.js
2.在main.js 引用common->然后实例化

/* 引入公共函数 */
import common from './common/common'
Vue.use(common);

3.common.js写一个示例

export default {
    install(Vue) {
        //这里是示例方法 getTime是方法名 function()可以携带参数
        //vue.prototype vue实例
        Vue.prototype.getTime = function () {
            var date = new Date()
            var y = date.getFullYear()
            var m = date.getMonth() + 1
            m = m < 10 ? ('0' + m) : m
            var d = date.getDate()
            d = d < 10 ? ('0' + d) : d
            // alert(y + m + d)
            console.log(y + m + d);
        }
        Vue.prototype.getApp = function (app) {
            console.log(app);
        }
    }
}

这里我们就封装好了示例公共方法,接着我们如何显示调用

在任何一个vue文件里面的生命周期this.方法名就可以了,因为已经在main.js全局实例化了

created() {
    this.getTime();
    let app = 123456;
    this.getApp(app);
},

猜你喜欢

转载自blog.csdn.net/weixin_52726621/article/details/131125854