Vuex實(shí)現(xiàn)計(jì)數(shù)器以及列表展示效果
本篇教程將以計(jì)數(shù)器及列表展示兩個(gè)例子來講解Vuex的簡單用法。
本案例github
從安裝到啟動(dòng)初始頁面的過程都直接跳過。注意安裝時(shí)選擇需要路由。
首先,src目錄下新建store目錄及相應(yīng)文件,結(jié)構(gòu)如下:
index.js文件內(nèi)容:
import Vue from "vue" import Vuex from 'vuex' Vue.use(Vuex); //務(wù)必在new Vuex.Store之前use一下 export default new Vuex.Store({ state:{ count:0 //計(jì)數(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" } ] })
完成后啟動(dòng),即可看到結(jié)果。計(jì)數(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); //務(wù)必在new Vuex.Store之前use一下 export default new Vuex.Store({ modules:{ cover //注冊cover數(shù)據(jù)模型 }, state:{ count:0 //計(jì)數(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>請嘗試點(diǎn)擊li。</h2> <router-link to="/num">計(jì)數(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') //調(diào)用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,即可看到結(jié)果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中的@blur事件 當(dāng)元素失去焦點(diǎn)時(shí)所觸發(fā)的事件問題
這篇文章主要介紹了Vue中的@blur事件 當(dāng)元素失去焦點(diǎn)時(shí)所觸發(fā)的事件問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vuejs中的watch實(shí)例詳解(監(jiān)聽者)
本文通過實(shí)例代碼給大家介紹了Vuejs中的watch,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-01-01webpack+vuex+axios 跨域請求數(shù)據(jù)的示例代碼
本篇文章主要介紹了webpack+vuex+axios 跨域請求數(shù)據(jù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Vue項(xiàng)目中對index.html中BASE_URL的配置方式
這篇文章主要介紹了Vue項(xiàng)目中對index.html中BASE_URL的配置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06vue多頁面項(xiàng)目中路由使用history模式的方法
這篇文章主要介紹了vue多頁面項(xiàng)目中路由如何使用history模式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09基于vue實(shí)現(xiàn)圖片預(yù)覽功能并顯示在彈窗的最上方
這篇文章主要為大家詳細(xì)介紹了如何基于vue實(shí)現(xiàn)圖片預(yù)覽功能并顯示在彈窗的最上方,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10簡單實(shí)現(xiàn)一個(gè)vue公式編輯器組件demo
這篇文章主要介紹了輕松實(shí)現(xiàn)一個(gè)簡單的vue公式編輯器組件示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01在?Vue?中使用?dhtmlxGantt?組件時(shí)遇到的問題匯總(推薦)
dhtmlxGantt一個(gè)功能豐富的甘特圖插件,支持任務(wù)編輯,資源分配和多種視圖模式,這篇文章主要介紹了在?Vue?中使用?dhtmlxGantt?組件時(shí)遇到的問題匯總,需要的朋友可以參考下2023-03-03Vue使用mixins實(shí)現(xiàn)壓縮圖片代碼
本篇文章主要介紹了Vue使用mixins實(shí)現(xiàn)壓縮圖片代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03