下面给大家提供了一个实例完整的ts示例:

    • 包含前面讲到的所有实例选项,以及router导航守卫周期、vuex使用等
    • 包含各个选项、属性的顺序排列。结合了部分官方的推荐 Vue 风格指南 - 组件/实例的选项的顺序
    • 包含各属性的public、private等修饰符的使用

    这里例子比较简单,仅是为了展示大部分的属性,若要更细致的了解各实例选项的写法,可以参考前面的文章。

    1. // TodoApp.vue
    2. <template>
    3. <div class="todo-app">
    4. <div ref="refTitle">
    5. this is title {{ UserModule.allName }} - {{ userAge }}
    6. </div>
    7. <TodoItem ref="refTodoItem" />
    8. </div>
    9. </template>
    10. <script lang="ts">
    11. import { Component, Mixins, Prop, Emit, Watch, Provide, Inject, Ref } from 'vue-property-decorator'
    12. import { Route, NavigationGuardNext } from 'vue-router'
    13. import TodoItem from '@/components/TodoItem.vue'
    14. import colorDirective from '@/directive/colorDirective'
    15. import FormMixins from '@/mixins/FormMixins'
    16. import UserMixins from '@/mixins/UserMixins'
    17. import { UserModule } from '@/store/modules/user'
    18. type gender = '男' | '女'
    19. @Component({
    20. components: { TodoItem },
    21. directives: {
    22. color: colorDirective,
    23. },
    24. filters: {
    25. formatUserName(val: string) {
    26. return `姓名:${val}`
    27. },
    28. },
    29. })
    30. export default class TodoApp extends Mixins(FormMixins, UserMixins) {
    31. // ref
    32. @Ref('refTitle') readonly refTitle!: HTMLDivElement // DOM
    33. @Ref('refTodoItem') readonly refTodoItem!: TodoItem // Vue Component
    34. // provide
    35. @Provide('department') public department = '销售部'
    36. // inject
    37. @Inject('userId') public injectUserId = 1
    38. // Model
    39. @Model('user-age')
    40. readonly userAge!: number
    41. // prop
    42. @Prop({ type: [String, Number], default: '' })
    43. readonly userId!: string | number
    44. // emit
    45. @Emit('user-age')
    46. public emitAgeChange(age: number) {
    47. return age
    48. }
    49. @Emit('add-to-count')
    50. public emitAddToCount(val: number) {
    51. return val
    52. }
    53. // Vuex store module
    54. public readonly UserModule = UserModule
    55. // data
    56. public userName: string = '韩梅梅'
    57. public gender: gender = '女'
    58. // computed
    59. public get userInfo(): string {
    60. return `性别:${this.gender},姓名:${this.userInfo}`
    61. }
    62. // watch
    63. @Watch('gender', { immediate: true, deep: true })
    64. private onGenderChange(newVal: gender, oldVal: gender) {
    65. console.log('新:', newVal)
    66. console.log('旧:', oldVal)
    67. }
    68. // Hook: 生命周期
    69. private mounted() {
    70. console.log('mounted')
    71. }
    72. private beforeDestroy() {
    73. console.log('beforeDestroy')
    74. }
    75. // Hook: 导航守卫
    76. private beforeRouteEnter(_to: Route, _from: Route, next: NavigationGuardNext) {
    77. next()
    78. }
    79. private beforeRouteLeave(_to: Route, _from: Route, next: NavigationGuardNext) {
    80. next()
    81. }
    82. // methods
    83. public changeUserGender(gender: gender) {
    84. this.gender = gender
    85. }
    86. }
    87. </script>