Vuex實現(xiàn)計數(shù)器以及列表展示效果
本篇教程將以計數(shù)器及列表展示兩個例子來講解Vuex的簡單用法。
本案例github
從安裝到啟動初始頁面的過程都直接跳過。注意安裝時選擇需要路由。
首先,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 //計數(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"
}
]
})
完成后啟動,即可看到結(jié)果。計數(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 //計數(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') //調(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事件 當元素失去焦點時所觸發(fā)的事件問題
這篇文章主要介紹了Vue中的@blur事件 當元素失去焦點時所觸發(fā)的事件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
webpack+vuex+axios 跨域請求數(shù)據(jù)的示例代碼
本篇文章主要介紹了webpack+vuex+axios 跨域請求數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Vue項目中對index.html中BASE_URL的配置方式
這篇文章主要介紹了Vue項目中對index.html中BASE_URL的配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
基于vue實現(xiàn)圖片預(yù)覽功能并顯示在彈窗的最上方
這篇文章主要為大家詳細介紹了如何基于vue實現(xiàn)圖片預(yù)覽功能并顯示在彈窗的最上方,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10
在?Vue?中使用?dhtmlxGantt?組件時遇到的問題匯總(推薦)
dhtmlxGantt一個功能豐富的甘特圖插件,支持任務(wù)編輯,資源分配和多種視圖模式,這篇文章主要介紹了在?Vue?中使用?dhtmlxGantt?組件時遇到的問題匯總,需要的朋友可以參考下2023-03-03

