欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue2.4+新增屬性.sync、$attrs、$listeners的具體使用

 更新時間:2020年03月08日 10:58:37   作者:藍山牧童  
這篇文章主要介紹了Vue2.4+新增屬性.sync、$attrs、$listeners的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

sync

在vue2.4以前,父組件向子組件傳值用props;子組件不能直接更改父組件傳入的值,需要通過$emit觸發(fā)自定義事件,通知父組件改變后的值。比較繁瑣,寫法如下:

//父組件
<template>
 <div class="parent">
  <p>父組件傳入子組件的值:{{name}}</p>
  <fieldset>
   <legend>子組件</legend>
   <child :val="name" @update="modify">
   </child>
  </fieldset>
 </div>
</template>

<script>
import Child from './Child'
export default {
 components:{Child},
 data () {
  return {
   name:'linda'
  }
 },
 methods:{
  modify(newVal){
   this.name=newVal
  }
 }
}
</script>

//子組件
<template>
  <label class="child">
    輸入框:
    <input :value=val @input="$emit('update',$event.target.value)"/>
  </label>
</template>
<script>
export default {
  props:['val']
}
</script>

vue2.4以后的寫法明顯舒服許多,上面同樣的功能,直接上代碼

//父組件
<template>
 <div class="parent">
  <p>父組件傳入子組件的值:{{name}}</p>
  <fieldset>
   <legend>子組件</legend>
   <child :val.sync="name">
   </child>
  </fieldset>
 </div>
</template>

<script>
import Child from './Child'
export default {
 components:{Child},
 data () {
  return {
   name:'linda'
  }
 }
}
</script>

//子組件
<template>
  <label class="child">
    輸入框:
    <input :value=val @input="$emit('update:val',$event.target.value)"/>
  </label>
</template>
<script>
export default {
  props:['val']
}
</script>

寫法上簡化了一部分,很明顯父組件不用再定義方法檢測值變化了。其實只是對以前的$emit方式的一種縮寫,.sync其實就是在父組件定義了一update:val方法,來監(jiān)聽子組件修改值的事件。

$attrs

想象一下,你打算封裝一個自定義input組件——MyInput,需要從父組件傳入type,placeholder,title等多個html元素的原生屬性。此時你的MyInput組件props如下:

props:['type','placeholder','title',...]

很丑陋不是嗎?$attrs專門為了解決這種問題而誕生,這個屬性允許你在使用自定義組件時更像是使用原生html元素。比如:

//父組件
<my-input placeholder="請輸入你的姓名" type="text" title="姓名" v-model="name"/>

my-input的使用方式就像原生的input一樣。而MyInput并沒有設(shè)置props,如下

<template>
  <div>
    <label>輸入框:</label><input v-bind="$attrsAll" @input="$emit('input',$event.target.value)"/>
  </div>
</template>
<script>
export default {
  inheritAttrs:false,
  computed: {
    $attrsAll() {
      return {
        value: this.$vnode.data.model.value,
        ...this.$attrs
      }
    }
  }
}
</script>

基礎(chǔ)掃盲

v-model是v-bind:value和v-on:input的簡寫,所以在父組件你完全可以直接寫 :value="name",@input="val => name = val"。查看文檔

疑難

引用下vue的官方api中對$attrs的說明

$attrs包含了父作用域中不作為 prop 被識別 (且獲取) 的特性綁定 (class 和 style 除外)

比較迷惑的一點是給子組件設(shè)置:value="name"相當于給子組件設(shè)置props:['value'],所以在MyInput中直接從$attrs獲取不到value,需要重新包裝$attrsAll,添加value屬性。所以子組件還有下面寫法,我傾向于這種寫法,因為它更優(yōu)雅

<template>
  <div>
    <label>輸入框:</label><input v-bind="$attrs" :value="value" @input="$emit('input',$event.target.value)"/>
  </div>
</template>
<script>
export default {
  inheritAttrs:false,
  props:['value']
}
</script>

$listener

同上面$attrs屬性一樣,這個屬性也是為了在自定義組件中使用原生事件而產(chǎn)生的。比如要讓前面的MyInput組件實現(xiàn)focus事件,直接這么寫是沒用的

<my-input @focus="focus" placeholder="請輸入你的姓名" type="text" title="姓名" v-model="name"/>

必須要讓focus事件作用于MyInput組件的input元素上,最終的MyInput源碼如下:

<template>
  <div>
    <label>輸入框:</label><input v-bind="$attrsAll" v-on="$listenserAll"/>
  </div>
</template>
<script>
export default {
  inheritAttrs:false,
  props:['value'],
  computed:{
     $attrsAll() {
      return {
        value: this.value,
        ...this.$attrs
      }
    },
    $listenserAll(){
      return Object.assign(
        {},
        this.$listeners,
        {input:(event) => this.$emit('input',event.target.value)})
    }
  }
}
</script>

到此這篇關(guān)于Vue2.4+新增屬性.sync、$attrs、$listeners的具體使用的文章就介紹到這了,更多相關(guān)Vue2.4 .sync、$attrs、$listeners內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)

    使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)

    這篇文章主要介紹了使用Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù),整篇文章圍繞Vue3進行數(shù)據(jù)綁定及顯示列表數(shù)據(jù)的想換自來哦展開內(nèi)容,需要的小伙伴可以參考一下
    2021-10-10
  • ant design vue中table表格滾動加載實現(xiàn)思路

    ant design vue中table表格滾動加載實現(xiàn)思路

    在處理一寫數(shù)據(jù)量特別大的情況下,我們不能把后端的數(shù)據(jù)一次性全部拿到前端在table表格中展示,為了考慮性能優(yōu)化,使用了滾動加載表格數(shù)據(jù),這篇文章主要介紹了ant design vue中table表格滾動加載實現(xiàn)思路,需要的朋友可以參考下
    2024-07-07
  • vue項目中如何配置eslint和prettier

    vue項目中如何配置eslint和prettier

    這篇文章主要介紹了vue項目中如何配置eslint和prettier問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue 表單輸入框不支持focus及blur事件的解決方案

    vue 表單輸入框不支持focus及blur事件的解決方案

    這篇文章主要介紹了vue 表單輸入框不支持focus及blur事件的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 詳解vuelidate 對于vueJs2.0的驗證解決方案

    詳解vuelidate 對于vueJs2.0的驗證解決方案

    本篇文章主要介紹了vuelidate 對于vueJs2.0的驗證解決方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Vuex管理dialog、toast等常見全局性組件狀態(tài)時唯一性的問題解決

    Vuex管理dialog、toast等常見全局性組件狀態(tài)時唯一性的問題解決

    工作中經(jīng)常會用到類似于?dialog、toast、popover?等一些狀態(tài)提示組件,這篇文章主要介紹了Vuex管理dialog、toast等常見全局性組件狀態(tài)時唯一性的問題,需要的朋友可以參考下
    2022-11-11
  • Vue路由跳轉(zhuǎn)步驟詳解

    Vue路由跳轉(zhuǎn)步驟詳解

    這篇文章主要介紹了?Vue路由跳轉(zhuǎn)步驟詳解,主要介紹當訪問API成功后跳轉(zhuǎn)到新的Vue頁面怎么處理,感興趣的朋友跟隨小編一起看看吧
    2023-07-07
  • 非Vuex實現(xiàn)的登錄狀態(tài)判斷封裝實例代碼

    非Vuex實現(xiàn)的登錄狀態(tài)判斷封裝實例代碼

    這篇文章主要給大家介紹了關(guān)于非Vuex實現(xiàn)的登錄狀態(tài)判斷封裝的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-02-02
  • vue中關(guān)于el-popover的使用

    vue中關(guān)于el-popover的使用

    這篇文章主要介紹了vue中關(guān)于el-popover的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Vue生命周期深入分析總結(jié)

    Vue生命周期深入分析總結(jié)

    Vue的生命周期就是vue實例從創(chuàng)建到銷毀的全過程,也就是new?Vue()?開始就是vue生命周期的開始。Vue?實例有?個完整的?命周期,也就是從開始創(chuàng)建、初始化數(shù)據(jù)、編譯模版、掛載Dom?->?渲染、更新?->?渲染、卸載?等?系列過程,稱這是Vue的?命周期
    2022-08-08

最新評論