Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare typescript interface Props in Vuejs class Component

I'm looking for to declare a typescript interface Props in Vuejs class Component like we can do with React Component.

It's look like this :

import {Component, Prop, Vue} from 'vue-property-decorator'

export class Props extends Vue
{
  classElement :string
}

@Component
export default class Menu extends Vue<Props>
{
    public props :Props;

    constructor(props)
    {
        super(props);
        console.log(props); // return undefined 
    }

    mounted()
    {
      console.log(this.props.classElement); // return undefined
    }
}

Is there a way to achieve this?

like image 455
BrownBe Avatar asked Jun 16 '18 18:06

BrownBe


People also ask

How do I add TS to Vue?

To let Typescript enters into your Vue components, 2 additions are mandatory in each component file: Adding the lang="ts" attribute to the script tag so that Typescript can recognized it.

Does vue2 support TypeScript?

Vue is written in TypeScript itself and provides first-class TypeScript support. All official Vue packages come with bundled type declarations that should work out-of-the-box.

Can I write TypeScript in Vue?

Now that Vue. js officially supports TypeScript, it's possible to create TypeScript projects from scratch using Vue CLI. However, you still need some third-party packages with custom decorators and features to create a true, complete TypeScript application.


Video Answer


3 Answers

Now you can use {type: Object as () => User} inside a Prop() decorator like this:

import Vue from 'vue'
import { Component, Prop } from 'vue-property-decorator'

import User from './models/user';

@Component()
export default class Menu extends Vue
{    

    @Prop({type: Object as () => User})
    public user!: User // notice the bang saying to compiler not to warn about no initial value

    mounted(){
      console.log(this.user);
    }

}
like image 89
Luis David Avatar answered Jan 02 '23 01:01

Luis David


Additionally it is now possible using the Typescript type PropType.

import Vue, { PropType } from 'vue'
import { Component, Prop } from 'vue-property-decorator'

import User from './models/user';

@Component()
export default class Menu extends Vue {    

    @Prop({type: Object as PropType<User>})
    public user!: User // notice the bang saying to compiler not to warn about no initial value

    mounted(){
      console.log(this.user);
    }

}
like image 44
Navid Mitchell Avatar answered Jan 02 '23 00:01

Navid Mitchell


Yes, all functionality of the basic javascript vue library can be used when using typescript. I suggest you use the offical class decorator.

Defining a prop can be done by simply adding it as a parameter to your class decorator like so:

@Component({
  props: {
    classElement: String
  }
})
export default class Menu extends Vue
{
    mounted()
    {
      console.log(this.classElement);
    }
}

Because component accepts an object you can define an interface for this object and pass this in instead as well.

Alternatively you can use the vue-property-decorator for a more angular-like syntax.

like image 34
Philip Feldmann Avatar answered Jan 02 '23 02:01

Philip Feldmann