国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vuex實現(xiàn)計數(shù)器以及列表展示效果

2019-11-19 14:11:52
字體:
來源:轉載
供稿:網(wǎng)友

 本篇教程將以計數(shù)器及列表展示兩個例子來講解Vuex的簡單用法。

本案例github

從安裝到啟動初始頁面的過程都直接跳過。注意安裝時選擇需要路由。

首先,src目錄下新建store目錄及相應文件,結構如下:

index.js文件內(nèi)容:

import Vue from "vue"import Vuex from 'vuex'Vue.use(Vuex);  //務必在new Vuex.Store之前use一下export default new Vuex.Store({ state:{  count:0    //計數(shù)器的count }, mutations:{  increment(state){   state.count++  } }})

src下的main.js里注冊store

new Vue({ el: '#app', router, store,    //注冊store components: { App }, template: '<App/>'});

components文件夾內(nèi)新建Num.vue組件,內(nèi)容如下

<template> <div>  <input type="button" value="+" @click="incr" />  <input type="text" id="input" v-model="count"/>  <input type="button" value="-"/>  <br/>  <router-link to="/list">列表demo</router-link> </div></template><script> import store from '../store' export default {  computed:{   count:{    get:function () {     return store.state.count    },    set:function (val) {     store.state.count = val    }   }  },  methods:{   incr(){    // store.commit("increment")    store.commit("increment")  //觸發(fā)修改   }  } }</script><!-- Add "scoped" attribute to limit CSS to this component only --><style scoped></style>

router文件夾內(nèi)配置路由:

import Vue from 'vue'import Router from 'vue-router'import Num from '../components/Num'import List from '../components/List'Vue.use(Router)export default new Router({ routes: [  {   path:'/num',   component:Num  },  {   path:"*",   redirect:"/num"  } ]})

完成后啟動,即可看到結果。計數(shù)器演示完成。

現(xiàn)在開始列表演示。

src目錄下新建api文件夾,再新建api文件。

api/cover.js:

const _cover = [ {"id": 1, "title": "iPad 4 Mini", "price": 500.01, "inventory": 2}, {"id": 2, "title": "H&M T-Shirt White", "price": 10.99, "inventory": 10}, {"id": 3, "title": "Charli XCX - Sucker CD", "price": 19.99, "inventory": 5}];export default { getCover(cb) {  setTimeout(() => cb(_cover), 100);/*  $.get("/api/data",function (data) {   console.log(data);  })*/ },}

修改store/modules/cover.js:(定義數(shù)據(jù)模型)

import cover from '../../api/cover'const state = { all:[]};const getters={ allCover:state=>state.all  //getter用來提供訪問接口};const actions = { getAllCover({commit}){  cover.getCover(covers=>{   commit('setCover',covers)    //觸發(fā)setCover修改。  }) }, removeCover({commit},cover){  commit('removeCover',cover) }};const mutations = {  //mutations用來修改state。 setCover(state,covers){  state.all = covers }, removeCover(state,cover){  console.log(cover.id);  state.all = state.all.filter(function (OCover) {   return OCover.id !== cover.id  }) }};export default { state,getters,actions,mutations}

store內(nèi)的index.js中注冊數(shù)據(jù)模型:

import Vue from "vue"import Vuex from 'vuex'import cover from './modules/cover'Vue.use(Vuex);  //務必在new Vuex.Store之前use一下export default new Vuex.Store({ modules:{  cover     //注冊cover數(shù)據(jù)模型 }, state:{  count:0    //計數(shù)器的count }, mutations:{  increment(state){   state.count++  } }})

components文件夾內(nèi)新建List.vue組件,內(nèi)容如下:

<template> <div class="list">  <ul>   <li v-for="cover in covers" @click="removeCover(cover)">    {{cover.title}}   </li>  </ul>  <p>   {{covers}}  </p>  <h2>請嘗試點擊li。</h2>  <router-link to="/num">計數(shù)器demo</router-link> </div></template><script>import {mapGetters,mapActions} from 'vuex';export default { computed:mapGetters({  covers:"allCover"   //利用module的getter獲得數(shù)據(jù) }), methods:mapActions([  'removeCover'    //利用module的action刪除數(shù)據(jù) ]), created(){  this.$store.dispatch('getAllCover')  //調用cover數(shù)據(jù)模型的getAllCover action 用來初始化列表數(shù)據(jù)。 }}</script><!-- Add "scoped" attribute to limit CSS to this component only --><style scoped> .list{  text-align: left; }</style>

路由中注冊新組件:

import Vue from 'vue'import Router from 'vue-router'import Num from '../components/Num'import List from '../components/List'Vue.use(Router)export default new Router({ routes: [  {   path:'/num',   component:Num  },  {   path:'/list',   component:List  },  {   path:"*",   redirect:"/num"  } ]})

完成后訪問http://localhost:8080/#/list,即可看到結果。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌审旗| 攀枝花市| 高邮市| 盐源县| 平乐县| 峡江县| 师宗县| 巢湖市| 黄大仙区| 平顶山市| 金平| 瑞丽市| 会东县| 泰州市| 锡林浩特市| 乌什县| 资阳市| 巨野县| 繁峙县| 文登市| 永年县| 平舆县| 新宁县| 名山县| 曲水县| 肇庆市| 蓬莱市| 汝南县| 平度市| 普陀区| 九台市| 平湖市| 武邑县| 江孜县| 宜春市| 铜川市| 娱乐| 安国市| 二手房| 东乌| 友谊县|