SpringBoot 處理異常的幾種常見姿式

                                                              歡迎你們關注微信公衆號vue

vue是數據驅動視圖更新的框架, 因此對於vue來講組件間的數據通訊很是重要,那麼組件之間如何進行數據通訊的呢?程序員

首先咱們須要知道在vue中組件之間存在什麼樣的關係, 才更容易理解他們的通訊方式, 就好像過年回家,坐着一屋子的陌生人,相互之間怎麼稱呼,這時就須要先知道本身和他們之間是什麼樣的關係。vuex

vue組件中關係說明:api

如上圖所示, A與B、A與C、B與D、C與E組件之間是父子關係; B與C之間是兄弟關係;A與D、A與E之間是隔代關係; D與E是堂兄關係(非直系親屬)數組

針對以上關係咱們歸類爲:微信

  •  父子組件之間通訊
  •  非父子組件之間通訊(兄弟組件、隔代關係組件等)

本文會介紹組件間通訊的8種方式以下圖目錄所示:並介紹在不一樣的場景下如何選擇有效方式實現的組件間通訊方式,但願能夠幫助小夥伴們更好理解組件間的通訊。session

1、props / $emitapp

父組件經過props的方式向子組件傳遞數據,而經過$emit 子組件能夠向父組件通訊。框架

1. 父組件向子組件傳值異步

下面經過一個例子說明父組件如何向子組件傳遞數據:在子組件article.vue中如何獲取父組件section.vue中的數據articles:['紅樓夢', '西遊記','三國演義']

 
  1. // section父組件  
  2. <template>  
  3.   <div class="section">  
  4.     <com-article :articles="articleList"></com-article>  
  5.   </div>  
  6. </template>  
  7. <script>  
  8. import comArticle from './test/article.vue'  
  9. export default {  
  10.   name: 'HelloWorld',  
  11.   components: { comArticle },  
  12.   data() {  
  13.     return {  
  14.       articleList: ['紅樓夢', '西遊記', '三國演義']  
  15.     }  
  16.   }  
  17. }  
  18. </script>  
 
  1. // 子組件 article.vue  
  2. <template>  
  3.   <div>  
  4.     <span v-for="(item, index) in articles" :key="index">{{item}}</span>  
  5.   </div>  
  6. </template>  
  7. <script>  
  8. export default {  
  9.   props: ['articles']  
  10. }  
  11. </script> 

總結: prop 只能夠從上一級組件傳遞到下一級組件(父子組件),即所謂的單向數據流。並且 prop 只讀,不可被修改,全部修改都會失效並警告。

2. 子組件向父組件傳值

對於$emit 我本身的理解是這樣的: $emit綁定一個自定義事件, 當這個語句被執行時, 就會將參數arg傳遞給父組件,父組件經過v-on監聽並接收參數。 經過一個例子,說明子組件如何向父組件傳遞數據。

在上個例子的基礎上, 點擊頁面渲染出來的ariticle的item, 父組件中顯示在數組中的下標

 
  1. // 父組件中  
  2. <template>  
  3.   <div class="section">  
  4.     <com-article :articles="articleList" @onEmitIndex="onEmitIndex"></com-article>  
  5.     <p>{{currentIndex}}</p>  
  6.   </div>  
  7. </template>  
  8. <script>  
  9. import comArticle from './test/article.vue'  
  10. export default {  
  11.   name: 'HelloWorld',  
  12.   components: { comArticle },  
  13.   data() {  
  14.     return {  
  15.       currentIndex: -1,  
  16.       articleList: ['紅樓夢', '西遊記', '三國演義']  
  17.     }  
  18.   },  
  19.   methods: {  
  20.     onEmitIndex(idx) {  
  21.       this.currentIndex = idx  
  22.     }  
  23.   }  
  24. }  
  25. </script>  
 
  1. <template>  
  2.   <div>  
  3.     <div v-for="(item, index) in articles" :key="index" @click="emitIndex(index)">{{item}}</div>  
  4.   </div>  
  5. </template>  
  6. <script>  
  7. export default {  
  8.   props: ['articles'],  
  9.   methods: {  
  10.     emitIndex(index) {  
  11.       this.$emit('onEmitIndex', index)  
  12.     }  
  13.   }  
  14. }  
  15. </script> 

2、 $children / $parent

上面這張圖片是vue官方的解釋,經過$parent和$children就能夠訪問組件的實例,拿到實例表明什麼?表明能夠訪問此組件的全部方法和data。接下來就是怎麼實現拿到指定組件的實例。

使用方法

 
  1. // 父組件中  
  2. <template>  
  3.   <div class="hello_world">  
  4.     <div>{{msg}}</div>  
  5.     <com-a></com-a>  
  6.     <button @click="changeA">點擊改變子組件值</button>  
  7.   </div>  
  8. </template>  
  9. <script>  
  10. import ComA from './test/comA.vue'  
  11. export default {  
  12.   name: 'HelloWorld',  
  13.   components: { ComA },  
  14.   data() {  
  15.     return {  
  16.       msg: 'Welcome'  
  17.     }  
  18.   },  
  19.   methods: {  
  20.     changeA() {  
  21.       // 獲取到子組件A  
  22.       this.$children[0].messageA = 'this is new value'  
  23.     }  
  24.   }  
  25. }  
  26. </script>  
 
  1. // 子組件中  
  2. <template>  
  3.   <div class="com_a">  
  4.     <span>{{messageA}}</span>  
  5.     <p>獲取父組件的值爲:  {{parentVal}}</p>  
  6.   </div>  
  7. </template>  
  8. <script>  
  9. export default {  
  10.   data() {  
  11.     return {  
  12.       messageA: 'this is old'  
  13.     }  
  14.   },  
  15.   computed:{  
  16.     parentVal(){  
  17.       return this.$parent.msg;  
  18.     }  
  19.   }  
  20. }  
  21. </script> 

要注意邊界狀況,如在#app上拿$parent獲得的是new Vue()的實例,在這實例上再拿$parent獲得的是undefined,而在最底層的子組件拿$children是個空數組。也要注意獲得$parent和$children的值不同,$children 的值是數組,而$parent是個對象

總結

上面兩種方式用於父子組件之間的通訊, 而使用props進行父子組件通訊更加廣泛; 兩者皆不能用於非父子組件之間的通訊。

3、provide/ inject

概念:

provide/ inject 是vue2.2.0新增的api, 簡單來講就是父組件中經過provide來提供變量, 而後再子組件中經過inject來注入變量。

注意: 這裏不論子組件嵌套有多深, 只要調用了inject 那麼就能夠注入provide中的數據,而不侷限於只能從當前父組件的props屬性中回去數據

舉例驗證

接下來就用一個例子來驗證上面的描述:

假設有三個組件: A.vue、B.vue、C.vue 其中 C是B的子組件,B是A的子組件

 
  1. // A.vue  
  2. <template>  
  3.   <div>  
  4.     <comB></comB>  
  5.   </div>  
  6. </template>  
  7. <script>  
  8.   import comB from '../components/test/comB.vue'  
  9.   export default {  
  10.     name: "A",  
  11.     provide: {  
  12.       for: "demo"  
  13.     },  
  14.     components:{  
  15.       comB  
  16.     }  
  17.   }  
  18. </script>  
 
  1. // B.vue  
  2. <template>  
  3.   <div>  
  4.     {{demo}}  
  5.     <comC></comC>  
  6.   </div>  
  7. </template>  
  8. <script>  
  9.   import comC from '../components/test/comC.vue'  
  10.   export default {  
  11.     name: "B",  
  12.     inject: ['for'],  
  13.     data() {  
  14.       return {  
  15.         demo: this.for  
  16.       }  
  17.     },  
  18.     components: {  
  19.       comC  
  20.     }  
  21.   }  
  22. </script>  
 
  1. // C.vue  
  2. <template>  
  3.   <div>  
  4.     {{demo}} 
  5.   </div>  
  6. </template>  
  7. <script>  
  8.   export default {  
  9.     name: "C",  
  10.     inject: ['for'],  
  11.     data() {  
  12.       return {  
  13.         demo: this.for  
  14.       }  
  15.     }  
  16.   }  
  17. </script> 

4、ref / refs

ref:若是在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;若是用在子組件上,引用就指向組件實例,能夠經過實例直接調用組件的方法或訪問數據, 咱們看一個ref 來訪問組件的例子:

 
  1. // 子組件 A.vue  
  2. export default {  
  3.   data () {  
  4.     return {  
  5.       name: 'Vue.js'  
  6.     }  
  7.   },  
  8.   methods: {  
  9.     sayHello () {  
  10.       console.log('hello')  
  11.     }  
  12.   }  
  13. }  
 
  1. // 父組件 app.vue  
  2. <template>  
  3.   <component-a ref="comA"></component-a>  
  4. </template>  
  5. <script>  
  6.   export default {  
  7.     mounted () {  
  8.       const comA = this.$refs.comA;  
  9.       console.log(comA.name);  // Vue.js  
  10.       comA.sayHello();  // hello  
  11.     }  
  12.   }  
  13. </script> 

5、eventBus

eventBus 又稱爲事件總線,在vue中可使用它來做爲溝通橋樑的概念, 就像是全部組件共用相同的事件中心,能夠向該中心註冊發送事件或接收事件, 因此組件均可以通知其餘組件。

eventBus也有不方便之處, 當項目較大,就容易形成難以維護的災難

在Vue的項目中怎麼使用eventBus來實現組件之間的數據通訊呢?具體經過下面幾個步驟

1. 初始化

首先須要建立一個事件總線並將其導出, 以便其餘模塊可使用或者監聽它.

 
  1. // event-bus.js  
  2. import Vue from 'vue'  
  3. export const EventBus = new Vue() 

2. 發送事件

假設你有兩個組件: additionNum 和 showNum, 這兩個組件能夠是兄弟組件也能夠是父子組件;這裏咱們以兄弟組件爲例:

 
  1. <template>  
  2.   <div>  
  3.     <show-num-com></show-num-com>  
  4.     <addition-num-com></addition-num-com>  
  5.   </div>  
  6. </template>  
  7. <script>  
  8. import showNumCom from './showNum.vue'  
  9. import additionNumCom from './additionNum.vue'  
  10. export default {  
  11.   components: { showNumCom, additionNumCom }  
  12. }  
  13. </script>  
 
  1. // addtionNum.vue 中發送事件  
  2. <template>  
  3.   <div>  
  4.     <button @click="additionHandle">+加法器</button>      
  5.   </div>  
  6. </template>  
  7. <script>  
  8. import {EventBus} from './event-bus.js'  
  9. console.log(EventBus)  
  10. export default {  
  11.   data(){  
  12.     return{  
  13.       num:1  
  14.     }  
  15.   },  
  16.   methods:{  
  17.     additionHandle(){  
  18.       EventBus.$emit('addition', {  
  19.         num:this.num++  
  20.       })  
  21.     }  
  22.   }  
  23. }  
  24. </script> 

3. 接收事件

 
  1. // showNum.vue 中接收事件  
  2. <template>  
  3.   <div>計算和: {{count}}</div>  
  4. </template>  
  5. <script>  
  6. import { EventBus } from './event-bus.js'  
  7. export default {  
  8.   data() {  
  9.     return {  
  10.       count: 0  
  11.     }  
  12.   },  
  13.   mounted() {  
  14.     EventBus.$on('addition', param => {  
  15.       thisthis.count = this.count + param.num;  
  16.     })  
  17.   }  
  18. }  
  19. </script> 

這樣就實現了在組件addtionNum.vue中點擊相加按鈕, 在showNum.vue中利用傳遞來的 num 展現求和的結果.

4. 移除事件監聽者

若是想移除事件的監聽, 能夠像下面這樣操做:

 
  1. import { eventBus } from 'event-bus.js'  
  2. EventBus.$off('addition', {}) 

6、Vuex

1. Vuex介紹

Vuex 是一個專爲 Vue.js 應用程序開發的狀態管理模式。它採用集中式存儲管理應用的全部組件的狀態,並以相應的規則保證狀態以一種可預測的方式發生變化.

Vuex 解決了多個視圖依賴於同一狀態和來自不一樣視圖的行爲須要變動同一狀態的問題,將開發者的精力聚焦於數據的更新而不是數據在組件之間的傳遞上

2. Vuex各個模塊

  1.  state:用於數據的存儲,是store中的惟一數據源
  2.  getters:如vue中的計算屬性同樣,基於state數據的二次包裝,經常使用於數據的篩選和多個數據的相關性計算
  3.  mutations:相似函數,改變state數據的惟一途徑,且不能用於處理異步事件
  4.  actions:相似於mutation,用於提交mutation來改變狀態,而不直接變動狀態,能夠包含任意異步操做
  5.  modules:相似於命名空間,用於項目中將各個模塊的狀態分開定義和操做,便於維護

3. Vuex實例應用

 
  1. // 父組件  
  2. <template>  
  3.   <div id="app">  
  4.     <ChildA/>  
  5.     <ChildB/>  
  6.   </div>  
  7. </template>  
  8. <script>  
  9.   import ChildA from './components/ChildA' // 導入A組件  
  10.   import ChildB from './components/ChildB' // 導入B組件  
  11.   export default {  
  12.     name: 'App',  
  13.     components: {ChildA, ChildB} // 註冊A、B組件  
  14.   }  
  15. </script>  
 
  1. // 子組件childA  
  2. <template>  
  3.   <div id="childA">  
  4.     <h1>我是A組件</h1>  
  5.     <button @click="transform">點我讓B組件接收到數據</button>  
  6.     <p>由於你點了B,因此個人信息發生了變化:{{BMessage}}</p>  
  7.   </div>  
  8. </template>  
  9. <script>  
  10.   export default {  
  11.     data() {  
  12.       return {  
  13.         AMessage: 'Hello,B組件,我是A組件'  
  14.       }  
  15.     },  
  16.     computed: {  
  17.       BMessage() {  
  18.         // 這裏存儲從store裏獲取的B組件的數據  
  19.         return this.$store.state.BMsg  
  20.       }  
  21.     },  
  22.     methods: {  
  23.       transform() {  
  24.         // 觸發receiveAMsg,將A組件的數據存放到store裏去  
  25.         this.$store.commit('receiveAMsg', {  
  26.           AMsg: this.AMessage  
  27.         })  
  28.       }  
  29.     }  
  30.   }  
  31. </script>  
 
  1. // 子組件 childB  
  2. <template>  
  3.   <div id="childB">  
  4.     <h1>我是B組件</h1>  
  5.     <button @click="transform">點我讓A組件接收到數據</button>  
  6.     <p>由於你點了A,因此個人信息發生了變化:{{AMessage}}</p>  
  7.   </div>  
  8. </template>  
  9. <script>  
  10.   export default {  
  11.     data() {  
  12.       return {  
  13.         BMessage: 'Hello,A組件,我是B組件'  
  14.       }  
  15.     },  
  16.     computed: {  
  17.       AMessage() {  
  18.         // 這裏存儲從store裏獲取的A組件的數據  
  19.         return this.$store.state.AMsg  
  20.       }  
  21.     },  
  22.     methods: {  
  23.       transform() {  
  24.         // 觸發receiveBMsg,將B組件的數據存放到store裏去  
  25.         this.$store.commit('receiveBMsg', {  
  26.           BMsg: this.BMessage  
  27.         })  
  28.       }  
  29.     }  
  30.   }  
  31. </script> 

vuex的store,js

 
  1. import Vue from 'vue'  
  2. import Vuex from 'vuex'  
  3. Vue.use(Vuex)  
  4. const state = {  
  5.   // 初始化A和B組件的數據,等待獲取  
  6.   AMsg: '',  
  7.   BMsg: ''  
  8. }  
  9. const mutations = {  
  10.   receiveAMsg(state, payload) {  
  11.     // 將A組件的數據存放於state  
  12.     state.AMsg = payload.AMsg  
  13.   },  
  14.   receiveBMsg(state, payload) {  
  15.     // 將B組件的數據存放於state  
  16.     state.BMsg = payload.BMsg  
  17.   }  
  18. }  
  19. export default new Vuex.Store({  
  20.   state,  
  21.   mutations  
  22. }) 

7、 localStorage / sessionStorage

這種通訊比較簡單,缺點是數據和狀態比較混亂,不太容易維護。

經過window.localStorage.getItem(key) 獲取數據

經過window.localStorage.setItem(key,value) 存儲數據

注意用JSON.parse() / JSON.stringify() 作數據格式轉換

localStorage / sessionStorage能夠結合vuex, 實現數據的持久保存,同時使用vuex解決數據和狀態混亂問題.

八 $attrs與 $listeners

如今咱們來討論一種狀況, 咱們一開始給出的組件關係圖中A組件與D組件是隔代關係, 那它們以前進行通訊有哪些方式呢?

  1.  使用props綁定來進行一級一級的信息傳遞, 若是D組件中狀態改變須要傳遞數據給A, 使用事件系統一級級往上傳遞
  2.  使用eventBus,這種狀況下仍是比較適合使用, 可是碰到多人合做開發時, 代碼維護性較低, 可讀性也低
  3.  使用Vuex來進行數據管理, 可是若是僅僅是傳遞數據, 而不作中間處理,使用Vuex處理感受有點大材小用了.

在vue2.4中,爲了解決該需求,引入了$attrs 和$listeners , 新增了inheritAttrs 選項。 在版本2.4之前,默認狀況下,父做用域中不做爲 prop 被識別 (且獲取) 的特性綁定 (class 和 style 除外),將會「回退」且做爲普通的HTML特性應用在子組件的根元素上。接下來看一個跨級通訊的例子:

 
  1. // app.vue  
  2. // index.vue  
  3. <template>  
  4.   <div>  
  5.     <child-com1  
  6.       :name="name"  
  7.       :age="age"  
  8.       :gender="gender"  
  9.       :height="height"  
  10.       title="程序員成長指北"  
  11.     ></child-com1>  
  12.   </div>  
  13. </template>  
  14. <script>  
  15. const childCom1 = () => import("./childCom1.vue");  
  16. export default {  
  17.   components: { childCom1 },  
  18.   data() {  
  19.     return {  
  20.       name: "zhang",  
  21.       age: "18",  
  22.       gender: "女",  
  23.       height: "158"  
  24.     };  
  25.   }  
  26. };  
  27. </script>  
 
  1. // childCom1.vue  
  2. <template class="border">  
  3.   <div>  
  4.     <p>name: {{ name}}</p>  
  5.     <p>childCom1的$attrs: {{ $attrs }}</p>  
  6.     <child-com2 v-bind="$attrs"></child-com2>  
  7.   </div>  
  8. </template>  
  9. <script>  
  10. const childCom2 = () => import("./childCom2.vue");  
  11. export default {  
  12.   components: {  
  13.     childCom2  
  14.   },  
  15.   inheritAttrs: false, // 能夠關閉自動掛載到組件根元素上的沒有在props聲明的屬性  
  16.   props: {  
  17.     name: String // name做爲props屬性綁定  
  18.   },  
  19.   created() {  
  20.     console.log(this.$attrs);  
  21.      // { "age": "18", "gender": "女", "height": "158", "title": "程序員成長指北" }  
  22.   }  
  23. };  
  24. </script>  
 
  1. // childCom2.vue  
  2. <template>  
  3.   <div class="border">  
  4.     <p>age: {{ age}}</p>  
  5.     <p>childCom2: {{ $attrs }}</p>  
  6.   </div>  
  7. </template>  
  8. <script>  
  9. export default {  
  10.   inheritAttrs: false,  
  11.   props: {  
  12.     age: String  
  13.   },  
  14.   created() {  
  15.     console.log(this.$attrs);   
  16.     // { "gender": "女", "height": "158", "title": "程序員成長指北" }  
  17.   }  
  18. };  
  19. </script> 

總結

常見使用場景能夠分爲三類:

  •  父子組件通訊: props; $parent / $children; provide / inject ; ref ; $attrs / $listeners
  •  兄弟組件通訊: eventBus ; vuex
  •  跨級通訊: eventBus;Vuex;provide / inject 、$attrs / $listeners
相關文章
相關標籤/搜索