内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

怎么使用Vue高版本中的新特性

這篇文章主要介紹“怎么使用Vue高版本中的新特性”,在日常操作中,相信很多人在怎么使用Vue高版本中的新特性問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用Vue高版本中的新特性”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

婁煩網(wǎng)站建設公司創(chuàng)新互聯(lián),婁煩網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為婁煩上1000家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設要多少錢,請找那個售后服務好的婁煩做網(wǎng)站的公司定做!

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

嚴格來說,這個應該是vue-loader的功能。”vue-loader”: “^12.2.0”

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

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

<template>  <div>     <h2 class="child-title">         如果你希望 scoped 樣式中的一個選擇器能夠作用得“更深”,例如影響子組件,你可以使用 >>> 操作   </h2>  </div>  </template>  <script>  export default {      name: 'child',      data() {          return {          }      }  }  </script>  <!-- Add "scoped" attribute to limit CSS to this component only -->  <style scoped>  .child-title {      font-size: 12px;  }  </style>

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

<template>  <div>     <child class="parent-custom"></child>  </div>  </template>  <script>  import Child from './child';  export default {      name: 'parent',      components:{          Child      },      data() {          return {          }      }  }  </script>  <style>  .parent-custom  >>> .child-title {      font-size:20px;      color: red;  }  </style>

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

<style lang="less">  .parent-custom {       >>> .child-title {          font-size:20px;          color: red;      }  }  </style>  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  Module build failed: Unrecognised input   @ /src/components/parent.vue (line 22, column 6)   near lines:     .parent-custom {          >>> .child-title {             font-size:20px;

上面的報錯信息其實是less語法不認識 >>>。(less的github issue上有人提議支持>>>操作符,但本文使用的v2.7.3會有這個問題)

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

<style lang="less">  @deep: ~'>>>';  .parent-custom {       @{deep} .child-title {          font-size:20px;          color: red;      }  }  </style>

對于其他的css預處理器,因為沒怎么用,不妄加評論,照搬一下文檔的話。

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

二、組件配置項inheritAttrs、組件實例屬性$attrs和$listeners

2.4.0新增

組件配置項 inheritAttrs

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

如果不希望這樣,可以設置子組件的配置項 inheritAttrs:false,根元素就會干凈多了。

<script>  export default {      name: 'child',      props:['a','b'],      inheritAttrs:false  }  </script>

組件實例屬性$attrs和$listeners

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

vm.$attrs

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

只讀

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

歸納起來就是兩點:

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

還是以前面的child組件舉例

//parent.vue  <template>      <div>          <child class="parent-custom" a="a" b="b" c="c"></child>      </div>  </template>  //child.vue  <script>  export default {      name: 'child',      props:['a','b'],      inheritAttrs:false,      mounted(){          //控制臺輸出:          //child:$attrs: {c: "c"}          console.log('child:$attrs:',this.$attrs);      }  }  </script>

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

//parent.vue  <template>      <div>          <child a="a" b="b" c="c"></child>      </div>  </template>  //child.vue  <template>      <div>          <grand-child v-bind="$attrs" d="d"></grand-child>      </div>  </template>  <script>  export default {      name: 'child',      props:['a','b'],      inheritAttrs:false  }  </script>  //grandchild.vue  <script>  export default {      name: 'grandchild',      props:[],      //props:['c'],      inheritAttrs:false,      mounted(){          //控制臺輸出:          //grandchild:$attrs: {d: "d", c: "c"}          console.log('grandchild:$attrs:',this.$attrs);          //如果props:['c']          //控制臺輸出:          //grandchild:$attrs: {d: "d"}      },  }  </script>

vm.$listeners

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

只讀

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

歸納起來也是兩點:

  1. 鴻蒙官方戰(zhàn)略合作共建——HarmonyOS技術(shù)社區(qū)

  2.  vm.$listeners是組件的內(nèi)置屬性,它的值是父組件(不含 .native 修飾器的) v-on 事件監(jiān)聽器。

  3.  組件可以通過在自己的子組件上使用v-on=”$listeners”,進一步把值傳給自己的子組件。如果子組件已經(jīng)綁定$listener中同名的監(jiān)聽器,則兩個監(jiān)聽器函數(shù)會以冒泡的方式先后執(zhí)行。 

//parent.vue  <template>      <div>          <child @update="onParentUpdate"></child>      </div>  </template>  <script>  export default {      name: 'parent',      components:{          Child      },      methods:{          onParentUpdate(){              console.log('parent.vue:onParentUpdate')          }      }  }  </script>  //child.vue  <template>      <div>          <grand-child @update="onChildUpdate" v-on="$listeners"></grand-child>      </div>  </template>  <script>  export default {      name: 'child',      components:{          GrandChild      },      methods:{          onChildUpdate(){              console.log('child.vue:onChildUpdate')          }      }  }  </script>  //grandchild.vue  <script>  export default {      name: 'grandchild',      mounted(){          //控制臺輸出:          //grandchild:$listeners: {update: &fnof;}          console.log('grandchild:$listeners:',this.$listeners);          //控制臺輸出:          //child.vue:onChildUpdate          //parent.vue:onParentUpdate          this.$listeners.update();      }  }  </script>

三、組件選項 provide/inject

  2.2.0 新增

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

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

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

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

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

//parent.vue  <template>      <div>          <child></child>      </div>  </template>  <script>  export default {      name: 'parent',      provide: {          data: 'I am parent.vue'      },      components:{          Child      }  }  </script>  //child.vue  <template>      <div>          <grand-child></grand-child>      </div>  </template>  <script>  export default {      name: 'child',      components:{          GrandChild      }  }  </script>  //grandchild.vue  <script>  export default {      name: 'grandchild',      inject: ['data'],      mounted(){          //控制臺輸出:          //grandchild:inject: I am parent.vue          console.log('grandchild:inject:',this.data);      }  }  </script>

provide 選項應該是一個對象或返回一個對象的函數(shù)。該對象包含可注入其子孫的屬性。

inject 選項應該是一個字符串數(shù)組或一個對象,該對象的 key 代表了本地綁定的名稱,value 就為provide中要取值的key。

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

const Child = {    inject: {      foo: {        from: 'bar',        default: 'foo'        //default: () => [1, 2, 3]      }    }  }

四、作用域插槽 slot-scope

2.1.0 新增

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

作用域插槽的文檔說明很詳細。下面舉個例子來展示下應用場景。

怎么使用Vue高版本中的新特性

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

//data-show.vue  <template>  <div>     <ul>          <li v-for="item in list">              <span>{{item.title}}</span>              <slot v-bind:item="item">              </slot>          </li>      </ul>  </div>  </template>  //list.vue  <template>  <p>列表頁</p>      <data-show :list="list">          <template slot-scope="slotProps">              <span v-if="slotProps.item.complete">?</span>              <span v-else>x</span>          </template>      </data-show>  </template>  //edit.vue  <template>  <p>編輯頁</p>     <data-show :list="list">         <template slot-scope="slotProps">             <a v-if="slotProps.item.complete">查看</a>             <a v-else>修改</a>         </template>     </data-show>  </template>

五、Vue的錯誤捕獲

全局配置errorHandler

從2.2.0起,這個鉤子也會捕獲組件生命周期鉤子里的錯誤。

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

更詳細的說明可以查看文檔errorHandler

生命周期鉤子errorCaptured

2.5.0+新增

更詳細的說明可以查看文檔errorCaptured

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

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

Vue.component('ErrorBoundary', {    data: () => ({ error: null }),    errorCaptured (err, vm, info) {      this.error = `${err.stack}\n\nfound in ${info} of component`      return false    },    render (h) {      if (this.error) {        return h('pre', { style: { color: 'red' }}, this.error)      }      // ignoring edge cases for the sake of demonstration      return this.$slots.default[0]    }  })
<error-boundary>    <another-component/>  </error-boundary>

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

到此,關(guān)于“怎么使用Vue高版本中的新特性”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

本文題目:怎么使用Vue高版本中的新特性
當前網(wǎng)址:http://www.rwnh.cn/article6/gpodig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、外貿(mào)建站品牌網(wǎng)站建設、網(wǎng)站導航、手機網(wǎng)站建設、標簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設
大连市| 建始县| 定远县| 闻喜县| 闵行区| 东乌珠穆沁旗| 拜泉县| 若尔盖县| 健康| 鄂州市| 法库县| 金秀| 河间市| 丹江口市| 永泰县| 六枝特区| 阆中市| 铅山县| 丰镇市| 揭阳市| 孟州市| 曲周县| 宁武县| 秦安县| 深圳市| 罗江县| 微博| 雅安市| 从化市| 西畴县| 孝义市| 行唐县| 蒲城县| 万年县| 蒲城县| 景洪市| 团风县| 塘沽区| 临沭县| 启东市| 浑源县|