programing

계산된 속성에 대해 형식 설명/Vuej가 컴파일되지 않음

prostudy 2022. 8. 25. 21:47
반응형

계산된 속성에 대해 형식 설명/Vuej가 컴파일되지 않음

나는 타자기본과 Vuejs를 배우기 시작했다.

계산된 all Checked()에서 데이터 내의 계정 속성에 접근할 수 없는 이유를 설명해 주실 수 있습니까?

import * as Vue from "vue";

declare var accounts: any[];    

var app = new Vue({
    el: '#vueowner',
    data: {
        accounts: accounts,
        hasAccount: this.accounts.length > 0,
        checkedAccounts: []
    },
    computed: {
        allChecked() {
            return this.accounts.length === this.checkedAccounts.length;
        }
    }
})

이 에러가 있습니다.

ERROR in index.ts
(25,25): error TS2339: Property 'accounts' does not exist on type 'Vue'.

ERROR in index.ts
(25,50): error TS2339: Property 'checkedAccounts' does not exist on type 'Vue'.

TypeScript는 특정 메서드의 유형을 추론하는 데 어려움이 있기 때문에 반환 유형에 주석을 달아야 할 것 같습니다.

그래서 대신

allChecked() {
   return this.accounts.length === this.checkedAccounts.length;
}

이거 먹어봐

allChecked(): boolean {
   return this.accounts.length === this.checkedAccounts.length;
}

고객님의 고객명allChecked메서드this키워드가 전달하고 있는 옵션오브젝트를 참조하고 있지 않습니다.Vue컨스트럭터입니다만,Vue클래스 자체입니다.

다음을 확장하는 클래스를 만들어야 합니다.Vue원래 클래스에 추가할 속성을 지정합니다.

import * as Vue from "vue";

class MyVue extends Vue {
    accounts: any[];
    checkedAccounts: any[];
}

const app = new MyVue({
    // (...)
})

Vue.js와 Typescript의 사용에 대한 자세한 내용은 다음 페이지를 참조하십시오.https://vuejs.org/v2/guide/typescript.html

이 문서는 VuejS with Typescript로 시작하는 것이 좋습니다.

https://johnpapa.net/vue-typescript/

<template>

</template>
<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';

  export default class App extends Vue {
    public accounts: any = []; 
    public checkedAccounts: any = [];
    public created(): void {
        this.accounts = [];
        this.checkedAccounts= [];
    }
   public allChecked(): boolean {
    return this.accounts.length === this.checkedAccounts.length;
   }
  }
</script>

언급URL : https://stackoverflow.com/questions/44654029/typescript-vuejs-not-compiling-for-computed-property

반응형