Vue 使用自定义组件时报错:Uncaught TypeError: Cannot assign to read only property 'exports' of 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,
};

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#' Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#' Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#' Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#‘的解决方法 Uncaught TypeError: Cannot assign to read only property 'exports' of object '#' Vue报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object 的解决方法 vue 项目启动报错:Cannot assign to read only property 'exports' of object '#' vue运行报错error:Cannot assign to read only property 'exports' of object '#' 解决 Cannot assign to read only property 'exports' of object '#' 问题 Cannot assign to read only property 'exports' of object '#'
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM