webpack-vue
vue-cli官方脚手架安装
安装官方脚手架(https://github.com/vuejs-templates/webpack)
1 |
|
开发环境运行
1 | npm run dev |
生产环境准备生成dist
1 | npm run build |
官方脚手架支持单元测试
1 | npm run unit |
这个模板是Vue 2.0兼容。对于Vue 1.x,请使用以下命令:1
vue init webpack#1.0 my-project
官方脚手架中用到eslint,若不需要使用在项目初始化时执行到选择是否用eslint时输入N再回车
若项目已经初始化完毕需要关闭找到/build/webpack.base.conf.js 在其中注释掉eslint


Vue-cli proxyTable 解决开发环境的跨域问题
在Vue-cli项目中webpack.dev.conf.js中devServer是这样的1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18devServer: {
clientLogLevel: 'warning',
historyApiFallback: true,
hot: true,
compress: true,
host: HOST || config.dev.host,
port: PORT || config.dev.port,
open: config.dev.autoOpenBrowser,
overlay: config.dev.errorOverlay
? { warnings: false, errors: true }
: false,
publicPath: config.dev.assetsPublicPath,
proxy: config.dev.proxyTable,
quiet: true, // necessary for FriendlyErrorsPlugin
watchOptions: {
poll: config.dev.poll,
}
}
其中proxy是设置代理的,这里的代理指向config/index.js中的proxyTable,所以说在Vue-cli官方脚手架项目中设置代理是在config/index.js中设置
在config/index.js中的dev是这样的
1 | dev: { |
因为webpack.dev.conf.js中的proxy指向config/index.js中的proxyTable
所以我们设置代理需要在proxyTable中设置1
2
3
4
5
6
7
8
9
10
proxyTable: {
'/apis': { //将www.exaple.com印射为/apis
target: 'https://www.exaple.com', // 接口域名
changeOrigin: true, //是否跨域
pathRewrite: {
'^/apis': '' //需要rewrite的,
}
}
}
vue-cli引入scss和less
vue-cli中已经内置配置好了sass 以及lass的配置。如果需要的话直接下载两个模块就可以了,webpack它会根据 lang 属性自动用适当的加载器去处理。
如果需要使用sass,则安装:
1 | npm install node-sass --save-dev |
sass的内联写法:
1 | <style lang="sass" scoped> |
sass的引用写法:
1 | <style lang="sass" src="./index.sass"></style> |
如果需要使用less,则安装:
1 | npm install less --save-dev |
less的内联写法:
1 | <style lang="less" scoped> |
less的引用写法:
1 | <style lang="less" src="./index.less"></style> |