最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年09月25日 09:23:04   投稿:mrr  
這篇文章主要介紹了Vue高版本中一些新特性的使用,需要的朋友可以參考下

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

嚴(yán)格來(lái)說(shuō),這個(gè)應(yīng)該是vue-loader的功能?!眝ue-loader”: “^12.2.0”

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

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

<template>
<div>
  <h1 class="child-title">
    如果你希望 scoped 樣式中的一個(gè)選擇器能夠作用得“更深”,例如影響子組件,你可以使用 >>> 操作
  </h1>
</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設(shè)定字體大小為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語(yǔ)法,如果采用less語(yǔ)法,你可能會(huì)收到一條webpack的報(bào)錯(cuò)信息。

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

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

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

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

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

有些像 Sass 之類(lèi)的預(yù)處理器無(wú)法正確解析 >>>。這種情況下你可以使用 /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ì)干凈多了。

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

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

先看看vm.$attrs文檔上是怎么說(shuō)的

vm.$attrs

類(lèi)型:{ [key: string]: string }

只讀

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

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

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(){
    //控制臺(tái)輸出:
    //child:$attrs: {c: "c"}
    console.log('child:$attrs:',this.$attrs);
  }
}
</script>

組件可以通過(guò)在自己的子組件上使用v-bind=”$attrs”,進(jìn)一步把值傳給自己的子組件。也就是說(shuō)子組件會(huì)把$attrs的值當(dāng)作傳入的prop處理,同時(shí)還要遵守第一點(diǎn)的規(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(){
    //控制臺(tái)輸出:
    //grandchild:$attrs: {d: "d", c: "c"}
    console.log('grandchild:$attrs:',this.$attrs);
    //如果props:['c']
    //控制臺(tái)輸出:
    //grandchild:$attrs: {d: "d"}
  },
}
</script>

vm.$listeners

類(lèi)型:{ [key: string]: Function | Array }

只讀

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

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

1、vm.$listeners是組件的內(nèi)置屬性,它的值是父組件(不含 .native 修飾器的) v-on 事件監(jiān)聽(tīng)器。
2、組件可以通過(guò)在自己的子組件上使用v-on=”$listeners”,進(jìn)一步把值傳給自己的子組件。如果子組件已經(jīng)綁定$listener中同名的監(jiān)聽(tīng)器,則兩個(gè)監(jiān)聽(tīng)器函數(shù)會(huì)以冒泡的方式先后執(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(){
    //控制臺(tái)輸出:
    //grandchild:$listeners: {update: ƒ}
    console.log('grandchild:$listeners:',this.$listeners);
    //控制臺(tái)輸出:
    //child.vue:onChildUpdate
    //parent.vue:onParentUpdate
    this.$listeners.update();
  }
}
</script>

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

2.2.0 新增

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

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

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

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

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

//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(){
    //控制臺(tái)輸出:
    //grandchild:inject: I am parent.vue
    console.log('grandchild:inject:',this.data);
  }
}
</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 代表了本地綁定的名稱(chēng),value 就為provide中要取值的key。

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

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)的任何元素或組件上。

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

可以看出列表頁(yè)和編輯頁(yè)對(duì)于數(shù)據(jù)的展示是一樣的,唯一的區(qū)別是在不同頁(yè)面對(duì)于數(shù)據(jù)有不同的處理邏輯。相同的數(shù)據(jù)展示這塊就可抽取成一個(gè)組件,不同的地方則可以借助作用域插槽實(shí)現(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>列表頁(yè)</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>編輯頁(yè)</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的錯(cuò)誤捕獲

全局配置errorHandler

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

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

生命周期鉤子errorCaptured

2.5.0+新增

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

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

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

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>

需要強(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).

參考

https://github.com/vuejs/vue/releases

https://github.com/vuejs/vue-loader/releases

總結(jié)

以上所述是小編給大家介紹的符Vue高版本中一些新特性的使用,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • vue移動(dòng)端項(xiàng)目緩存問(wèn)題實(shí)踐記錄

    vue移動(dòng)端項(xiàng)目緩存問(wèn)題實(shí)踐記錄

    最近在做一個(gè)vue移動(dòng)端項(xiàng)目,被緩存問(wèn)題搞得頭都大了,積累了一些經(jīng)驗(yàn),特此記錄總結(jié)下,分享到腳本之家平臺(tái),對(duì)vue移動(dòng)端項(xiàng)目緩存問(wèn)題實(shí)踐記錄感興趣的朋友跟隨小編一起看看吧
    2018-10-10
  • 使用imba.io框架得到比 vue 快50倍的性能基準(zhǔn)

    使用imba.io框架得到比 vue 快50倍的性能基準(zhǔn)

    imba 是一種新的編程語(yǔ)言,可以編譯為高性能的 JavaScript??梢灾苯佑糜?Web 編程(服務(wù)端與客戶(hù)端)開(kāi)發(fā)。這篇文章主要介紹了使用imba.io框架,得到比 vue 快50倍的性能基準(zhǔn),需要的朋友可以參考下
    2019-06-06
  • 基于Vue實(shí)例對(duì)象的數(shù)據(jù)選項(xiàng)

    基于Vue實(shí)例對(duì)象的數(shù)據(jù)選項(xiàng)

    下面小編就為大家?guī)?lái)一篇基于Vue實(shí)例對(duì)象的數(shù)據(jù)選項(xiàng)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • Vue項(xiàng)目中使用vuex詳解

    Vue項(xiàng)目中使用vuex詳解

    Vuex是一個(gè)專(zhuān)為Vue.js應(yīng)用程序開(kāi)發(fā)的狀態(tài)管理模式,它采用集中式存儲(chǔ)管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測(cè)的方式發(fā)生變化,下面這篇文章主要給大家介紹了關(guān)于vuex模塊獲取數(shù)據(jù)及方法的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • Vue element-admin權(quán)限控制之按鈕使用

    Vue element-admin權(quán)限控制之按鈕使用

    這篇文章主要介紹了Vue element-admin權(quán)限控制之按鈕使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • vue中數(shù)組常用的6種循環(huán)方法代碼示例

    vue中數(shù)組常用的6種循環(huán)方法代碼示例

    在vue項(xiàng)目開(kāi)發(fā)中,我們需要對(duì)數(shù)組進(jìn)行處理等問(wèn)題,這里簡(jiǎn)單記錄遍歷數(shù)組的幾種方法,這篇文章主要給大家介紹了關(guān)于vue中數(shù)組常用的6種循環(huán)方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • Vue3基于?rem?比例縮放方案示例詳解

    Vue3基于?rem?比例縮放方案示例詳解

    這篇文章主要介紹了Vue3基于rem比例縮放方案,本縮放方案置于hooks中即可,文中通過(guò)示例代碼介紹了vue-cli3 中使用rem布局的方法,需要的朋友可以參考下
    2023-05-05
  • vue開(kāi)發(fā)實(shí)現(xiàn)評(píng)論列表

    vue開(kāi)發(fā)實(shí)現(xiàn)評(píng)論列表

    這篇文章主要為大家詳細(xì)介紹了vue開(kāi)發(fā)實(shí)現(xiàn)評(píng)論列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue3中Reactive的使用詳解

    Vue3中Reactive的使用詳解

    Vue 3 的 Composition API 帶來(lái)了強(qiáng)大的 reactive 函數(shù),它允許你在 Vue 應(yīng)用程序中創(chuàng)建響應(yīng)式數(shù)據(jù),本文我們將深入探討 Vue 3 的 reactive,并提供一些注意事項(xiàng)和解決方案,希望可以幫助打更好地使用它
    2023-11-11
  • 使用Vue3實(shí)現(xiàn)列表虛擬滾動(dòng)的詳細(xì)步驟

    使用Vue3實(shí)現(xiàn)列表虛擬滾動(dòng)的詳細(xì)步驟

    在前端開(kāi)發(fā)中,列表的虛擬滾動(dòng)是一種常見(jiàn)的優(yōu)化手段,可以大大提升頁(yè)面性能,在Vue3中,我們可以通過(guò)一些技巧來(lái)實(shí)現(xiàn)列表的虛擬滾動(dòng),本文將介紹如何使用Vue3實(shí)現(xiàn)列表的虛擬滾動(dòng),讓你的頁(yè)面加載更快、更流暢,需要的朋友可以參考下
    2024-09-09

最新評(píng)論

怀集县| 永寿县| 菏泽市| 临高县| 德庆县| 恩施市| 北碚区| 德兴市| 遵化市| 嵊州市| 两当县| 石景山区| 思南县| 阳谷县| 海南省| 灵川县| 南岸区| 景洪市| 清远市| 福清市| 林芝县| 黑河市| 岳阳市| 锦州市| 鄱阳县| 桃江县| 桑日县| 青神县| 呼图壁县| 建始县| 东明县| 邵阳县| 嘉鱼县| 商城县| 恩施市| 平昌县| 南和县| 万安县| 江安县| 甘孜县| 博湖县|