git代码:https://github.com/chentianwei411/vue-router-demo.gitcss
使用Vue-CLI3html
webpack, ESLInt,vue
Vuex,node
vue-routerjquery
axios (用于异步发出请求)webpack
vee-validate (用于表格验证)ios
bootstrap-vuegit
vue-toastr-2 (用于添加通知) (须要yarn add jquery, 须要yarn add toastr )github
须要安装node到最新版本,而后使用npm安装vue-cli。web
npm install -g vue-cli
使用vue ui用户界面建立一个app,并选择使用的插件。
(具体使用见我以前的博文)https://www.cnblogs.com/chentianwei/p/10116155.html
//2个基本组件 <router-view></router-view> <router-link to="/">Home</router-link> // store.js中的routes export default new Router({ routes: [ { path: '/', name: 'Home', component: Home }, { path: '/admin', //由于子路径有默认的,因此无需 name: 'Admin', component: Index, // children children: [ { path: 'new', name: 'New', component: New },
还有嵌套路由,及嵌套命名路由,动态路由。
过渡<transition>包裹<router-view>。
使用VeeValidate
便可。 见博文:https://www.cnblogs.com/chentianwei/p/10128205.html
看productForm.vue:
代码摘录:
//template: <form @submit.prevent="saveProduct"> <div class="row"> <div class="col-lg-7 col-md-5 col-sm-12 col-xs-12"> <div class="form-group"> <label>Name</label> <input type="text" placeholder="Name" v-model="model.name" v-validate="'required'" name="name" :class="{'form-control': true, 'error': errors.has('name') }" /> <span class="small text-danger" v-show="errors.has('name')">Name is required</span> </div>
<script> import { ERROR_MSG } from '../../store/mutations-types.js' export default { // isEditing prop用于显示提交按钮上的名字 props: ['model', 'isEditing', 'manufacturers'], methods: { saveProduct() {// 这是插件的方法,会返回一个Promise: this.$validator.validateAll().then(() => { // emit一个event给父组件 this.$emit('save-product', this.model) }).catch(() => { // 提交一个错误到Vuex.Store this.$store.commit(ERROR_MSG, { type: 'error', title: 'Validation!', content: 'Please ensure the form is valid' }) }) } } } </script>
1.store.js文件,state的状态管理根文件。
import { productGetters, manufacturerGetters } from './getters.js' //...略... export default new Vuex.Store({ strict: true, state: {//略... }, // 把传入的对象合并:Object.assign() getters: Object.assign({}, productGetters, manufacturerGetters),
2.getters.js
要使用的计算属性。
3. mutations-types.js
// When creating the mutation and while committing to the mutation. // This creates a layer of possible typographical error. // You can use constants to replace these types // and allow tools like IntelliSense to help you avoid typos and errors: // 使用常量,来防止语法错误。
4. actions
import axios from 'axios' //... // 这里定义的actions都是由各个组件来激发的 // 例如: allProducts,它在Products.vue中被激发 // 提交ALL_PRODUCTS mutation,它会开始一个loading spinner即记录此时是加载中。 // 以后使用axios发出一个HTML request,这是异步函数。 // 收到response后,提交一个带有返回data做为参数的success mutation。 // 这个mutiation会修改state的products,并改showLoader为false,表明结束加载。 export const productActions = { // Prodcuts.vue, PrdouctList.vue中被使用。(created hook内) allProducts ({commit}) { commit(ALL_PRODUCTS) axios.get(`${API_BASE}/products`).then(response => { commit(ALL_PRODUCTS_SUCCESS, response.data) }) },
5. mutations.js
export const productMutations = { // 每一个mutation都有一个success mutation, 它会在mutation完成后调用! // mutation只会抛出一个等待的pending state, 能够用于过渡的css // 而后success mutation更新UI,并关闭pending state。 // showLoader 用于配合vue-toastr插件显示通知信息。 [ALL_PRODUCTS] (state) { state.showLoader = true }, [ALL_PRODUCTS_SUCCESS] (state, payload) { state.showLoader = false state.products = payload },
见博文 Vue.js简单的状态管理和 Vuex的几个核心概念使用。
因为状态零散地分布在许多组件和组件之间的交互中,大型应用复杂度也常常逐渐增加。
参考以前的博客:嵌套的JavaScript评论 Widget Models (有一个替代的vue插件)
当在严格模式中使用 Vuex 时,在属于 Vuex 的 state 上使用 v-model
会比较棘手。
这是由于严格模式下,state的任何变化都须要经过mutation来改变。而v-model直接绑定data,因此会报错❌。
2种办法:
//template:
<input v-model="message">
//script
computed: { message: { get () { return this.$store.state.obj.message }, set (value) { this.$store.commit('updateMessage', value) } } }
包括
在/views/home.vue中
import ProductList from '../components/products/ProductList.vue'
编写ProductList.vue
编写product-item组件, 它用在ProductList.vue内。
编写product-button组件,它用在ProductItem.vue内
建立一个产品详细描述的组件。Details.vue。它内部调用ProductDetails.vue
须要增长一个动态路径,来导航到产品详情页面。
//在routers.js的routes:内添加一个新的路径 { path: '/details/:id', name: 'Details', component: Details }
由于cart.vue中也要用到ProductDetails.vue.