vue搭建安装环境
最近在学习vue,然后总结一下vue项目的环境搭建,这里用的是vue-cli.
这里应该先确定电脑里已经安装了node和npm(判断时候安装了node和npm:node -v 和 npm -v)
因为我的电脑里已经安装了node,所以这里直接安装vue,打开项目目录(比方说要在D盘建文件夹project,然后在project里建vue项目,这里新建的是vuetest,就打开project这一层)
注: 如果npm安装失败或报错改成用淘宝镜像的cnpm安装就可以了。
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$': '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' } }
配置后:
'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$': '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" }) ] })
11. 如果需要使用vux等插件 ,在安装依赖即可,如下:
npm install vux --save
然后安装vux预处理器
npm install vux-loader --save -dev
12. npm install yaml-loader --save -dev (这一步是为了正确进行语言读取,就是将yaml有效的转换成json)
最后在运行一下项目 npm run dev
以上配置可能会有些地方会出现一些报错,查看vue版本或者是看是否少装了东西,如果npm安装错误可以尝试使用cnpm。欢迎一起讨论改正。