利用Vue的插件机制,实现项目公共代码管理

import Vue from "vue";
import axios from "axios";
import { apis } from "@/static/js/apis.js";
// import NoData from "@/components/NoData.vue";


let myVuePlugin = new Object();
myVuePlugin.install = function(Vue) {
  /**
   *  接口全局化
   * */
  Vue.prototype.apis = apis;
  /**
   *  get 请求
   *  @params String url
   *  @params Object params
   *  @use this.get(url,params:{}).then(fn).catch(fn)
   * */
  Vue.prototype.get = axios.get;
  /**
   *  post 请求
   *  @params String url
   *  @params Object params
   *  @use this.post(url,{}).then(fn).catch(fn)
   * */
  Vue.prototype.post = axios.post;
  /**
   *  post 请求
   *  @params String url
   *  @params Object params
   *  @use this.post(url,{}).then(fn).catch(fn)
   * */
  Vue.prototype.changeRouter = function(path) {
    this.$router.push({
      path,
    });
  };
  /**
   *  全局无数据组件
   * */
  // Vue.component('NoData', NoData);
  Vue.component('NoData', () => import("@/components/NoData.vue"));
};

Vue.use(myVuePlugin);

 

posted @ 2020-07-22 16:50  奔跑的太阳花  阅读(336)  评论(0编辑  收藏  举报