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

Vue3值得注意的新特性之——觸發(fā)組件選項(xiàng)

開發(fā) 前端
與組件和prop一樣,事件名提供了自動的大小寫轉(zhuǎn)換。如果用駝峰命名的子組件中觸發(fā)一個事件,你將可以在父組件中添加一個kebabcase(短橫線分割命名)的監(jiān)聽器。

事件名

與組件和prop一樣,事件名提供了自動的大小寫轉(zhuǎn)換。如果用駝峰命名的子組件中觸發(fā)一個事件,你將可以在父組件中添加一個kebabcase(短橫線分割命名)的監(jiān)聽器。

  1. <my-component @my-event="doSomething"></my-component>  
  2. this.$emit('myEvent'

與props命名一樣,當(dāng)你使用DOM模板時,我們建議使用kebabcase事件監(jiān)聽器。如果你使用的是字符串模板,這個限制就不適用。

定義自定義事件

可以通過emits選項(xiàng)在組件上定義已發(fā)出的事件。 

  1. app.component('custom-form', { 
  2.   emits: ['inFocus''submit'
  3. }) 

當(dāng)在emits選項(xiàng)中定義了原生事件(如click)時,將使用組件中的事件替代原生事件偵聽器。

驗(yàn)證拋出的事件

與props類型驗(yàn)證類似,如果使用對象語法而不是數(shù)組語法,則可以驗(yàn)證它。

要添加驗(yàn)證,將為事件分配一個函數(shù),該函數(shù)接收傳遞給$emit調(diào)用的參數(shù),并返回一個布爾值以指示事件是否有效。 

  1. app.component('custom-form', { 
  2.   emits: { 
  3.     // 沒有驗(yàn)證 
  4.     click: null
  5.  
  6.     // 驗(yàn)證submit 事件 
  7.     submit: ({ email, password }) => { 
  8.       if (email && password) { 
  9.         return true 
  10.       } else { 
  11.         console.warn('Invalid submit event payload!'
  12.         return false 
  13.       } 
  14.     } 
  15.   }, 
  16.   methods: { 
  17.     submitForm() { 
  18.       this.$emit('submit', { email, password }) 
  19.     } 
  20.   } 
  21. }) 

v-model事件

默認(rèn)情況下,組件上的v-model使用modelValue作為props和update:modelValue做完事件。我們可以通過向v-model傳遞參數(shù)來修改這些名稱:

  1. <my-component v-model:title="bookTitle"></my-component> 

在本例中,子組件將需要一個 title prop 并發(fā)出 update:title 要同步的事件: 

  1. app.component('my-component', { 
  2.   props: { 
  3.     title: String 
  4.   }, 
  5.   emits: ['update:title'], 
  6.   template: ` 
  7.     <input 
  8.       type="text" 
  9.       :value="title" 
  10.       @input="$emit('update:title', $event.target.value)"
  11.   ` 
  12. }) 

多個v-model綁定

通過利用特定prop和事件為目標(biāo)的能力,正如我們之前在v-model參數(shù)中所學(xué)的那樣,我們現(xiàn)在可以在單個組件實(shí)例上創(chuàng)建多個v-model綁定。

每個v-model將同步到不同的prop,而不需要在組件中添加額外的選項(xiàng)。 

  1. <user-name 
  2.   v-model:first-name="firstName" 
  3.   v-model:last-name="lastName" 
  4. ></user-name
  5.  
  6. app.component('user-name', { 
  7.   props: { 
  8.     firstName: String, 
  9.     lastName: String 
  10.   }, 
  11.   emits: ['update:firstName''update:lastName'], 
  12.   template: ` 
  13.     <input  
  14.       type="text" 
  15.       :value="firstName" 
  16.       @input="$emit('update:firstName', $event.target.value)"
  17.  
  18.     <input 
  19.       type="text" 
  20.       :value="lastName" 
  21.       @input="$emit('update:lastName', $event.target.value)"
  22.   ` 
  23. }) 

處理v-model修飾詞

在2.X中,我們對組件v-model上的.trim等修飾符提供了硬編碼支持。但是,如果組件可以支持自定義修飾符,則會更有用。

在3.X中,添加到組件v-model的修飾符將通過modelModifiers prop提供給組件。

v-model有內(nèi)置的修飾符——.trim,.number和.lazy。但是,在某些情況下,你可能還需要添加自己的自定義修飾符。

我們做個實(shí)例:將提供的字符串第一個字母大寫。 

  1. <my-component v-model.capitalize="myText"></my-component> 
  2.  
  3. app.component('my-component', { 
  4.   props: { 
  5.     modelValue: String, 
  6.     modelModifiers: { 
  7.       default: () => ({}) 
  8.     } 
  9.   }, 
  10.   emits: ['update:modelValue'], 
  11.   template: ` 
  12.     <input type="text" 
  13.       :value="modelValue" 
  14.       @input="$emit('update:modelValue', $event.target.value)"
  15.   `, 
  16.   created() { 
  17.     console.log(this.modelModifiers) // { capitalize: true } 
  18.   } 
  19. }) 

現(xiàn)在我們已經(jīng)設(shè)置了 prop,我們可以檢查 modelModifiers 對象鍵并編寫一個處理器來更改發(fā)出的值。在下面的代碼中,每當(dāng)<input/> 元素觸發(fā) input 事件時,我們都將字符串大寫。

  1. <div id="app"
  2.   <my-component v-model.capitalize="myText"></my-component> 
  3.   {{ myText }} 
  4. </div> 
  5.  
  6. const app = Vue.createApp({ 
  7.   data() { 
  8.     return { 
  9.       myText: '' 
  10.     } 
  11.   } 
  12. }) 
  13.  
  14. app.component('my-component', { 
  15.   props: { 
  16.     modelValue: String, 
  17.     modelModifiers: { 
  18.       default: () => ({}) 
  19.     } 
  20.   }, 
  21.   emits: ['update:modelValue'], 
  22.   methods: { 
  23.     emitValue(e) { 
  24.       let value = e.target.value 
  25.       if (this.modelModifiers.capitalize) { 
  26.         value = value.charAt(0).toUpperCase() + value.slice(1) 
  27.       } 
  28.       this.$emit('update:modelValue', value) 
  29.     } 
  30.   }, 
  31.   template: `<input 
  32.     type="text" 
  33.     :value="modelValue" 
  34.     @input="emitValue">` 
  35. }) 
  36.  
  37. app.mount('#app'

對于帶參數(shù)的 v-model 綁定,生成的 prop 名稱將為 arg + "Modifiers": 

  1. <my-component v-model:description.capitalize="myText"></my-component> 
  2.  
  3. app.component('my-component', { 
  4.   props: ['description''descriptionModifiers'], 
  5.   emits: ['update:description'], 
  6.   template: ` 
  7.     <input type="text" 
  8.       :value="description" 
  9.       @input="$emit('update:description', $event.target.value)"
  10.   `, 
  11.   created() { 
  12.     console.log(this.descriptionModifiers) // { capitalize: true } 
  13.   } 
  14. }) 

 

 

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2021-05-12 10:25:29

開發(fā)技能代碼

2015-06-23 15:48:41

Swift 2.0iOS9

2010-07-21 16:28:33

職場

2022-07-14 08:22:48

Computedvue3

2013-09-16 13:18:28

遺留系統(tǒng)系統(tǒng)遷移

2021-12-31 07:48:58

Vue3 插件Vue應(yīng)用

2010-11-26 15:05:58

MySQL變量

2015-10-08 09:25:05

比特幣存儲開源

2010-03-31 15:52:24

Oracle子查詢

2017-02-21 13:20:02

SD-WAN軟件定義網(wǎng)絡(luò)廣域網(wǎng)

2011-05-12 09:29:54

2009-06-15 14:53:00

NetBeans 6.

2021-08-11 08:31:42

前端技術(shù)Vue3

2023-03-15 18:25:25

2020-12-01 08:34:31

Vue3組件實(shí)踐

2025-04-18 08:07:12

2019-07-19 10:08:49

iOS應(yīng)用系統(tǒng)

2025-04-14 08:06:04

2025-04-15 08:00:53

2025-04-17 08:00:48

點(diǎn)贊
收藏

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