午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

Vue局部組件數據共享Vue.observable()的使用

瀏覽:142日期:2022-09-28 14:55:32

隨著組件的細化,就會遇到多組件狀態共享的情況, Vuex當然可以解決這類問題,不過就像 Vuex官方文檔所說的,如果應用不夠大,為避免代碼繁瑣冗余,最好不要使用它,今天我們介紹的是 vue.js 2.6 新增加的 Observable API ,通過使用這個 api 我們可以應對一些簡單的跨組件數據狀態共享的情況。

創建store對象

首先創建一個 store.js,包含一個 store和一個 mutations,分別用來指向數據和處理方法。

//store.jsimport 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對象應用在不同組件中

然后再在組件中使用該對象

//obserVable.vue<template> <div> <h1>跨組件數據狀態共享 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)'>當前a組件中+1</button> <button @click='setCount(count - 1)'>當前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)'>當前b組件中+1</button> <button @click='setCount(count - 1)'>當前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>

顯示效果

Vue局部組件數據共享Vue.observable()的使用

到此這篇關于Vue局部組件數據共享Vue.observable()的使用的文章就介紹到這了,更多相關Vue.observable() 數據共享內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
主站蜘蛛池模板: 久久99深爱久久99精品 | 亚洲 欧美 中文字幕 | 国产不卡一区 | 欧美性猛交xxxx黑人交 | 天天干天天色综合 | 91无套直看片红桃在线观看 | 污污视频在线观看免费 | 在线免费观看黄网站 | 日批网站在线观看 | 婷婷啪啪| 中文字幕日韩专区 | 成人在线免费看片 | 午夜久久影院 | 成年人免费在线观看视频网站 | 欧美一区免费看 | 在线播放亚洲 | 白浆av| 久久人体视频 | 夜夜操影院 | 成人一级网站 | 国产成人三级在线观看 | 黄网站在线播放 | 国产传媒一区二区三区 | av亚洲一区 | 亚洲成熟少妇视频在线观看 | 奇米影音先锋 | 五月婷婷导航 | 97av在线 | 国产精品一二三在线观看 | 伊人一区二区三区 | 久久久久久久福利 | 在线视频99 | 日本一区二区在线 | 老牛影视av一区二区在线观看 | 乱一色一乱一性一视频 | 天堂av中文在线 | 亚洲一区在线视频观看 | аⅴ资源新版在线天堂 | 亚洲一区二区美女 | 欧美福利小视频 | 丁香婷婷色 |