美文网首页
vue模仿axios封装插件

vue模仿axios封装插件

作者: Felicity0512 | 来源:发表于2019-05-20 10:15 被阅读0次

插件文件toast.js

import Vue from 'vue';

/* js主体 */
let toast= function(options) {
  this._name = 'name';
}

toast.prototype = {
  VERSION: '0.0.1',
  log: function(){
    console.log(this._name)
  },
  get: function() {
    return this._name
  }
}

toast.create = function(options) {
  return new toast(options);
}
/* js主体 */

let config = {};
const _toast = toast.create(config);

let Plugin = {};
Plugin.install = function (Vue, options) {
  Vue.toast = _toast;
  window.toast = _toast;
  Object.defineProperties(Vue.prototype, {
    toast: {
      get() {
        return _toast;
      }
    },
    $toast: {
      get() {
        return _toast;
      }
    }
  });
}

Vue.use(Plugin);

export default Plugin

main.js引入

import './plugins/toast'

使用

export default {
  created() {
    toast.log();
    console.log(toast.get());
  }
}

相关文章

网友评论

      本文标题:vue模仿axios封装插件

      本文链接:https://www.haomeiwen.com/subject/byshzqtx.html