尝试使用 webpack4 使用 Vue 和 ElementUI 设置项目。我想从 CDN 中提取 Vue 和 ElementUI,所以我有以下 webpack 配置
module.exports = {
mode: "development",
entry: ["./app.js"],
externals: {
vue: "Vue",
"element-ui": "ElementUI"
},
module: {
rules: [
{
test: /.vue$/,
use: "vue-loader"
},
{
test: /.css$/,
use: ["style-loader", "css-loader"]
},
{
test: /.ttf$|.woff$/,
use: [{ loader: "url-loader", options: { limit: 10000 } }]
}
]
}
};
我的 html 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<title>Document</title>
<style>
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<div v-cloak id="root">
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.runtime.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.3.4/index.js"></script>
<script src="./dist/main.js"></script>
</body>
</html>
我的 app.js 有根 Vue 实例
import Vue from "vue";
import ElementUI from "element-ui";
import Counter from "./Counter.vue";
Vue.use(ElementUI);
var app = new Vue({
el: "#root",
render: h => h(Counter)
});
计数器组件在下面
<script>
export default {
data() {
return {
counter: 0
};
},
methods: {
incement() {
this.counter++;
}
}
};
</script>
<template>
<div>
<p><span>Count: </span>{{counter}}</p>
<el-button @click="incement">Incement</el-button>
</div>
</template>
当我在浏览器中打开应用程序时,出现以下错误
external_"ElementUI":1 Uncaught ReferenceError: ElementUI is not defined at this line in webpack generated js file module.exports = ElementUI;
我有什么遗漏吗?Vue 外部工作没有任何问题,只有元素 UI 有问题