0

I do the same as in the following 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'
        }
    }

});

Updated the original question with the full component code, I I pass down a prop the component is rendered as expected

1

1 Answer 1

1

The syntax is OK, I passed down an empty array and props default value is used only if the value is null or undefined.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.