如何理解Vue簡(jiǎn)單狀態(tài)管理之store模式
概述
store 狀態(tài)管理模式的實(shí)現(xiàn)思想很簡(jiǎn)單,就是定義一個(gè) store 對(duì)象,對(duì)象里有 state 屬性存儲(chǔ)共享數(shù)據(jù),對(duì)象里還存儲(chǔ)操作這些共享數(shù)據(jù)的方法。在組件中將 store.state 共享數(shù)據(jù)作為 data 的一部分或全部,在對(duì) store.state 對(duì)象里的共享數(shù)據(jù)進(jìn)行改變時(shí),必須調(diào)用 store 提供的接口進(jìn)行共享數(shù)據(jù)的更改。
以下以一個(gè)簡(jiǎn)單 todo-list demo 來(lái)介紹 store 狀態(tài)管理模式
1. 定義 store.js
//store.js
export const store = {
state: {
todos: [
{text: '寫語(yǔ)文作業(yè)', done: false},
{text: '做數(shù)學(xué)卷子', done: false}
]
},
addTodo(str){
const obj = {text: str, done: false}
this.state.todos.push(obj)
},
setDone(index){
this.state.todos[index].done = true
}
}
2. 組件使用 store.js
//A.vue
<template>
<div class="A">
我是 A組件
<ul>
<li v-for="(todo,index) in todos"
:key="index" :class="todo.done?'done':''" @click="setDone(index)">
{{todo.text}}
</li>
</ul>
</div>
</template>
<script>
import {store} from '../store/store.js'
export default {
name: 'A',
data(){
return store.state
},
methods: {
setDone(index){
store.setDone(index)
}
}
}
</script>
<style scoped>
.A{
background: red;
color: white;
padding: 20px;
}
.A li.done{
background: green;
}
</style>
//B.vue
<template>
<div class="B">
<div>
我是 B 組件,在下方輸入框輸入任務(wù)在 A組件 中添加任務(wù)
</div>
<input type="text" v-model="text">
<button @click="addTodo">add todo</button>
</div>
</template>
<script>
import {store} from '../store/store.js'
export default {
name: 'B',
data(){
return {
text: ''
}
},
methods:{
addTodo(){
if(this.text){
store.addTodo(this.text)
}
}
}
}
</script>
<style scoped>
.B{
background: yellow;
padding: 20px;
}
</style>
//App.vue
<template>
<div id="app">
<A />
<B />
</div>
</template>
<script>
import A from './components/A.vue'
import B from './components/B.vue'
export default {
name: 'App',
components: {
A,
B
}
}
</script>
3. 實(shí)現(xiàn)效果

可以看到,在 A組件 中顯示的數(shù)據(jù),在 B組件 中進(jìn)行添加和修改,就是通過數(shù)據(jù)共享的方式進(jìn)行數(shù)據(jù)通信,簡(jiǎn)單的 store模式 就是這樣的運(yùn)用方式。
以上就是如何理解Vue簡(jiǎn)單狀態(tài)管理之store模式的詳細(xì)內(nèi)容,更多關(guān)于Vue簡(jiǎn)單狀態(tài)管理之store模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在Vue3中使用localStorage保存數(shù)據(jù)的流程步驟
在前端開發(fā)中,尤其是利用Vue3構(gòu)建現(xiàn)代Web應(yīng)用時(shí),掌握如何使用本地存儲(chǔ)(localStorage)來(lái)保存數(shù)據(jù)是非常重要的能力,在這篇博客中,我將詳細(xì)介紹如何在Vue3中使用localStorage保存數(shù)據(jù),并提供示例代碼來(lái)幫助理解,需要的朋友可以參考下2024-06-06
vue.js 初體驗(yàn)之Chrome 插件開發(fā)實(shí)錄
這篇文章主要介紹了vue.js 初體驗(yàn)之Chrome 插件開發(fā)實(shí)錄 ,需要的朋友可以參考下2017-05-05
elementui使用el-upload組件實(shí)現(xiàn)自定義上傳的詳細(xì)步驟
upload上傳是前端開發(fā)很常用的一個(gè)功能,在Vue開發(fā)中常用的Element組件庫(kù)也提供了非常好用的upload組件,這篇文章主要給大家介紹了關(guān)于elementui使用el-upload組件實(shí)現(xiàn)自定義上傳的詳細(xì)步驟,需要的朋友可以參考下2023-12-12
vue表單驗(yàn)證rules及validator驗(yàn)證器的使用方法實(shí)例
在vue開發(fā)中,難免遇到各種表單校驗(yàn),下面這篇文章主要給大家介紹了關(guān)于vue表單驗(yàn)證rules及validator驗(yàn)證器使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
vue+node實(shí)現(xiàn)圖片上傳及預(yù)覽的示例方法
這篇文章主要介紹了vue+node實(shí)現(xiàn)圖片上傳及預(yù)覽的示例方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-11-11
Vuejs第九篇之組件作用域及props數(shù)據(jù)傳遞實(shí)例詳解
這篇文章主要介紹了Vuejs第九篇之組件作用域及props數(shù)據(jù)傳遞實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09

