cnpm install vue-router -S
//引入VueRouter import VueRouter from 'vue-router' //使用VueRouter Vue.use(VueRouter); import routerConfig from './router.config.js' //建立路由实例 const router=new VueRouter(routerConfig);
export default { routes:[ { path:'/home', component:Home }, { path:'/news', component:News } ] }
cnpm install axios -S 使用axios的两种方式: 方式1:在每一个组件中引入axios 方式2:在main.js中全局引入axios并添加到Vue原型中
//为自定义组件加修饰符:native <MyButton @click.native="send"></MyButton>
模块化开发css
Element UI是饿了么团队提供的一套基于Vue2.0的组件库,能够快速搭建网站,提升开发效率 ElementUI PC端 MintUI 移动端
官网html
cnpm install element-ui -S
import ElementUI from 'element-ui' import 'element-ui/lib/theme-default/index.css' //该样式文件须要单独引入 Vue.use(ElementUI); 这种方式引入了ElementUI中全部的组件
CSS样式和字体图标都须要由相应的loader来加载,因此须要style-loader、css-loader 默认并无style-loader模块,因此须要单独安装 cnpm install style-loader --save-dev
安装loader,须要两个:less、less-loader cnpm install less less-loader -D 在webpack.config.js中添加loader
cnpm install babel-plugin-component -D
"plugins": [["component", [ { "libraryName": "element-ui", "styleLibraryName": "theme-default" } ]]]
全局组件(插件):就是指能够在main.js中使用Vue.use()进行全局引入,而后在其余组件中就均可以使用了,如vue-router import VueRouter from 'vue-router' Vue.use(VueRouter); 普通组件(插件):每次使用时都要引入,如axios import axios from 'axios'
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的全部组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。 简单来讲,用来集中管理数据,相似于React中的Redux,都是基于Flux的前端状态管理框架
cnpm install vuex -S
import store from './store.js' //导入store对象 new Vue({ store, //配置store选项,指定为store对象,会自动将store对象注入到全部子组件中,在子组件中经过this.$store访问该store对象 el: '#app', render: h => h(App) })
Vuex的核心是Store(仓库),至关因而一个容器,一个store实例中包含如下属性的方法: state 定义属性(状态、数据) getters 用来获取属性 actions 定义方法(动做) commit 提交变化,修改数据的惟一方式就是显式的提交mutations mutations 定义变化 注:不能直接修改数据,必须显式提交变化,目的是为了追踪到状态的变化
/** * vuex配置 */ import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); //定义属性(数据) var state={ count:6 } //定义getters var getters={ count(state){ return state.count; }, isEvenOrOdd(state){ return state.count%2==0?'偶数':'奇数'; } } //定义actions,要执行的操做,如流程判断、异步请求等 const actions = { increment(context){//包含:commit、dispatch、state console.log(context); // context.commmit() }, // increment({commit,state}){ // commit('increment'); //提交一个名为increment的变化,名称可自定义,能够认为是类型名 // }, decrement({commit,state}){ if(state.count>10){ commit('decrement'); } }, incrementAsync({commit,state}){ //异步操做 var p=new Promise((resolve,reject) => { setTimeout(() => { resolve(); },3000); }); p.then(() => { commit('increment'); }).catch(() => { console.log('异步操做'); }); } } //定义mutations,处理状态(数据)的改变 const mutations={ increment(state){ state.count++; }, decrement(state){ state.count--; } } //建立store对象 const store=new Vuex.Store({ state, getters, actions, mutations }) //导出store对象 export default store;
在子组件中访问store对象的两种方式: 方式1:经过this.$store访问 方式2:经过mapState、mapGetters、mapActions访问,vuex提供了两个方法: mapState 获取state mapGetters 获取getters mapActions 获取actions
|-src |-store |-index.js |-getters.js |-actions.js |-mutations.js |-modules //分为多个模块,每一个模块均可以拥有本身的state、getters、actions、mutations |-user.js |-cart.js |-goods.js |....
参考Vue教学视频:Vue.js 2.0之全家桶系列视频课程(vue、vue-router、axios、vuex)vue