0

In typescript, i have seen the prop is initialized like this below in vue component.

@Prop({ type: Object }) tabDetails: tabDetailsTypes

This tabDetailsTypes is like :

export interface tabDetailsTypes {
  label: string
  name: string
  count: number | string

Same thing i want to create prop in vue component by using JavaScript like this.

props: {
  serverUrls: {
   required: true,
            type: Object,
            default: () => {
             label: '',
             name: '',
             count: 1
     }
  }
}

It is correct syntax or what is the correct syntax for this?

1
  • To return and inline Object with ES6 arrows functions, you should escape the braces with parenthesis like: default: () => ({ ... }). Another way is using return statement: default: () => { return { ... } }; Commented Mar 16, 2021 at 15:22

1 Answer 1

1

If you mean by that to create a prop that the behavior follows Typescript's behavior where your IDE would complaint if you defined something that is unavailable or different by the type on the given properties, or if you want it to serve the available option through intellisense, I don't think you can do that on Javascript.

And as you edited the question, if you want to set the default value, you don't need the required property. Remove that, add braces around the object and all is good.

props: {
  serverUrls: {
    type: Object,
    default: () => ({
      label: 'the default label',
      name: 'the default name',
      id: 1,
    }),
  }
},
mounted() {
  console.log(this.serverUrls)
},

Here is the working example

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.