自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Vue高版本中一些新特性的使用

開發(fā) 前端
在項(xiàng)目開發(fā)中,如果業(yè)務(wù)比較復(fù)雜,特別像中臺(tái)或B端功能頁面都不可避免的會(huì)用到第三方組件庫,產(chǎn)品有時(shí)會(huì)想對(duì)這些組件進(jìn)行一些UI方面的定制。

[[243288]]

 一、深度作用選擇器( >>> )

嚴(yán)格來說,這個(gè)應(yīng)該是vue-loader的功能。”vue-loader”: “^12.2.0”

在項(xiàng)目開發(fā)中,如果業(yè)務(wù)比較復(fù)雜,特別像中臺(tái)或B端功能頁面都不可避免的會(huì)用到第三方組件庫,產(chǎn)品有時(shí)會(huì)想對(duì)這些組件進(jìn)行一些UI方面的定制。如果這些組件采用的是有作用域的CSS,父組件想要定制第三方組件的樣式就比較麻煩了。

深度作用選擇器( >>> 操作符)可以助你一臂之力。 

  1. <template>  
  2. <div>  
  3.    <h1 class="child-title">  
  4.        如果你希望 scoped 樣式中的一個(gè)選擇器能夠作用得“更深”,例如影響子組件,你可以使用 >>> 操作   </h1>  
  5. </div>  
  6. </template>  
  7. <script>  
  8. export default {  
  9.     name: 'child',  
  10.     data() {  
  11.         return {  
  12.         }  
  13.     }  
  14.  
  15. </script>  
  16. <!-- Add "scoped" attribute to limit CSS to this component only -->  
  17. <style scoped>  
  18. .child-title {  
  19.     font-size: 12px;  
  20.  
  21. </style> 

上面的child組件中 .child-title 的作用域CSS設(shè)定字體大小為12px,現(xiàn)在想在父組件中定制為大小20px,顏色為紅色。 

  1. <template>  
  2. <div>  
  3.    <child class="parent-custom"></child>  
  4. </div>  
  5. </template>  
  6. <script>  
  7. import Child from './child';  
  8. export default {  
  9.     name: 'parent',  
  10.     components:{  
  11.         Child  
  12.     },  
  13.     data() {  
  14.         return {  
  15.         }  
  16.     }  
  17.  
  18. </script>  
  19. <style>  
  20. .parent-custom  >>> .child-title {  
  21.     font-size:20px;  
  22.     color: red;  
  23.  
  24. </style> 

效果妥妥的。但是別高興太早,注意到上面的style使用的是純css語法,如果采用less語法,你可能會(huì)收到一條webpack的報(bào)錯(cuò)信息。 

  1. <style lang="less">  
  2. .parent-custom {  
  3.      >>> .child-title {  
  4.         font-size:20px;  
  5.         color: red;  
  6.     }  
  7.  
  8. </style>  
  9. ERROR in ./~/css-loader!./~/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-960c5412","scoped":false,"hasInlineConfig":false}!./~/postcss-loader!./~/less-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/parent.vue 
  10.  
  11. Module build failed: Unrecognised input  
  12.  @ /src/components/parent.vue (line 22, column 6)  
  13.  near lines:  
  14.    .parent-custom {  
  15.         >>> .child-title {  
  16.            font-size:20px; 

上面的報(bào)錯(cuò)信息其實(shí)是less語法不認(rèn)識(shí) >>>。(less的github issue上有人提議支持>>>操作符,但本文使用的v2.7.3會(huì)有這個(gè)問題)

解決方案是采用的less的轉(zhuǎn)義(scaping)和變量插值(Variable Interpolation) 

  1. <style lang="less">  
  2. @deep: ~'>>>';  
  3. .parent-custom {  
  4.      @{deep} .child-title {  
  5.         font-size:20px;  
  6.         color: red;  
  7.     }  
  8.  
  9. </style> 

對(duì)于其他的css預(yù)處理器,因?yàn)闆]怎么用,不妄加評(píng)論,照搬一下文檔的話。

有些像 Sass 之類的預(yù)處理器無法正確解析 >>>。這種情況下你可以使用 /deep/ 操作符取而代之——這是一個(gè) >>> 的別名,同樣可以正常工作。

二、組件配置項(xiàng)inheritAttrs、組件實(shí)例屬性$attrs和$listeners

2.4.0新增

組件配置項(xiàng) inheritAttrs

我們都知道假如使用子組件時(shí)傳了a,b,c三個(gè)prop,而子組件的props選項(xiàng)只聲明了a和b,那么渲染后c將作為html自定義屬性顯示在子組件的根元素上。

如果不希望這樣,可以設(shè)置子組件的配置項(xiàng) inheritAttrs:false,根元素就會(huì)干凈多了。 

  1. <script>  
  2. export default {  
  3.     name: 'child',  
  4.     props:['a','b'],  
  5.     inheritAttrs:false  
  6.  
  7. </script> 

組件實(shí)例屬性$attrs和$listeners

先看看vm.$attrs文檔上是怎么說的

vm.$attrs

類型:{ [key: string]: string }

只讀

包含了父作用域中不作為 prop 被識(shí)別 (且獲取) 的特性綁定 (class 和 style 除外)。當(dāng)一個(gè)組件沒有聲明任何 prop 時(shí),這里會(huì)包含所有父作用域的綁定 (class 和 style 除外),并且可以通過 v-bind=”$attrs” 傳入內(nèi)部組件——在創(chuàng)建高級(jí)別的組件時(shí)非常有用。

歸納起來就是兩點(diǎn):

vm.$attrs是組件的內(nèi)置屬性,值是父組件傳入的所有prop中未被組件聲明的prop(class和style除外)。

還是以前面的child組件舉例 

  1. //parent.vue  
  2. <template>  
  3.     <div>  
  4.         <child class="parent-custom" a="a" b="b" c="c"></child>  
  5.     </div>  
  6. </template>  
  7. //child.vue  
  8. <script>  
  9. export default {  
  10.     name: 'child',  
  11.     props:['a','b'],  
  12.     inheritAttrs:false,  
  13.     mounted(){  
  14.         //控制臺(tái)輸出:  
  15.         //child:$attrs: {c: "c"}  
  16.         console.log('child:$attrs:',this.$attrs);  
  17.     }  
  18.  
  19. </script> 

組件可以通過在自己的子組件上使用v-bind=”$attrs”,進(jìn)一步把值傳給自己的子組件。也就是說子組件會(huì)把$attrs的值當(dāng)作傳入的prop處理,同時(shí)還要遵守***點(diǎn)的規(guī)則。 

  1. //parent.vue  
  2. <template>  
  3.     <div>  
  4.         <child a="a" b="b" c="c"></child>  
  5.     </div>  
  6. </template>  
  7. //child.vue  
  8. <template>  
  9.     <div>  
  10.         <grand-child v-bind="$attrs" d="d"></grand-child>  
  11.     </div>  
  12. </template>  
  13. <script>  
  14. export default {  
  15.     name: 'child',  
  16.     props:['a','b'],  
  17.     inheritAttrs:false  
  18.  
  19. </script>  
  20. //grandchild.vue  
  21. <script>  
  22. export default {  
  23.     name: 'grandchild',  
  24.     props:[],  
  25.     //props:['c'],  
  26.     inheritAttrs:false,  
  27.     mounted(){  
  28.         //控制臺(tái)輸出:  
  29.         //grandchild:$attrs: {d: "d", c: "c"}  
  30.         console.log('grandchild:$attrs:',this.$attrs);  
  31.         //如果props:['c']  
  32.         //控制臺(tái)輸出:  
  33.         //grandchild:$attrs: {d: "d"}  
  34.     },  
  35.  
  36. </script> 

vm.$listeners

類型:{ [key: string]: Function | Array }

只讀

包含了父作用域中的 (不含 .native 修飾器的) v-on 事件監(jiān)聽器。它可以通過 v-on=”$listeners” 傳入內(nèi)部組件——在創(chuàng)建更高層次的組件時(shí)非常有用

歸納起來也是兩點(diǎn):

  1.  vm.$listeners是組件的內(nèi)置屬性,它的值是父組件(不含 .native 修飾器的) v-on 事件監(jiān)聽器。
  2.  組件可以通過在自己的子組件上使用v-on=”$listeners”,進(jìn)一步把值傳給自己的子組件。如果子組件已經(jīng)綁定$listener中同名的監(jiān)聽器,則兩個(gè)監(jiān)聽器函數(shù)會(huì)以冒泡的方式先后執(zhí)行。 
  1. //parent.vue  
  2. <template>  
  3.     <div>  
  4.         <child @update="onParentUpdate"></child>  
  5.     </div>  
  6. </template>  
  7. <script>  
  8. export default {  
  9.     name: 'parent',  
  10.     components:{  
  11.         Child  
  12.     },  
  13.     methods:{  
  14.         onParentUpdate(){  
  15.             console.log('parent.vue:onParentUpdate')  
  16.         }  
  17.     }  
  18.  
  19. </script>  
  20. //child.vue  
  21. <template>  
  22.     <div>  
  23.         <grand-child @update="onChildUpdate" v-on="$listeners"></grand-child>  
  24.     </div>  
  25. </template>  
  26. <script>  
  27. export default {  
  28.     name: 'child',  
  29.     components:{  
  30.         GrandChild  
  31.     },  
  32.     methods:{  
  33.         onChildUpdate(){  
  34.             console.log('child.vue:onChildUpdate')  
  35.         }  
  36.     }  
  37.  
  38. </script>  
  39. //grandchild.vue  
  40. <script>  
  41. export default {  
  42.     name: 'grandchild',  
  43.     mounted(){  
  44.         //控制臺(tái)輸出:  
  45.         //grandchild:$listeners: {update: ƒ}  
  46.         console.log('grandchild:$listeners:',this.$listeners);  
  47.         //控制臺(tái)輸出:  
  48.         //child.vue:onChildUpdate  
  49.         //parent.vue:onParentUpdate  
  50.         this.$listeners.update();  
  51.     }  
  52.  
  53. </script> 

三、組件選項(xiàng) provide/inject

    2.2.0 新增

如果列舉Vue組件之間的通信方法,一般都會(huì)說通過prop,自定義事件,事件總線,還有Vuex。provide/inject提供了另一種方法。

這對(duì)選項(xiàng)需要一起使用,以允許一個(gè)祖先組件向其所有子孫后代注入一個(gè)依賴,不論組件層次有多深,并在起上下游關(guān)系成立的時(shí)間里始終生效。

如果你熟悉 React,這與 React 的上下文特性(context)很相似。

不過需要注意的是,在文檔中并不建議直接用于應(yīng)用程序中。

provide 和 inject 主要為高階插件/組件庫提供用例。并不推薦直接用于應(yīng)用程序代碼中。 

  1. //parent.vue  
  2. <template>  
  3.     <div>  
  4.         <child></child>  
  5.     </div>  
  6. </template>  
  7. <script>  
  8. export default {  
  9.     name: 'parent',  
  10.     provide: {  
  11.         data: 'I am parent.vue'  
  12.     },  
  13.     components:{  
  14.         Child  
  15.     }  
  16.  
  17. </script>  
  18. //child.vue  
  19. <template>  
  20.     <div>  
  21.         <grand-child></grand-child>  
  22.     </div>  
  23. </template>  
  24. <script>  
  25. export default {  
  26.     name: 'child',  
  27.     components:{  
  28.         GrandChild  
  29.     }  
  30.  
  31. </script>  
  32. //grandchild.vue  
  33. <script>  
  34. export default {  
  35.     name: 'grandchild',  
  36.     inject: ['data'],  
  37.     mounted(){  
  38.         //控制臺(tái)輸出:  
  39.         //grandchild:inject: I am parent.vue  
  40.         console.log('grandchild:inject:',this.data);  
  41.     }  
  42.  
  43. </script> 

provide 選項(xiàng)應(yīng)該是一個(gè)對(duì)象或返回一個(gè)對(duì)象的函數(shù)。該對(duì)象包含可注入其子孫的屬性。

inject 選項(xiàng)應(yīng)該是一個(gè)字符串?dāng)?shù)組或一個(gè)對(duì)象,該對(duì)象的 key 代表了本地綁定的名稱,value 就為provide中要取值的key。

在2.5.0+時(shí)對(duì)于inject選項(xiàng)為對(duì)象時(shí),還可以指定from來表示源屬性,default指定默認(rèn)值(如果是非原始值要使用一個(gè)工廠方法)。 

  1. const Child = {  
  2.   inject: {  
  3.     foo: {  
  4.       from: 'bar',  
  5.       default: 'foo'  
  6.       //default: () => [1, 2, 3]  
  7.     }  
  8.   }  

四、作用域插槽 slot-scope

2.1.0 新增

在 2.5.0+,slot-scope 不再限制在 template 元素上使用,而可以用在插槽內(nèi)的任何元素或組件上。

作用域插槽的文檔說明很詳細(xì)。下面舉個(gè)例子來展示下應(yīng)用場景。

可以看出列表頁和編輯頁對(duì)于數(shù)據(jù)的展示是一樣的,***的區(qū)別是在不同頁面對(duì)于數(shù)據(jù)有不同的處理邏輯。相同的數(shù)據(jù)展示這塊就可抽取成一個(gè)組件,不同的地方則可以借助作用域插槽實(shí)現(xiàn)。 

  1. //data-show.vue  
  2. <template>  
  3. <div>  
  4.    <ul>  
  5.         <li v-for="item in list">  
  6.             <span>{{item.title}}</span>  
  7.             <slot v-bind:item="item">  
  8.             </slot>  
  9.         </li>  
  10.     </ul>  
  11. </div>  
  12. </template>  
  13. //list.vue  
  14. <template>  
  15. <p>列表頁</p>  
  16.     <data-show :list="list">  
  17.         <template slot-scope="slotProps">  
  18.             <span v-if="slotProps.item.complete"></span>  
  19.             <span v-else>x</span>  
  20.         </template>  
  21.     </data-show>  
  22. </template>  
  23. //edit.vue  
  24. <template>  
  25. <p>編輯頁</p>  
  26.    <data-show :list="list">  
  27.        <template slot-scope="slotProps">  
  28.            <a v-if="slotProps.item.complete">查看</a>  
  29.            <a v-else>修改</a>  
  30.        </template>  
  31.    </data-show>  
  32. </template> 

五、Vue的錯(cuò)誤捕獲

全局配置errorHandler

從2.2.0起,這個(gè)鉤子也會(huì)捕獲組件生命周期鉤子里的錯(cuò)誤。

從 2.4.0 起這個(gè)鉤子也會(huì)捕獲 Vue 自定義事件處理函數(shù)內(nèi)部的錯(cuò)誤了。

更詳細(xì)的說明可以查看文檔errorHandler

生命周期鉤子errorCaptured

2.5.0+新增

更詳細(xì)的說明可以查看文檔errorCaptured

如果熟悉React的話,會(huì)發(fā)現(xiàn)它跟錯(cuò)誤邊界(Error Boundaries)的概念很像,實(shí)際上也確實(shí)是這么用的。

在文檔Error Handling with errorCaptured Hook就舉了一個(gè)典型的例子 

  1. Vue.component('ErrorBoundary', {  
  2.   data: () => ({ error: null }),  
  3.   errorCaptured (err, vm, info) {  
  4.     this.error = `${err.stack}\n\nfound in ${info} of component`  
  5.     return false  
  6.   },  
  7.   render (h) {  
  8.     if (this.error) {  
  9.       return h('pre', { style: { color: 'red' }}, this.error)  
  10.     }  
  11.     // ignoring edge cases for the sake of demonstration  
  12.     return this.$slots.default[0]  
  13.   }  
  14. })  
  1. <error-boundary>  
  2.   <another-component/>  
  3. </error-boundary> 

需要強(qiáng)調(diào)的是errorCaptured并不能捕獲自身錯(cuò)誤和異步錯(cuò)誤(比如網(wǎng)絡(luò)請(qǐng)求,鼠標(biāo)事件等產(chǎn)生的錯(cuò)誤)。

In 2.5 we introduce the new errorCaptured hook. A component with this hook captures all errors (excluding those fired in async callbacks) from its child component tree (excluding itself). 

責(zé)任編輯:龐桂玉 來源: 前端大全
相關(guān)推薦

2021-04-23 07:51:56

CSS Container Q Chrome

2011-07-19 18:11:09

iPhone 開發(fā)

2017-05-23 14:33:46

簡歷求職前端開發(fā)

2009-06-16 13:29:56

JBoss事務(wù)

2009-07-07 17:34:15

collectionJDK5.0

2012-12-24 14:51:02

iOS

2022-05-24 12:50:58

Pandas索引代碼

2013-03-29 09:03:59

iOS實(shí)用小代碼iOS開發(fā)

2023-11-13 07:54:54

.NET Core開源框架

2011-03-16 10:40:42

JavaEEJ2EE

2014-03-19 15:41:21

編程語言編程規(guī)則

2014-08-08 09:14:43

Linux瀏覽器

2014-08-26 10:03:45

Oracle 12c新

2013-07-24 09:32:13

Android項(xiàng)目

2018-07-30 08:41:48

VueReact區(qū)別

2010-03-25 13:59:52

Python API

2016-11-16 21:18:42

android日志

2020-08-23 08:56:52

Vue Router 前端Vue

2011-06-16 14:28:08

Qt Symbian 文件

2010-08-17 10:16:37

DIV樣式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)