Files
hcframe-doc/docs/02.前端/03.代码示例/02.新增页面示例.md
2021-02-03 17:01:33 +08:00

7.8 KiB
Raw Permalink Blame History

title, date, permalink
title date permalink
新增页面示例 2021-02-03 09:26:32 /pages/5003ef/

新增页面示例

::: tip 此处提供使用通用模板的页面使用方法,若要添加常规页面请参考 Vue 官网提供的说明。 使用此方法前需要已经在配置表配置好需要调用的单表信息。 :::

  1. 首先在 src>api>views 中新建文件夹 user-example ,进入文件夹新建文件 user-example.ts,写入以下信息

    import { Component, Prop, Vue } from 'vue-property-decorator'
    import { mixins } from 'vue-class-component'
    import Common from '@/api/mixins/Common'
    
    @Component({
      name: 'userExample'
    })
    export default class extends mixins(Common) {
    }
    
  2. 添加默认配置信息,需要使用者根据注释及页面需求更改配置

    import { Component, Prop, Vue } from 'vue-property-decorator'
    import { mixins } from 'vue-class-component'
    import Common from '@/api/mixins/Common'
    import StringUtils from '@/common/StringUtils'
    
    @Component({
      name: 'userExample'
    })
    export default class extends mixins(Common) {
      // 表格中 '操作'列的宽度
      private operationWidth = 200
    
      async created() {
        // 设置默认排序
        this.sort = 'UPDATE_TIME'
        this.order = 'DESC'
        // 设置表别名
        this.tableAlias = 'user'
        // 获取表头及表单信息
        await this.acquireTableInfo()
        // 获取基表信息格式化下拉框及表格数据
        await this.getBaseTableInfo()
        // 获取列表信息
        this.acquireTable()
        // 设置默认校验构造
        this.formRules = {
          PASSWORD: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
          USERNAME: [{ required: true, message: '用户名不能为空', trigger: 'blur' }]
        }
        // 设置默认禁用字段
        this.disableField = ['CREATE_TIME', 'UPDATE_TIME']
      }
    
      // 编辑表格新增行
      addRow() {
        const obj: any = {
          index: this.editList.length,
          edit: true
        }
        this.editList.push(obj)
      }
    
      // 可编辑表格select
      editTableSelectionChange(val: any) {
        this.editTableSelectList = val
      }
    
      // 表格校验规则
      tableRules(val: any) {
        const { rows, rules, callback } = val
        let message = ''
        Object.getOwnPropertyNames(rules).forEach(function(key) {
          if (key !== '__ob__') {
            if (StringUtils.isEmpty(rows[key])) {
              message += rules[key][0].message
              message += ','
            }
          }
        })
        callback(message)
      }
    }
    
  3. 在路径 src>views 新建文件夹 user-example , 进入 user-example 文件夹,新建文件 user-example.vue并写入以下信息scss请根据页面设计自行更改或者复制通用页面示例中的scss这里先不再显示。

    <template>
      <div class="dashboard-editor-container">
      </div>
    </template>
    
    <script lang="ts" src="../../api/views/user-example/user-example.ts"></script>
    
    <style lang="scss">
    
    </style>
    
    
  4. src>router>index.ts 中增加 user-example.vue 的路由指向。

    export const asyncRoutes: RouteConfig[] = [
    ....,
    {
      path: '/userExample',
      component: Layout,
      children: [
        {
          path: 'userExample',
          component: () => import(/* webpackChunkName: "icons" */ '@/views/user-example/userExample.vue'),
          name: 'userExample',
          meta: {
            title: 'userExample',
            icon: 'example',
            noCache: true
          }
        }
      ]
    },
    ....
    ]
    

    此时可以看到目录上多了刚刚我们添加的路由

    router

  5. 此时目录还未显示中文由于框架使用i18n插件此时我们需要进入语言目录下进行设置进入 src>lang>zh.ts 中设置显示中文。

    ::: warning 注意 此处设置需要添加到router对象下且key值需要与路由中的title设置一致 :::

    export default {
      route: {
        userExample:'用户示例',
        ...
      }
    }   
    

    然后我们就可以看到显示目录变成中文。

    router2

  6. 然后我们添加通用组件到ts文件及页面中。

    首先引入组件到ts文件中

    import { Component, Prop, Vue } from 'vue-property-decorator'
    import { mixins } from 'vue-class-component'
    import Common from '@/api/mixins/Common'
    import StringUtils from '@/common/StringUtils'
    import tableHeadCo from '@/components/CommonCo/tableHeadCo.vue'
    @Component({
      name: 'commonExample',
      components: {
        tableHeadCo,
      }
    })
    export default class extends mixins(Common) {
      // 表格中 '操作'列的宽度
      private operationWidth = 200
    
      async created() {
        // 设置默认排序
        this.sort = 'UPDATE_TIME'
        this.order = 'DESC'
        // 设置表别名
        this.tableAlias = 'user'
        // 获取表头及表单信息
        await this.acquireTableInfo()
        // 获取基表信息格式化下拉框及表格数据
        await this.getBaseTableInfo()
        // 获取列表信息
        this.acquireTable()
        // 设置默认校验构造
        this.formRules = {
          PASSWORD: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
          USERNAME: [{ required: true, message: '用户名不能为空', trigger: 'blur' }]
        }
        // 设置默认禁用字段
        this.disableField = ['CREATE_TIME', 'UPDATE_TIME']
      }
    
      // 编辑表格新增行
      addRow() {
        const obj: any = {
          index: this.editList.length,
          edit: true
        }
        this.editList.push(obj)
      }
    
      // 可编辑表格select
      editTableSelectionChange(val: any) {
        this.editTableSelectList = val
      }
    
      // 表格校验规则
      tableRules(val: any) {
        const { rows, rules, callback } = val
        let message = ''
        Object.getOwnPropertyNames(rules).forEach(function(key) {
          if (key !== '__ob__') {
            if (StringUtils.isEmpty(rows[key])) {
              message += rules[key][0].message
              message += ','
            }
          }
        })
        callback(message)
      }
    }
    
    

    然后再vue实例中引入组件

    <template>
      <div class="dashboard-editor-container">
        <table-head-co
        ref="tableCo"
        :check-box-visible="true"
        :formatter="tableFomatter"
        :form-list="tableList"
        :table-heads="tableHead"
        :loading="loading"
        :slot-visible="true"
        :width="operationWidth"
        @handle-sort-change="handleSortChange"
        @handle-selection-change="handleSelectionChange"
        />
      </div>
    </template>
    
    <script lang="ts" src="../../api/views/user-example/user-example.ts"></script>
    
    <style lang="scss">
    
    </style>
    

    然后我们刷新页面,就可以看到刚刚引入的组件了。

    table

  7. 页面实例讲解完毕,最后,请按照使用者按照需要进行页面相关的开发或引入其他组件进行开发。