如下是使用vue-cli3.x的一些总结,后续会持续更新
https://cli.vuejs.org/zh/guide/javascript
注:若要使用 Vue CLI 3,需将 Node 版本升级至 8.9 及以上。css
npm install -g @vue/cli //初始化项目 vue create xx //你的项目名称
注:以后的操做都在package.json(同目录)新建vue.config.js进行项目的配置前端
解决build静态文件引入错误的问题vue
module.exports = { publicPath:'./' }
在前端项目中,常常会用到相同的主题色。此时,咱们须要存储这些变量,且将其全局引入。java
安装lesswebpack
npm install less-loader less --save-dev
安装style-resources-loaderweb
npm install style-resources-loader --save-dev
//使用sass把less替换成less便可 function addStyleResource(rule) { rule.use('style-resource') .loader('style-resources-loader') .options({ patterns: [ path.resolve(__dirname, 'src/assets/css/base.less')//你的文件目录 ] }) } module.exports = { chainWebpack: config => { const types = ['vue-modules', 'vue', 'normal-modules', 'normal'] types.forEach(type => addStyleResource(config.module.rule('less').oneOf(type))) }
安装postcss-px2remvue-cli
npm install postcss-px2rem --save-dev
使用lib-flexible后配置px转换成remnpm
module.exports = { css: { loaderOptions: { less: { javascriptEnabled: true }, postcss: { plugins: [require('postcss-px2rem')({ remUnit: 75 })] } } } }
安装webpack-spritesmithjson
npm install webpack-spritesmith --save-dev
module.exports = { configureWebpack:{ plugins:[ new SpritesmithPlugin({ // 目标小图标 src: { cwd: path.resolve(__dirname,'./src/assets/sprite'),//小图标的目录 glob: '*.png' }, // 输出雪碧图文件及样式文件 target: { image: path.resolve(__dirname, './src/assets/images/sprite.png'),//生成雪碧图的目录 css: [[path.resolve(__dirname, './src/assets/css/sprite.less'),css{format:'function_based_template'}]]//生成雪碧图对应的 }, customTemplates: { 'function_based_template': path.resolve(__dirname, './src/utils/my_handlebars_template.handlebars')//模板 }, // 样式文件中调用雪碧图地址写法 apiOptions: { cssImageRef: '../images/sprite.png'//对于雪碧图css对应的雪碧图的相对路径 }, spritesmithOptions: { algorithm: 'binary-tree', padding:10 } }) ] } }
my_handlebars_template.handlebars的代码以下:
{{#spritesheet}} [class^="icon-"], [class*=" icon-"]{ background-image: url({{{escaped_image}}}); background-size:{{px.width}} {{px.height}}; } {{/spritesheet}} {{#sprites}} .icon-{{name}}{ background-position:{{offset_x}}px {{offset_y}}px; width:{{width}}px; height:{{height}}px; } {{/sprites}}
在升级至 Vue CLI 3 以后,直接运行可能会出现以下报错:
[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build. (found in <Root>)
这是由于 3.x 版本默认使用的是运行时模式,须要对 main.js 文件进行修改:
new Vue({ router, store, render: h => h(App) }).$mount('#app');
module.exports = { productionSourceMap:false, }