Vue 使用自定义组件时报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

自己试做了一下vue的插件

参考element-ui:

写了一个组件

import message from './packages/message/index.js';

const install = function (Vue, options) {
    if(install.installed ) return;
      //console.log(Vue)
    // 1. 添加全局方法或属性
    Vue.prototype.myMessage= message;


    Vue.prototype.dtime=function(time){
        let d=Date.parse(time);
        let now =Date.parse(new Date()); 
        //console.log(now - d);
        let sec= parseInt((now- d)/1000);

        if(sec <60){
            return sec+'秒前';
        }else if(sec <3600){
            return parseInt(sec/60)+'分钟前';
        }else if(sec <(24*3600)){
            return parseInt(sec/3600) +'小时前';
        }else if(sec <(24*3600*30)){
            return parseInt(sec/24/3600) + '天前';
        }else{
            return "一个月以前";
        }
        //return t[0]+t[1].substr(0,8);
    }



 }

module.exports = {
    install
}

结果就报

Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

后来网上查了之后说,这个是因为webpack 2中不允许混用import和module.exports,

解决办法就是统一改成ES6的方式编写即可.

export default {
    install,
};
原文地址:https://www.cnblogs.com/zjhblogs/p/7199946.html