我们挑选了常用的@Component、@Prop、@PropSync、@Watch来举例,全部装饰器使用,请移步 vue-class-componentvue-property-decorator

@Component

修饰符注明了此类为一个 Vue 组件

  1. @Component({
  2. name: 'UserList',
  3. components: {
  4. Pagination
  5. }
  6. })
  7. export default class extends Vue {}

@Prop

单向数据流

  1. @Prop({ type: Number, required: true }) private readonly id!: number

@PropSync

双向数据流

  1. @PropSync('dialogVisible', { type: Boolean, default: false })
  2. syncDialogVisible!: boolean

@Watch

监控数据

  1. @Watch('listQueryData', { deep: true, immediate: true })
  2. private watchListQueryData(newVal: string) {
  3. if (newVal == '') {
  4. return
  5. }
  6. this.getList()
  7. }
  8. @Watch('listQuery.pagesize', { immediate: true })
  9. private watchListQuerypagesize() {
  10. this.listQuery.page = 1
  11. }

代码示例

  1. <template>
  2. <div class="container">
  3. <div class="button-area">
  4. <el-button type="primary" size="small" @click="handleOpenAddDialog"
  5. ><svg class="icon" aria-hidden="true">
  6. <use xlink:href="#icon-btn_add" /></svg
  7. >新增</el-button
  8. >
  9. </div>
  10. <el-table
  11. class="data-list"
  12. v-loading="listLoading"
  13. :data="listData.items"
  14. border
  15. :header-cell-style="{ textAlign: 'center' }"
  16. :cell-style="{ textAlign: 'center' }"
  17. >
  18. <el-table-column prop="id" label="编号"></el-table-column>
  19. <el-table-column prop="account" label="账号"></el-table-column>
  20. <el-table-column prop="fullName" label="姓名"></el-table-column>
  21. <el-table-column prop="mobile" label="联系电话"></el-table-column>
  22. <el-table-column prop="email" label="邮件"></el-table-column>
  23. <el-table-column prop="create_date" label="注册时间"></el-table-column>
  24. <el-table-column label="操作">
  25. <template slot-scope="scope">
  26. <el-button type="text" size="mini" @click="handleOpenEditDialog"
  27. ><svg class="icon" aria-hidden="true">
  28. <use xlink:href="#icon-btn_edit" /></svg
  29. >编辑</el-button
  30. >
  31. <el-button
  32. type="text"
  33. size="mini"
  34. @click="handleOpenRemoveConfirm(scope.row.id)"
  35. ><svg class="icon" aria-hidden="true">
  36. <use xlink:href="#icon-btn_del" /></svg
  37. >删除</el-button
  38. >
  39. <el-button type="text" size="mini" @click="handleOpenDetailDialog"
  40. ><svg class="icon" aria-hidden="true">
  41. <use xlink:href="#icon-btn_chakan" /></svg
  42. >查看</el-button
  43. >
  44. </template>
  45. </el-table-column>
  46. </el-table>
  47. <common-pagination
  48. v-show="listData.counts > 0"
  49. :total="listData.counts"
  50. :page.sync="listQuery.page"
  51. :limit.sync="listQuery.pagesize"
  52. @pagination="getList"
  53. />
  54. <!-- 用户添加对话框 -->
  55. <user-add-dialog
  56. :dialogVisible.sync="addDialogVisible"
  57. @refreshList="getList"
  58. />
  59. <!-- 用户编辑对话框 -->
  60. <user-edit-dialog
  61. :dialogVisible.sync="editDialogVisible"
  62. :id="id"
  63. @refreshList="getList"
  64. />
  65. <!-- 用户查看对话框 -->
  66. <user-detail-dialog :dialogVisible.sync="detailDialogVisible" :id="id" />
  67. </div>
  68. </template>
  69. <script lang="ts">
  70. import { Component, Watch } from 'vue-property-decorator'
  71. import MixinTools from '@/utils/mixins.vue'
  72. import CommonPagination from '@/components/common-pagination/index.vue'
  73. import UserAddDialog from './components/user-add-dialog.vue'
  74. import UserEditDialog from './components/user-edit-dialog.vue'
  75. import UserDetailDialog from './components/user-detail-dialog.vue'
  76. import { list, remove } from '@/api/users'
  77. import { Page } from '@/entity/page'
  78. import { User } from '@/entity/user'
  79. import { mixins } from 'vue-class-component'
  80. @Component({
  81. name: 'UserList',
  82. components: {
  83. CommonPagination,
  84. UserAddDialog,
  85. UserEditDialog,
  86. UserDetailDialog
  87. }
  88. })
  89. export default class extends mixins(MixinTools) {
  90. private listData: Page<User> = {
  91. counts: 0,
  92. page: 0,
  93. items: [],
  94. pages: 0,
  95. pagesize: 0
  96. }
  97. private listLoading = false
  98. private listQuery = {
  99. page: 1,
  100. pagesize: 10
  101. }
  102. private addDialogVisible = false
  103. private editDialogVisible = false
  104. private detailDialogVisible = false
  105. private id = 0
  106. @Watch('listQueryData', { deep: true, immediate: true })
  107. private watchListQueryData(newVal: string) {
  108. if (newVal == '') {
  109. return
  110. }
  111. this.getList()
  112. }
  113. @Watch('listQuery.pagesize', { immediate: true })
  114. private watchListQuerypagesize() {
  115. this.listQuery.page = 1
  116. }
  117. private async getList() {
  118. this.listLoading = true
  119. const { data } = await list(this.listQuery)
  120. this.listData = data
  121. this.listLoading = false
  122. }
  123. private handleOpenAddDialog() {
  124. this.addDialogVisible = true
  125. }
  126. private handleOpenEditDialog() {
  127. this.editDialogVisible = true
  128. }
  129. private async handleOpenRemoveConfirm(id: number) {
  130. try {
  131. await this.showDeleteConfirm()
  132. await remove(id)
  133. this.showMessage('操作成功')
  134. } catch (err) {
  135. console.log('err: ' + err)
  136. }
  137. }
  138. private handleOpenDetailDialog() {
  139. this.detailDialogVisible = true
  140. }
  141. }
  142. </script>
  143. <style lang="scss" scoped>
  144. .container {
  145. width: 1366px;
  146. margin: 0 auto;
  147. .button-area {
  148. text-align: right;
  149. }
  150. .data-list {
  151. margin-top: 20px;
  152. }
  153. }
  154. </style>