Vue-class-component: Static class properties as vue options

Created on 10 May 2017  路  4Comments  路  Source: vuejs/vue-class-component

If you want to pass plain options to the vue component, you need to do it like this:

import Vue from 'vue'
import Component from 'vue-class-component'
import Hello from './components/Hello'

@Component({
    components: {
        Hello
    }
})
export default class App extends Vue {
}

But according to #2 it was possible at some time by using this:

import Vue from 'vue'
import Component from 'vue-class-component'
import Hello from './components/Hello'

@Component
export default class App extends Vue {
    static components = {
        Hello
    }
}

Is there any chance we can get the 2nd behavior working again?

Most helpful comment

@Component({
  component: {} // error, should be components
})
class T extends Vue {}

@Component
class T extends Vue {
  static copmonent = {} // no error, cannot detect typo
}

All 4 comments

Static properties cannot be type checked. So current API has better type safety.

If you want using static properties, you can wrap your own decorator. It's just a plain function.

What do you mean by that? One can declare types on static properties and tsc checks them. Would be nice if you can give me a link for further reading since google didnt help much regarding that case.

edit: oh you mean, ts cant infer the required types for the config options as static properties like components from the fact that the class is extending Vue?

@Component({
  component: {} // error, should be components
})
class T extends Vue {}

@Component
class T extends Vue {
  static copmonent = {} // no error, cannot detect typo
}

As @HerringtonDarkholme explained, there is an advantage to pass component options as a decorator's argument than static properties.

Was this page helpful?
0 / 5 - 0 ratings