Refer to the link below, we can use class based vue component which is written in TypeScript.
What is the correct way to use these custom components?
For example, the Es5 code below defines a component which can be used in other components' template like <my-component></my-component>
because we put a name 'my-component'
as a parameter to Vue.component
method. How to achieve this in typescript?
Vue.component('my-component', {
template: '<span>{{ message }}</span>',
data: {
message: 'hello'
}
})
Vue.component('parent-component', {
template: '<my-component></my-component>'
})
https://vuejs.org/v2/guide/typescript.html#Class-Style-Vue-Components https://alligator.io/vuejs/typescript-class-components/
Class-Based Vue Component What is the tag name of this component which can be used in other components' template string?
import Vue from 'vue'
import Component from 'vue-class-component'
// The @Component decorator indicates the class is a Vue component
@Component({
// All component options are allowed in here
template: '<button @click="onClick">Click!</button>'
})
export default class MyComponent extends Vue {
// Initial data can be declared as instance properties
message: string = 'Hello!'
// Component methods can be declared as instance methods
onClick (): void {
window.alert(this.message)
}
}
You can still register components like usual as if you weren't using Typescript:
Since the code above is exporting the component from a module, you probably shouldn't register it globally (unless it is a common component). The best way would be to import the component into the modules of other components which will use it: