av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

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

創(chuàng)建store對象

首先創(chuàng)建一個 store.js,包含一個 store和一個 mutations,分別用來指向數(shù)據(jù)和處理方法。

//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對象應(yīng)用在不同組件中

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

//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>

顯示效果

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

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

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 手机在线观看 | 在线中文字幕亚洲 | 久久午夜精品 | 欧美vide| 人人操日日干 | 国产精品国产a | 国产精品久久久久久久久免费 | 精品久久久久久一区二区 | 99精品一区二区三区 | 另类专区亚洲 | 在线观看三级av | av黄色在线| 久久中文免费视频 | 午夜爽爽爽男女免费观看影院 | 国产精品视频观看 | 中文字幕国产 | 精品一二三 | 欧美一区二区三区在线 | 看av网址| 亚洲精品一区二三区不卡 | 热久色 | va精品| 黄色片a级 | 欧美日韩一 | 国产91精品在线 | 亚洲日产精品 | 午夜一区二区三区视频 | 超碰av免费| 精品在线一区二区三区 | 在线观看av网站永久 | 久久69精品久久久久久久电影好 | 羞羞视频在线观看网站 | 三级成人片 | 成人免费视频 | 欧美黑人一级爽快片淫片高清 | 蜜桃精品视频在线 | 一区二区欧美在线 | 日韩精品 电影一区 亚洲 | 亚洲综合久久精品 | 国产九九精品视频 | 精品一区二区三区中文字幕 |