因为版本存在变更,如下安装和配置都有可能发生变化,请以官方文档为准。css
html-webpack-plugin
是在内存中生成html模板的插件pluginhtml
首先安装插件npm install html-webpack-plugin -D
,而后在配置文件webpack.config.js
中进行插件配置:vue
const HtmlWebpackPlugin = require('html-webpack-plugin'); //.... plugins: [ //注意:模板地址未找到会报错 new htmlWebpackTemplate({ template: path.join(__dirname, "/src/index.html"), filename: 'index.html' }) ]
style-loader
&css-loader
一样的先安装loader模块:node
npm install style-loader css-loader -D
而后在配置文件中进行模块向配置:webpack
module: { rules: [ {test: /\.css$/, use: ['style-loader', 'css-loader']},//注意顺序不能错,从后往前调用 ] }
less-loader
;scss loader:sass-loader
同时同样的配置方法。相似的非js文件都须要咱们手动去配置对应的文件加载loader。es6
url-loadr
&file-loader
有时候咱们须要在css中引用图片等文件做为背景,这个时候涉及到url的解析,以及文件的加载,因此须要url和file的loader。web
安装后基本配置:vue-router
//file-loader是url-loader的内部依赖,因此配置url-loader便可。 {test: /\.(jpg|png|bmp|jpeg|gif)$/, use: 'url-loader'}
注意:shell
加载资源时默认加载为base64
字节形式,但能够在配置中进行限制:npm
{test: /\.(jpg|png|bmp|jpeg|gif)$/, use: 'url-loader?limit=2048name=[name].[ext]'} //使用limit参数来限制字节数,大于或等于limit值时不转为base64字符。 //name参数是保留文件名和后缀。可是这样在引入不一样目录的同名文件时先引入的文件会被覆盖。 //能够设置hash值进行区别:url-loader?limit=2048&name=[hash:8][name].[ext]
使用字体图标时一样须要使用url-loader
:
好比咱们使用bootstrap
中的字体图标(注意安装的时候请安装3.x.x
的版本,4的版本不带字体图标)。
咱们能够直接在index.js
中使用,首先引用css文件,而后再随便使用一个字体图标便可看到效果。
字体loader配置:
{test: /\.(ttf|svg|eot|woff|woff2)$/, use: 'url-loader'}
咱们能够在页面中这样使用:
<!--引入css--> <link rel="stylesheet" type="text/css" href="../node_modules/bootstrap/dist/css/bootstrap.min.css"> <!--使用字体图标--> <span class="glyphicon glyphicon-music" aria-hidden="true"></span>
既然咱们使用了webpack
,就不推荐这样直接在页面引入的形式,咱们应该在入口文件main.css
中引入这个bootstrap.css
文件,像这样:
//注意:对于导入node_modules下面的文件可省略前面的路径 import 'bootstrap/dist/css/bootstrap.min.css';
babel
模块的使用有时候咱们但愿使用最新es6语法
进行项目开发,可是webpack
可能尚未最好支持的时候,咱们就能够安装配置babel
相关的模块进行js
文件的解析加载。
若是在webpack
中某些es6
或es7
的高级语法号没法直接使用时,还须要使用一些loader来处理这些高级语法。
npm install babel-core babel-loader babel-plugin-transform-runtime -D npm install babel-preset-env babel-preset-stage-0 -D
说明:babel-preset-env
,babel-preset-stage-0
为babel须要的语法模块;另外的为须要的语法转换器模块。
{test: /\.js$/, use: 'babel-loader', exclude:/node_modules/} //配置 babel 规则时必须把 node_modules 目录排除,若是不排除会把目录下的js文件都打包,这样会很是消耗cpu。
.babelrc
配置文件在项目根目录新建.babelrc
配置文件,必须符合json格式规范。(不能注释;字符串必须使用双引号等)
配置项:
{ "presets": ["env", "stage-0"], "plugins": ["transform-runtime"] }
注意不须要插件或语法的前缀。
更多细节请参考:https://webpack.docschina.org/loaders/babel-loader/#src/components/Sidebar/Sidebar.jsx
webpack
中使用vue
在使用以前咱们先安装Vue
:
npm install vue -D
先回顾一下组件的使用:
//建立一个组件模板 var buttonCounter = { data: function(){ return { count: 0, } }, template: '<a @click="++count">you click me {{count}} times!</a>' } //全局注册 Vue.component('button-counter', buttonCounter); //局部注册 new Vue({ el: '#app', components:{ 'button-counter': buttonCounter, } });
<!--组件接口--> <button-counter></button-counter>
咱们先尝试直接拿到webpack
环境下使用看看可否直接使用:
//导入vue模块 import Vue from 'vue'; //建立一个组件模板 var buttonCounter = { data: function(){ return { count: 0, } }, template: '<button @click="++count">you click me {{count}} times!</button>' } //注册到Vue实例中 new Vue({ el: '#app', components:{ 'button-counter': buttonCounter, } });
访问页面发现运行报错:
vue.runtime.esm.js:620 [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.
报错的缘由是vue在node中引用的这个js文件是runtime-only
运行时环境包,功能并无原来页面中所引用的全面,这个包没法直接渲染组件到页面中。
咱们能够手动更改引入的包:
import Vue from 'vue/dist/vue.js'
这样再运行就不会报错了。
除了使用component
挂在的方式,还可使用render
函数进行渲染,可是二者渲染有所区别:
new Vue({ el: '#app', render: function(createElement){ return createElement(html) } });
既然Vue默认使用了运行时环境的包,就是但愿咱们使用这个包进行开发而不是手动去引用完整包。
运行时的模块没法直接渲染,可是可使用render
函数配合单文件组件
来使用。
注意,render
函数会将实例的已有内容清空再渲染,因此若是使用了render
函数进行页面渲染,实例根元素内部的元素都没有必要再书写了。
可是若是咱们的实例内部元素都写在组件注册中,无疑太冗杂,因此咱们须要使用单文件组件进行开发。
文件扩展名为 .vue
的 single-file components(单文件组件) 解决了Vue.component
定义组件存在的相关局限,而且还可使用 webpack 或 Browserify 等构建工具,也更符合模块式编程的思想。
因为引用的为.vue
格式的文件,天然须要安装配置相应的loader:
npm install vue-loader vue-template-compiler -D
{test: /\.vue$/, use: 'vue-loader'}
此外,还须要确保在你的 webpack 配置中添加 Vue Loader 的插件(必须):
const VueLoaderPlugin = require('vue-loader/lib/plugin') //... plugins: [ new VueLoaderPlugin() ]
这样,咱们就能够直接把.vue
格式的文件引入做为render
的模板文件。
书写一个相似这样的单文件组件:由三部分组成,
<template> <div class="example"> {{msg}} </div> </template> <script> export default { data () { return { msg: 'Hello world!' } } } </script> <style> .example { color: red; } </style>
能够发现, 单文件组件比普通的组件注册多了css
控制。
参考地址:
值得注意的是,module.exports
导出模块接口的方法是Node
提供的,符合commonJS规范
,导入模块时使用的是require()
方法。
而es6对
于模块的两个关键字,import
和export
,一样可使用require
关键字进行模块的导入。
[总结一下]
require
: node 和 es6 都支持的引入
export / import
: 只有es6 支持的导出引入
module.exports / exports
: 只有 node 支持的导出
参考:
http://www.javashuo.com/article/p-zizjvavv-t.html
http://www.javashuo.com/article/p-bupuxjan-dk.html
复习Vue-Router
的使用
使用以前记得安装:
npm install vue-router -D
js代码:
import VueRouter from 'vue-router'; //一、建立路由视图组件 var home = {template: '<h3>This is home page!</h3>'}; var about = {template: '<h3>This is about page!</h3>'}; //二、建立路由规则 var routes = [ {path: '/home', component: home}, {path: '/about', component: about} ]; //三、建立一个路由实例 var router = new VueRouter({ routes: routes }); //四、将路由搭载到Vue实例中 new Vue({ el: '#app', router: router });
在webpack
中使用:
须要注意两个区别,咱们须要手动挂载到Vue上,使用use
方法:
Vue.use(VueRouter)//手动挂载路由
还有就是路由的视图组件不能直接这样写,须要使用单文件组件来定义视图组件,不然回报runtime-only...
错误。
lang
属性能够指定样式使用的语言,好比less
,scss
等。
scoped
属性存在时style
的做用域就只在当前组件,不然为全局样式。