【问题标题】:vue-property-decorator @Prop How to use interface to define an arrayvue-property-decorator @Prop 如何使用接口定义数组
【发布时间】:2019-01-29 09:14:03
【问题描述】:
'use strict'

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

interface ITabs {
    name: string
    value: string
}

export default class extends Vue {
    @Prop({
        type: Array,
        default() {
            return []
        }
    })
    public tabs!: ITabs[] // TS2416
}

TS2416:“默认”类型中的属性“选项卡”不可分配给 基类型“object & Record & Vue”中的相同属性。类型 'ITabs[]' 不能分配给类型 '{ name: "";价值: ””; }[] | 不明确的'。类型 'ITabs[]' 不可分配给类型 '{ name: ""; 价值: ””; }[]'。类型 'ITabs' 不可分配给类型 '{ name: ""; 价值: ””; }'。属性“名称”的类型不兼容。类型 'string' 不可分配给类型 '""'。

我想在 Prop Array 中使用接口,我该怎么办?

【问题讨论】:

    标签: typescript vue.js


    【解决方案1】:

    如果有人像我一样挣扎,这对我有用:

    @Prop({ default: () => {
            return [ ]
        }
        }) private tabs!: ITabs[];
    

    【讨论】:

    • 这就像一个魅力!真的很感激,但现在我想知道为什么这种用法有效......
    猜你喜欢
    • 2019-12-05
    • 2018-02-20
    • 2019-10-04
    • 2019-12-17
    • 2018-05-01
    • 2020-03-10
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多