0
点赞
收藏
分享

微信扫一扫

向Vue的prototype上绑定或.use()时Cannot read properties of undefined (reading ‘prototype‘)

梦想家们 2022-01-23 阅读 56

使用cli3创建的项目,main中的语法是:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';
import localData from "../src/assets/js/local"

new Vue({
  el: '#app',
  render: h => h(App)
});

挂载全局的语法是:

Vue.use(ElementUI);

Vue.prototype.localData = localData;

但是使用cli4的时候,语法修改成为了:

import { createApp } from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

const app = createApp(App);

app.mount('#app');

如果仍然使用cli3的挂载方法就会报错

Cannot read properties of undefined (reading 'prototype')

修改挂载方式为:

app.use(router);

app.config.globalProperties.$localData = localData;

其中 $localData 前面的 $ 符号为标识,用于和组件内部变量区分(不加也行)
使用的时候为:

this.$localData()
举报

相关推荐

0 条评论