公用方法提取

公用方法提取

方法一

export & import
config.js文件

const config = {
 request_prefix: 'http://localhost:10003',
 base_img: 'http://www.baidu.com'
}
const DingConf = function(data){
 xxxxxxxxx
}
export {config, DingConf}

使用:

import {config} from 'src/util/config'      // 引入模块
export default {
  created(){
    this.$http({
      url: config.request_prefix + xxxxxxxxxxxxx  // 使用
    })
  }
}

方法二

util.js

export default {
    install(Vue, options) {
        Vue.prototype.formatDuring = function (mss) {
            var days = parseInt(mss / (1000 * 60 * 60 * 24));
            var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = (mss % (1000 * 60)) / 1000;
            return days + " 天 " + hours + " 小时 " + minutes + " 分 " + Math.round(seconds) + " 秒 ";
        }
    }

}

main.js中引入,以便全局使用

// 引入公用js
import utils from '../static/js/utils.js'    
Vue.use(utils);

使用页面

endline = this.formatDuring(currentTime);

方法三

util,js
mixins一般只加methods里的方法

export default {
    methods: {
        tagChange(val) {
            switch (val) {
                case 'kehuanli':
                    this.$router.push({path: '/gw_cms/cms2/customer-case/list'});
                    break;
                case 'qdinfo':
                    this.$router.push({path: '/gw_cms/cms2/qidian-info/list'});
                    break;
                case 'download':
                    this.$router.push({path: '/gw_cms/cms2/download/list'});
            }
        }
    }
};

使用页面
方法就相当于加入到了methods里

import oldView from '@/mixins/oldView';
export default {
    mixins: [oldView]
}

猜你喜欢

转载自blog.csdn.net/zhong242526/article/details/84939930