javascript - Vue.js 中的嵌套属性类型

标签 javascript vue.js

有什么方法可以跟踪 Vue 上的嵌套属性类型吗?

我有这个 Prop 设置:

// ...
props: {
    navigation: {
        type: [Array],
        default: () => {
            return [
                { type: String, icon: "home" },
                { type: String, icon: "star" }
            ]
        }
    }
}

现在我仅对第一级数组进行验证,但其中没有任何反应。我可以检查类型级别之一内的类型吗?谢谢帮助。

最佳答案

我认为不可能使用 type 属性直接键入对象数组,如 Vue prop validation说:

The type can be one of the following native constructors:

  • String
  • Number
  • Boolean
  • Function
  • Object
  • Array
  • Symbol

话虽如此,您似乎可以定义一个验证器函数,例如:

props: {
    navigation: {
        validator: function (value) {
            if (!(value instanceof Array)) {
                return false;
            }
            for(var i = 0; i < value.length; ++i) {
                if (typeof value.icon !== "string") {
                    return false;
                }
            }
            return true;
        },
        default: () => {
            return [
                {icon: "home" },
                {icon: "star" }
            ]
        }
    }
}

关于javascript - Vue.js 中的嵌套属性类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43913556/

相关文章:

vue.js - 如何从 vuetify 上的多个日期选择器获取日期?

javascript - 如何基于 JSON 对象使 Vuetify 中的 @click 动态化

javascript - Ractive JS 并使用 LaTeX 输入更新 MathJax?

javascript - Angular JS 中的 Jquery 网络摄像头集成

javascript - Angular 6 'app-action-button' 不是已知元素

javascript - D3、Vue 和 typescript : how to fix common errors for "Expected 1 arguments, but got 0" and "Object is possibly ' null'

javascript - 如何删除 Stripe 的 iframe?

javascript - 如何使用 Vue Slider 控制元素

javascript - 如何将组合框选定的值和单选按钮列表选中的值从 View 传递到 Controller

javascript - 使用 AJAX 不使用 jQuery 在索引页和登录页之间切换 div 内容