【问题标题】:Vue Array prop default value doesn't workVue Array prop 默认值不起作用
【发布时间】:2021-04-24 15:04:30
【问题描述】:

我在下面的Vue 2 - How to set default type of array in props做同样的事情

Vue.component("Test-Attribute", {
    props: {
        attributes: {
            type: Array,
            required: false,
            // Object or array defaults must be returned from
            // a factory function
            default: function () {
                return [
                    {attributeName: 'One', multiselect: 0},
                    {attributeName: 'Two', multiselect: 1},
                    {attributeName: 'Three', multiselect: 0},
                    {attributeName: 'Four', multiselect: 1},
                ]
            }
        }
    },
    template: `
      <div>
      <component
          v-for="(attribute,index) in attributes"
          :key="index"
          :name="attribute.attributeName"
          :is="getAttributeType(attribute)"
      >
        {{ attribute.attributeName + ':' }}
      </component>
      </div>
    `,
    created() {
        console.log(this.attributes)
    },
    methods: {
        getAttributeType: function (attribute) {
            return parseInt(attribute.multiselect) === 1 ? 'MultiAttribute' : 'SingleAttribute'
        }
    }

});

用完整的组件代码更新了原始问题,我传递了一个组件按预期呈现的道具

【问题讨论】:

标签: javascript vue.js vuejs2


【解决方案1】:

语法没问题,我传了一个空数组,props只有在值为null或未定义时才使用默认值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 2020-08-22
    • 2016-04-20
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多