Vue局部組件數(shù)據(jù)共享Vue.observable()的使用
隨著組件的細(xì)化,就會(huì)遇到多組件狀態(tài)共享的情況, Vuex當(dāng)然可以解決這類問(wèn)題,不過(guò)就像 Vuex官方文檔所說(shuō)的,如果應(yīng)用不夠大,為避免代碼繁瑣冗余,最好不要使用它,今天我們介紹的是 vue.js 2.6 新增加的 Observable API ,通過(guò)使用這個(gè) api 我們可以應(yīng)對(duì)一些簡(jiǎn)單的跨組件數(shù)據(jù)狀態(tài)共享的情況。
創(chuàng)建store對(duì)象
首先創(chuàng)建一個(gè) store.js,包含一個(gè) store和一個(gè) mutations,分別用來(lái)指向數(shù)據(jù)和處理方法。
//store.js
import Vue from 'vue';
export let store =Vue.observable({count:0,name:'李四'});
export let mutations={
setCount(count){
store.count=count;
},
changeName(name){
store.name=name;
}
}
把store對(duì)象應(yīng)用在不同組件中
然后再在組件中使用該對(duì)象
//obserVable.vue
<template>
<div>
<h1>跨組件數(shù)據(jù)狀態(tài)共享 obserVable</h1>
<div>
<top></top>
<bottom></bottom>
</div>
</div>
</template>
<script>
import top from './components/top.vue';
import bottom from './components/bottom.vue';
export default {
name: 'obserVable',
components: {
top,
bottom
}
};
</script>
<style scoped>
</style>
//組件a
<template>
<div class="bk">
<span
><h1>a組件</h1>
{{ count }}--{{ name }}</span
>
<button @click="setCount(count + 1)">當(dāng)前a組件中+1</button>
<button @click="setCount(count - 1)">當(dāng)前a組件中-1</button>
</div>
</template>
<script>
import { store, mutations } from '@/store';
export default {
computed: {
count() {
return store.count;
},
name() {
return store.name;
}
},
methods: {
setCount: mutations.setCount,
changeName: mutations.changeName
}
};
</script>
<style scoped>
.bk {
background: lightpink;
}
</style>
//組件b
<template>
<div class="bk">
<h1>b組件</h1>
{{ count }}--{{ name }}
<button @click="setCount(count + 1)">當(dāng)前b組件中+1</button>
<button @click="setCount(count - 1)">當(dāng)前b組件中-1</button>
</div>
</template>
<script>
import { store, mutations } from '@/store';
export default {
computed: {
count() {
return store.count;
},
name() {
return store.name;
}
},
methods: {
setCount: mutations.setCount,
changeName: mutations.changeName
}
};
</script>
<style scoped>
.bk {
background: lightgreen;
}
</style>
顯示效果

到此這篇關(guān)于Vue局部組件數(shù)據(jù)共享Vue.observable()的使用的文章就介紹到這了,更多相關(guān)Vue.observable() 數(shù)據(jù)共享內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)將數(shù)據(jù)存入vuex中以及從vuex中取出數(shù)據(jù)
今天小編就為大家分享一篇vue實(shí)現(xiàn)將數(shù)據(jù)存入vuex中以及從vuex中取出數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
axios向后臺(tái)傳遞數(shù)組作為參數(shù)的方法
今天小編就為大家分享一篇axios向后臺(tái)傳遞數(shù)組作為參數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue+element-ui監(jiān)聽(tīng)滾動(dòng)實(shí)現(xiàn)錨點(diǎn)定位方式(雙向),錨點(diǎn)問(wèn)題
這篇文章主要介紹了vue+element-ui監(jiān)聽(tīng)滾動(dòng)實(shí)現(xiàn)錨點(diǎn)定位方式(雙向),錨點(diǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue動(dòng)態(tài)權(quán)限登錄實(shí)現(xiàn)(基于路由與角色)
很多應(yīng)用都會(huì)需要對(duì)不同的用戶進(jìn)行權(quán)限控制,本文主要介紹了Vue動(dòng)態(tài)權(quán)限登錄實(shí)現(xiàn)(基于路由與角色),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
vue2?element-ui?el-checkbox視圖不更新問(wèn)題及解決
作者在開(kāi)發(fā)過(guò)程中遇到了視圖不更新的問(wèn)題,最終通過(guò)改變一個(gè)無(wú)關(guān)緊要的響應(yīng)式數(shù)據(jù)來(lái)解決,讓視圖發(fā)生改變2024-12-12
vue3?TS?vite?element?ali-oss使用教程示例
這篇文章主要為大家介紹了vue3?TS?vite?element?ali-oss使用教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
vue實(shí)現(xiàn)監(jiān)控視頻直播的示例代碼
本文主要介紹了vue實(shí)現(xiàn)監(jiān)控視頻直播的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
uniapp+vue3路由跳轉(zhuǎn)傳參的實(shí)現(xiàn)
本文主要介紹了uniapp+vue3路由跳轉(zhuǎn)傳參的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
vue-cli腳手架build目錄下utils.js工具配置文件詳解
這篇文章主要介紹了vue-cli腳手架build目錄下utils.js工具配置文件詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

