The second approach of using mounted is preferred over the rest of the approaches. The only change I would suggest is the use of created hook instead of mounted:
export default class Test extends Vue {
protected msg: string = '';
created() {
this.msg = 'Today\'s date ' + moment().format('YYYY/MM/DD');
}
}
Generally, for simple properties, you can directly assign a value at a time of declaration. Use created when your assignment is not simple.
Also, we don't really use constructors when writing the class-based component. The reason behind is that essentially Vue.js components are object-based. The @Component decorator is eventually making the component behave like object-based.
Further, if you look at Vue.js component lifecycle methods, then there is no place for a constructor. The initial methods are beforeCreate -> data -> created -> mounted and so on. How can a beforeCreate execute without an actual call to the constructor? That make is really weird to reason about.
Note 1: For version 3 of Vue.js, official class-based components are proposed. Thus, this might change in the near future.
Note 2: TypeScript will move msg declaration to the constructor after compilation and Vue.js seems to work well with it. But it is still unspecified and better be avoided.
» npm install vue-property-decorator
» npm install vue-facing-decorator