AngularOne
AngularOne

Reputation: 2860

Is it possible in VueJS to give component prop more than one type and more than one default value?

I know I can give any prop its expected type and default value like this:

export default {
            name: "myComponent",
            props: {
                myProp: { type: String, default: 'any text' }
           }
    }

And I so in documentation I can give 2 types as array like this:

export default {
        name: "myComponent",
        props: {
            myProp: [String, Array]
            }
}

BUT I would expect I can also give any of these types a default value also (this is not working): Is it possible?

export default {
        name: "myComponent",
        props: {
          myProp: **[{type: String, default: ''}, {type: Array, default: []} ]** 
       }
}

Upvotes: 0

Views: 2712

Answers (1)

ittus
ittus

Reputation: 22393

I think it's impossible, for example if you have

export default {
    name: "myComponent",
    props: {
      myProp: [
        {type: String, default: ''}, 
        {type: Array, default: []}
      ] 
   }
}

then when you write your component without passing myProp:

<my-component />

Then my-component doesn't know it should take value from default String or default Array definition.

Upvotes: 2

Related Questions