第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從觀察者內(nèi)部的 Vue 類組件裝飾器訪問(wèn)方法

從觀察者內(nèi)部的 Vue 類組件裝飾器訪問(wèn)方法

精慕HU 2023-05-18 11:09:20
我將 Vue 與 Typscript 和 Vue 類組件一起使用。我需要做的是從 @Component 裝飾器內(nèi)的觀察者內(nèi)部訪問(wèn)我的(Vue-)類的方法。我知道可以使用 訪問(wèn)組件的數(shù)據(jù)this.$data,但是方法呢?我的代碼在運(yùn)行時(shí)運(yùn)行,但在 vscode 中產(chǎn)生編譯時(shí)錯(cuò)誤和錯(cuò)誤(“屬性‘clearInfo’在‘Vue’類型上不存在。”);@Component({? watch: {? ? firstMesh(newMesh) {? ? ? if (newMesh === undefined) this.clearInfo(1);? ?// this produces the errors? ? ? else this.showMeshInfo(newMesh, 1);? ? },? ? secondMesh(newMesh) {? ? ? if (newMesh === undefined) this.clearInfo(2);? ? ? else this.showMeshInfo(newMesh, 2);? ? },? },})export default class Info extends Vue {? clearInfo(whichMesh : number) {...? }? showMeshInfo(mesh : any, index : number) {? ? ....? }}
查看完整描述

1 回答

?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊

你有兩個(gè)選擇:


在類本身中定義手表

// first you need to install vue-property-decorators with npm i -S vue-property-decorator

// This library has a lot of useful decorators. You can read more about it here: https://github.com/kaorun343/vue-property-decorator


import { Vue, Component, Watch } from 'vue-property-decorator'



@Component

export default class Info extends Vue {

? @Watch('firstMesh')

? public watchFirstMesh(newValue) {

? ? ?// ... do whatever you need to do with the newValue here

? }


? @Watch('secondMesh')

? public watchSecondMesh(newValue) {

? ? ?// ... do whatever you need to do with the newValue here

? }

}

在 @Component 的選項(xiàng)部分定義手表和方法

@Component({

? watch: {

? ? firstMesh(newMesh) {

? ? ? if (newMesh === undefined) this.clearInfo(1);? ?// this produces the errors

? ? ? else this.showMeshInfo(newMesh, 1);

? ? },

? ? secondMesh(newMesh) {

? ? ? if (newMesh === undefined) this.clearInfo(2);

? ? ? else this.showMeshInfo(newMesh, 2);

? ? },

? },

? methods: {

? ?clearInfo(whichMesh : number) {

? ? ?...

? ?},

? ?showMeshInfo(mesh : any, index : number) {

? ? ?....

? ?}? ?

? }

})

export default class Info extends Vue {

? // Now you need to tell to typescript that there will be a method inside this class called clearInfo and showMeshInfo

? public clearInfo!: (wichMesh: number) => void;

? public showMeshInfo!: (mesh: any, index: number) => void;

}

解釋

  1. 可以在我留下的鏈接上閱讀解釋

  2. 由于您是在裝飾器中定義選項(xiàng),因此@Component({...})這將在實(shí)例化類的上下文中可用。Typescript 不知道什么是可用的(我們希望它是那么聰明)。你必須告訴它,這就是為什么我們有這個(gè)public clearInfo!: (wichMesh: number) => void;角色。如果你不知道這個(gè)語(yǔ)法是什么意思,我會(huì)簡(jiǎn)單地解釋一下,并在最后留下一個(gè)鏈接:

public clearInfo!: (wichMesh: number) => void;


the ! part is called the non-null assertion operator. It is a way to tell the compiler "this expression cannot be null or undefined here, so don't complain about the possibility of it being null or undefined."


The (wichMesh: number) => void; is the function signature. Basically it says: this will be a function that receives as the first argument a number (whichMesh) and returns void (=> void)


查看完整回答
反對(duì) 回復(fù) 2023-05-18
  • 1 回答
  • 0 關(guān)注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)