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

Vue中裝飾器的使用示例詳解

 更新時(shí)間:2023年07月04日 11:24:31   作者:陽樹陽樹  
Vue?Property?Decorator提供了一些裝飾器,包括@Prop、@Watch、@Emit、@Inject、@Provide、@Model等等,本文主要來和大家講講它們的使用方法,需要的可以參考一下

Vue Property Decorator

Github 地址:Vue Property Decorator

Vue Property Decorator提供了一些裝飾器,包括@Prop、@Watch、@Emit、@Inject、@Provide、@Model等等。這些裝飾器可以幫助你更方便地定義組件的屬性、監(jiān)聽屬性的變化、觸發(fā)事件、注入依賴、提供依賴等等。

下載

npm i -S vue-property-decorator

使用

Prop

使用 Prop 可以快速地讓你設(shè)置你的傳入屬性。

比如下面 propA,可以設(shè)置為只讀,number 類型,下面的 propB,可以設(shè)置默認(rèn)值

import { Vue, Component, Prop } from 'vue-property-decorator';  
@Component  
export default class MyComponent extends Vue {  
?@Prop(Number) readonly propA!: number;  
?@Prop({ default: 'default value' }) readonly propB!: string; 
 // 這行代碼使用了@Prop裝飾器來定義一個(gè)名為propC的屬性,并指定了它的類型為string或boolean。
 // [String, Boolean]是一個(gè)數(shù)組,表示propC可以是string類型或boolean類型。readonly表示這個(gè)屬性是只讀的,不能在組件內(nèi)部修改它的值。
 // propC: string | boolean
 // 表示這個(gè)屬性的類型是string或boolean,也就是說,它的值可以是string類型或boolean類型。
 // 這樣定義propC的好處是,當(dāng)propC被傳入組件時(shí),Vue會(huì)自動(dòng)根據(jù)它的類型進(jìn)行類型檢查,確保它的值符合預(yù)期。
?@Prop([String, Boolean]) readonly propC: string | boolean;  
}  

等同于:

export default {
  props: {
    propA: {
      type: Number,
    },
    propB: {
      default: 'default value',
    },
    propC: {
      type: [String, Boolean],
    },
  },
}

Watch

@Watch 裝飾器可以用于類中的方法上,用于監(jiān)聽指定的數(shù)據(jù)變化。當(dāng)被監(jiān)聽的數(shù)據(jù)發(fā)生變化時(shí),這個(gè)方法就會(huì)被調(diào)用,并且會(huì)傳入兩個(gè)參數(shù):新值和舊值。

例如,我們可以使用 @Watch 裝飾器來監(jiān)聽 child 這個(gè)屬性的變化,如下所示:

import { Vue, Component, Watch } from 'vue-property-decorator'
@Component
export default class YourComponent extends Vue {
  @Watch('child')
  onChildChanged(val: string, oldVal: string) {}
  @Watch('person', { immediate: true, deep: true })
  onPersonChanged1(val: Person, oldVal: Person) {}
  @Watch('person')
  onPersonChanged2(val: Person, oldVal: Person) {}
  @Watch('person')
  @Watch('child')
  onPersonAndChildChanged() {}
}

相當(dāng)于:

export default {
  watch: {
    child: [
      {
        handler: 'onChildChanged',
        immediate: false,
        deep: false,
      },
      {
        handler: 'onPersonAndChildChanged',
        immediate: false,
        deep: false,
      },
    ],
    person: [
      {
        handler: 'onPersonChanged1',
        immediate: true,
        deep: true,
      },
      {
        handler: 'onPersonChanged2',
        immediate: false,
        deep: false,
      },
      {
        handler: 'onPersonAndChildChanged',
        immediate: false,
        deep: false,
      },
    ],
  },
  methods: {
    onChildChanged(val, oldVal) {},
    onPersonChanged1(val, oldVal) {},
    onPersonChanged2(val, oldVal) {},
    onPersonAndChildChanged() {},
  },
}

Emit

當(dāng)一個(gè) Vue 組件需要與其它組件進(jìn)行通信時(shí),可以使用 emit 方法來觸發(fā)自定義事件。emit 方法接收兩個(gè)參數(shù):第一個(gè)參數(shù)是自定義事件的名稱,第二個(gè)參數(shù)是傳遞給父組件的數(shù)據(jù)。

父組件可以通過 v-on 指令監(jiān)聽這個(gè)自定義事件,并且在父組件中定義一個(gè)方法來處理這個(gè)事件。在 vue-property-decorator 中,可以使用 @Emit 裝飾器來定義組件的自定義事件。@Emit 裝飾器可以用于方法上,將這個(gè)方法標(biāo)記為組件的自定義事件。在這個(gè)方法中,可以通過 return 語句來返回需要傳遞給父組件的數(shù)據(jù)。例如:

import { Component, Vue, Emit } from 'vue-property-decorator';
@Component
export default class MyComponent extends Vue {
  @Emit()
  handleClick() {
    return 'hello';
  }
}

在這個(gè)例子中,我們定義了一個(gè)名為 handleClick 的方法,并且使用 @Emit 裝飾器將它標(biāo)記為組件的自定義事件。當(dāng)這個(gè)方法被調(diào)用時(shí),它會(huì)返回一個(gè)字符串 'hello',這個(gè)字符串會(huì)被傳遞給父組件。在父組件中,可以使用 v-on 指令來監(jiān)聽這個(gè)自定義事件,并且在父組件中定義一個(gè)方法來處理這個(gè)事件。例如:

<template>
  <div>
    <my-component @click="handleClick"></my-component>
  </div>
</template>
<script>
import MyComponent from './MyComponent.vue';
export default {
  components: {
    MyComponent,
  },
  methods: {
    handleClick(data) {
      console.log(data); // 輸出 'hello'
    },
  },
};
</script>

在這個(gè)例子中,我們?cè)诟附M件中使用 @click 指令來監(jiān)聽 MyComponent 組件的自定義事件,并且在父組件中定義了一個(gè)名為 handleClick 的方法來處理這個(gè)事件。當(dāng) MyComponent 組件觸發(fā)自定義事件時(shí),這個(gè)事件會(huì)被傳遞給父組件的 handleClick 方法,并且傳遞的數(shù)據(jù)是 'hello'。

更普遍的用法:

import { Vue, Component, Emit } from 'vue-property-decorator'
@Component
export default class YourComponent extends Vue {
  count = 0
  @Emit()
  addToCount(n: number) {
    this.count += n
  }
  @Emit('reset')
  resetCount() {
    this.count = 0
  }
  @Emit()
  returnValue() {
    return 10
  }
  @Emit()
  onInputChange(e) {
    return e.target.value
  }
  @Emit()
  promise() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(20)
      }, 0)
    })
  }
}

上述寫法等同于:

export default {
  data() {
    return {
      count: 0,
    }
  },
  methods: {
    addToCount(n) {
      this.count += n
      this.$emit('add-to-count', n)
    },
    resetCount() {
      this.count = 0
      this.$emit('reset')
    },
    returnValue() {
      this.$emit('return-value', 10)
    },
    onInputChange(e) {
      this.$emit('on-input-change', e.target.value, e)
    },
    promise() {
      const promise = new Promise((resolve) => {
        setTimeout(() => {
          resolve(20)
        }, 0)
      })
      promise.then((value) => {
        this.$emit('promise', value)
      })
    },
  },
}

Ref

@Ref 是用來獲取其他組件的 Ref 的,比如我們像如下使用,對(duì)于第一個(gè) @Ref() readonly anotherComponent!: AnotherComponent,我們的含義是這個(gè)ref是只讀的,并且是從this.$refsanotherComponet上拿到的值,而@Ref('aButton') readonly button!: HTMLButtonElement,則是制定了我們的this.$refs后面的訪問值為aButton。

例子:

import { Vue, Component, Ref } from 'vue-property-decorator'
import AnotherComponent from '@/path/to/another-component.vue'
@Component
export default class YourComponent extends Vue {
  @Ref() readonly anotherComponent!: AnotherComponent
  @Ref('aButton') readonly button!: HTMLButtonElement
}

相當(dāng)于

export default {
  computed() {
    anotherComponent: {
      cache: false,
      get() {
        return this.$refs.anotherComponent as AnotherComponent
      }
    },
    button: {
      cache: false,
      get() {
        return this.$refs.aButton as HTMLButtonElement
      }
    }
  }
}

以上就是Vue中裝飾器的使用示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Vue裝飾器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue-router+vuex addRoutes實(shí)現(xiàn)路由動(dòng)態(tài)加載及菜單動(dòng)態(tài)加載

    vue-router+vuex addRoutes實(shí)現(xiàn)路由動(dòng)態(tài)加載及菜單動(dòng)態(tài)加載

    本篇文章主要介紹了vue-router+vuex addRoutes實(shí)現(xiàn)路由動(dòng)態(tài)加載及菜單動(dòng)態(tài)加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • vue3實(shí)現(xiàn)自定義導(dǎo)航菜單的示例代碼

    vue3實(shí)現(xiàn)自定義導(dǎo)航菜單的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何使用vue3實(shí)現(xiàn)自定義導(dǎo)航菜單,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • vite.config.js或者vue.config.js配置方式

    vite.config.js或者vue.config.js配置方式

    這篇文章主要介紹了vite.config.js或者vue.config.js配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue3路由query參數(shù)實(shí)例詳解

    Vue3路由query參數(shù)實(shí)例詳解

    文章介紹了Vue3路由的query參數(shù),包括其定義、使用方法以及如何在組件中接收和展示query參數(shù),通過一個(gè)簡(jiǎn)單的實(shí)例,展示了如何在路由配置中添加query參數(shù),并在子組件中通過計(jì)算屬性獲取和顯示這些參數(shù),感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • vue3+ts+vite打包后靜態(tài)資源404無法加載js和css問題解決辦法

    vue3+ts+vite打包后靜態(tài)資源404無法加載js和css問題解決辦法

    這篇文章主要給大家介紹了關(guān)于vue3+ts+vite打包后靜態(tài)資源404無法加載js和css問題的解決辦法,文中通過代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-04-04
  • vue自定義指令實(shí)現(xiàn)一鍵復(fù)制功能

    vue自定義指令實(shí)現(xiàn)一鍵復(fù)制功能

    本文旨在記錄解決在工作中一鍵復(fù)制功能得需求,本文主要使用了Vue3+TypeScript+Ant-Design-Vue,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-11-11
  • vue-json-viewer展示JSON內(nèi)容實(shí)踐

    vue-json-viewer展示JSON內(nèi)容實(shí)踐

    這篇文章主要介紹了vue-json-viewer展示JSON內(nèi)容實(shí)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • Vue極簡(jiǎn)生成器?Vuepress的實(shí)現(xiàn)

    Vue極簡(jiǎn)生成器?Vuepress的實(shí)現(xiàn)

    本文主要介紹了Vue極簡(jiǎn)生成器?Vuepress的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2022-06-06
  • Vue3源碼解讀computed和watch

    Vue3源碼解讀computed和watch

    這篇文章主要為大家介紹了Vue3中的computed和watch源碼解讀分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • vue實(shí)現(xiàn)單選和多選功能

    vue實(shí)現(xiàn)單選和多選功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)單選和多選功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評(píng)論

宣威市| 克东县| 三明市| 廊坊市| 江川县| 江阴市| 汪清县| 临朐县| 平湖市| 阿克苏市| 乌鲁木齐市| 鄂托克旗| 石景山区| 永福县| 呼和浩特市| 全椒县| 津南区| 武强县| 壶关县| 河津市| 临猗县| 黑龙江省| 栾川县| 临夏县| 黔江区| 西充县| 濮阳县| 霍林郭勒市| 鹤岗市| 英吉沙县| 修文县| 荣成市| 黄石市| 会东县| 龙岩市| 遂溪县| 永福县| 灌南县| 高台县| 陕西省| 黄龙县|