一、使用webpack初始化一个项目
vue init webpack-simple vueadmin
使用webpacck-simple模板创建项目,
$ vue init webpack-simple vueadmin
? Project name vueadmin
? Project description a project by vue2.0
? Author kingQQ673575951
? License MIT
? Use sass? No
vue-cli · Generated "vueadmin".
To get started:
cd vueadmin
npm install
npm run dev
二、引入elementui
http://element.eleme.io/#/zh-CN
npm i element-ui -S
main.js
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App.vue'
Vue.use(ElementUI)
注意这个错误:
Failed to compile.
./node_modules/_element-ui@2.2.0@element-ui/lib/theme-chalk/fonts/element-icons.ttf Module parse failed: Unexpected character '' (1:0) You may need an appropriate loader to handle this file type. (Source code omitted for this binary file) @ ./node_modules/_css-loader@0.28.10@css-loader!./node_modules/_element-ui@2.2.0@element-ui/lib/theme-chalk/index.css 7:327-363 @ ./node_modules/_element-ui@2.2.0@element-ui/lib/theme-chalk/index.css @ ./src/main.js @ multi ./node_modules/_webpack-dev-server@2.11.2@webpack-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js
错误的原因是element-ui里面的图标是字体,需要使用一个loader处理
修改webpack.config.js 添加一个loader
//element ui里面有字体文件 必须引入
{ test: /\.(eot|woff|ttf)$/, loader: 'file-loader' },
总结
- 注意引入elementui后的错误
webpack.config.js
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
}, {
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
}
// other vue-loader options go here
}
},
//element ui里面有字体文件 必须引入
{ test: /\.(eot|woff|ttf)$/, loader: 'file-loader' },
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
main.js
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
new Vue({
el: '#app',
render: h => h(App)
})
package.json
{
"name": "myadmin",
"description": "A Vue.js project",
"version": "1.0.0",
"author": "oneboi <673575951@qq.com>",
"license": "MIT",
"private": true,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"axios": "^0.17.1",
"element-ui": "^2.0.11",
"style-loader": "^0.19.1",
"vue": "^2.5.11",
"vue-router": "^3.0.1"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"
}
}