javascript - export default 和 module.exports怎么用?
迷茫
迷茫 2017-04-11 13:10:03
[JavaScript讨论组]

最近想学着写点vue的插件,但是碰到模块输出的问题

如图上两段不同的代码,是我在网上找的,左边第②种写法就会报错,但是右边相同的写法都没事

报错:Cannot assign to read only property 'exports' of object '#'

附上完整的代码:

// 左边的

var plugin = {};
plugin.install = function(Vue) {
  if (plugin.installed) {
    return;
  }
  var get = function(a){console.log('Hello  ' +a)}
  Vue.prototype.who = get;
  Object.defineProperties(Vue.prototype, {
    $who: {
      get() {
        return {get:get}
      }
    }
  });
  Vue.mixin({
    created: function () {
      console.log('Plugin activiated')
    }
  })
};

// export default plugin;
module.exports = plugin;

// 右边的
/**
 * Created by Administrator on 2017-04-01.
 */

var Toast = {};
Toast.install = function (Vue, options) {
  var opt = {
    defaultType: 'bottom',
    duration: '2500'
  }
  for (var property in options) {
    opt[property] = options[property];
  }
  Vue.prototype.$toast = function (tips, type) {

    var curType = type ? type : opt.defaultType;

    if (document.querySelector('.lx-toast')) {
      // 如果toast还在,则不再执行
      return;
    }
    var toastTpl = Vue.extend({
      template: '

' + tips + '

' }); var tpl = new toastTpl().$mount().$el; document.body.appendChild(tpl); setTimeout(function () { document.body.removeChild(tpl); }, opt.duration) }; ['bottom', 'center', 'top'].forEach(function (type) { Vue.prototype.$toast[type] = function (tips) { return Vue.prototype.$toast(tips, type) } }); Vue.prototype.$loading = function (tips, type) { var load = document.querySelector('.lx-load-mark'); if (type == 'close') { load && document.body.removeChild(load); } else { if (load) { // 如果loading还在,则不再执行 return; } var loadTpl = Vue.extend({ template: `

${tips}

` }); var tpl = new loadTpl().$mount().$el; document.body.appendChild(tpl); } }; ['open', 'close'].forEach(function (type) { Vue.prototype.$loading[type] = function (tips) { return Vue.prototype.$loading(tips, type) } }); } module.exports = Toast;
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(1)
阿神

新版 webpack 的一个 bug,用 export default 就好了

https://github.com/webpack/we...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号