Home > front end >  Vue3 / Typescript define an property of Array(string)
Vue3 / Typescript define an property of Array(string)

Time:02-01

I have a property on a component that worked in JavaScript, but have problems with Typescript:

props: {
   keep_open: {
     type: Array,
     default: () => [],
   }
}

This definition doesn't fail here, but later in the code:

let keepOpen: Array<string> = [this.data.popupId];
this.keep_open.forEach((id) => {
   keepOpen.push(id);
});

The id from Keep_Open is type undefined and will not push into KeepOpen.

How do I fix this, either in the later code or in the definition of the properties?

CodePudding user response:

as mentionedin the vue documentation you should use PropType from vue, so it should be something like

type: Array as PropType<string[]>

And as good practice ,don't change value of a props inside a composant , emit an event and the owner of the props should update it.

  •  Tags:  
  • Related