Vue 2 - 如何在 props 中设置默认数组类型 [英] Vue 2 - How to set default type of array in props

查看:90
本文介绍了Vue 2 - 如何在 props 中设置默认数组类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的 Vue 组件,它将一组对象作为道具.我经常使用道具验证,特别是对于默认"值功能.

I have my Vue component, which is taking an array of objects as a prop. I often use prop validation, especially for 'default' value feature.

在这种情况下,我有:

props: {
    items: Array
}

但我希望它像 Typescript 或 React 一样:

but I'd like it to have like in Typescript or React:

props: {
    items: Array.of(
        {key: {type: String, default: 'myText'}}
        )
}

有可能实现吗?否则我需要使用计算数据作为地图来设置默认值

Is it possible to achieve? Otherwise I need to use computed data as map just to set the defaults

推荐答案

我创建了示例:jsFiddle,这可能对你有帮助,是的......你可以将默认值作为数组返回:

I created example: jsFiddle, that might can help you, and yes... you can return the default value as a array:

ES6

props: {
    items: {
        type: Array,
        default: () => []
    }
}

这篇关于Vue 2 - 如何在 props 中设置默认数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆