参考链接:https://www.cnblogs.com/ly-qingqiu/p/10330047.html
1. 安装淘宝镜像
npm install -g cnpm --registry=https://registry.npm.taobao.org
注: 这里有可能会报错:operation not permitted, 这是因为没有用管理员身份打开终端,用管理员身份打开然后重新安装就可以了
2. 安装webpack
npm install webpack -g
3. 安装vue-cli
npm install vue-cli -g
4. 创建项目
vue init webpack vuetest
5. 进入新建的项目层
cd vuetest
6. 安装项目依赖
npm install
7. 安装vue路由模块vue-router和网络请求模块vue-resource
npm install vue-router vue-resource --save
8. 启动项目
npm run dev
9. 安装less
npm install less less-loader --save -dev
10. 配置webpack config
配置前:
'use strict' const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf') function resolve (dir) { return path.join(__dirname, '..', dir)
}
const createLintingRule = () => ({
test: /\.(js|vue)$/,
loader: 'eslint-loader',
enforce: 'pre',
include: [resolve('src'), resolve('test')],
options: {
formatter: require('eslint-friendly-formatter'),
emitWarning: !config.dev.showEslintErrorsInOverlay
}
})
module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js' },
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: { 'vue/pre>: 'vue/dist/vue.esm.js', '@': resolve('src'),
}
},
module: {
rules: [
...(config.dev.useEslint ? [createLintingRule()] : []),
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
node: { // prevent webpack from injecting useless setImmediate polyfill because Vue
// source contains it (although only uses it if it's native).
setImmediate: false, // prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty' }
}</pre>
配置后:
'use strict' const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
const vuxLoader = require('vux-loader')
const webpack = require("webpack") // const webpackConfig = originalConfig // 原来的 module.exports 代码赋值给变量 webpackConfig
function resolve (dir) { return path.join(__dirname, '..', dir)
}
const webpackConfig = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js' },
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: { 'vue/pre>: 'vue/dist/vue.esm.js', '@': resolve('src'), //设置让@代表src目录 引用时候用到src目录的地方就可以直接用@代替
'styles': resolve('src/assets/style'), 'img': resolve('src/assets/img'), 'jquery': 'jquery' }
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
node: { // prevent webpack from injecting useless setImmediate polyfill because Vue
// source contains it (although only uses it if it's native).
setImmediate: false, // prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty' }
}
module.exports = vuxLoader.merge(webpackConfig, {
plugins: [ 'vux-ui', new webpack.ProvidePlugin({
jQuery: "jquery",
$: "jquery" })
]
})</pre>
11.如果需要使用vux等插件 ,在安装依赖即可,如下:
npm install vux --save
然后安装vux预处理器
npm install vux-loader --save -dev
12. 这一步是为了正确进行语言读取,就是将yaml有效的转换成json
npm install yaml-loader --save -dev
13. 最后在运行一下项目
npm run dev
网友评论