前端組件化設計——佈局、邏輯、視圖

1、拆分頁面:將一個頁面拆分紅幾個部分,如:父子包裹、左右或上下佈局canvas

<!-- 上下佈局 -->
<template>
  <el-card
    style="background: #fff;min-height: 800px"
    shadow="never">
    <div
      slot="header"
      style="height: 28px">
      <!-- 標題 -->
      <span>xxxx列表<span/>
    </div>
    <!--內容-->
    <div>
      <featureTable :parameters="parameters"/>
    </div>
  </el-card>
</template>

2、表格操做部分,屬於中間件——處理複雜邏輯、數據轉換數組

1.純展現的表格app

<template>
  <el-card id="activityManage">
    <div slot="header">
      <el-button
        type="primary"
        @click="addRowDialog"
      >新建</el-button>
    </div>
    <el-row >
      <el-col>
        <CommonTable
          :table-data="tableData"
          :table-column="tableColumn"
          height="680"
          :loading="listLoading"
        >
          <el-table-column
            type="index"
            label="序號"
            align="center"
            width="60"
            slot="header">
            <template slot-scope="scope">
              {{ (currentPage-1)*pageSize+scope.$index+1 }}
            </template>
          </el-table-column>
          <el-table-column
            label="操做"
            align="center"
            width="150"
            slot="footer">
            <template slot-scope="scope">
              <el-button-group>
                <el-button
                  type="primary"
                  @click="editRowDialog(scope.$index, scope.row)">編輯</el-button>
                <el-button
                  type="danger"
                  @click="deleteRowDialog(scope.$index, scope.row)">刪除</el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </CommonTable>
        <!-- 分頁 -->
        <Pagination
          style="margin-top:20px"
          :table-begin="tableBeigin"
          @changTable="getTablePage"/>
        <!-- 2.對話框 -->
        <el-dialog
          top='60px'
          :title="dialogTitle"
          :visible.sync="dialogVisible"
          :width="dialogWidth"
          :close-on-click-modal="false"
          :show-close="!showCancelButton"
        >
          <component
            :is='componentType'
            :show-btn='showCancelButton'
            :form-info='formInfo'
            @form-change='hanldeFormChange'
          />
        </el-dialog>
    </el-row>
  </el-card>
</template>

2.複雜的內容須要自定義編輯器

<template>
  <div>
    <!-- 新建+搜索 -->
    <el-row>
      <el-col :span="4">
        <el-button
          type="primary"
          @click="featureNew"
        >新建xxx</el-button>
      </el-col>
      <el-col
        :span="4"
        :offset="parameters.privatePage==='我的'?16:20">
        <el-input
          v-model="searchInfo"
          placeholder="關鍵字搜索"
          clearable
          @clear="searchInfoMatch"
          @keyup.enter.native="searchInfoMatch(searchInfo)">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="searchInfoMatch(searchInfo)"/>
        </el-input>
      </el-col>
    </el-row>
    <!-- 自定義表格合併 -->
    <el-table
      style='margin-top:20px'
      v-loading="parameters.loading"
      :data="tableData"
      border
      :span-method="colspanMethod"
    >
      <el-table-column
        show-overflow-tooltip
        width="120"
        align="center"
        prop="type"
        label="xx分類"
      />
      <!--操做-->
      <el-table-column
        align="center"
        label="操做"
        width="200">
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              @click="tableRowChange(scope, 'edit')"
              type="primary"
              :disabled="scope.row.app_id===''">查看編輯</el-button>
          </el-button-group>
          <el-dropdown
            trigger="click">
            <el-button type="primary">
              更多<i class="el-icon-arrow-down el-icon--right"/>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                style="padding:0"
                v-for="(item,index) in tableRowMore"
                :key='index'>
                <el-button
                  style="padding: 7px 15px;width:100%"
                  @click="tableRowChange(scope, item.action)"
                  type="text"
                  :disabled="scope.row.app_id===''">{{ item.name }}</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分頁組件 -->
    <el-pagination
        style="margin-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotal"/>
    <!-- 2.對話框 -->
    <el-dialog
      top='60px'
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :width="dialogWidth"
      :close-on-click-modal="false"
      :show-close="!showCancelButton"
    >
        xxx表單內容xxx
        <span slot="footer">
          <el-button
            @click="handleClose('ruleForm')">取消</el-button>
          <el-button
            type="primary"
            @click="submitNewActivity('ruleForm')">肯定</el-button>
        </span>
    </el-dialog>
</template>

3、展現組件:函數

好比通用表格組件、搜索框組件、分頁組件、表單校驗組件(若是大於1個使用component組件動態切換)、按鈕彈框(放大編輯)、通用的編輯器組件、可視化圖形組件等,固然通用的函數、數據配置等也能夠抽離出來,或者使用第三方庫實現:佈局

import CommonTable from 'module-comp/table/CommonTable'
import _ from 'lodash' // 第三方庫
import {getSpanArr} from '@/utils/xxxManage'// 公共的方法或數據配置
import canvasInstanceStatus from 'mixins/xxxStatus'// 插入實現方法,比較隱晦很差維護mixins:[xxx]
import Pagination from './Pagination'// 通用的分頁組件或自定義須要的內容

化繁爲簡:spa

好比上面的表格操做比較繁瑣,使用數組遍歷輸出菜單,設置通用的方法調用(設置一個type區分);code

又如:表格輸出列自適應時,標題也能夠設置成一個數組格式,經過遍歷的形式進行輸出,示例在下面component

 

    <el-table
      :max-height="height"
      ref="commonTable"
      :data="tableData"
      :size="size"
      :stripe="stripe"
      border
      highlight-current-row
      v-loading="loading"
      :row-class-name="tableRowClassName"
      @filter-change="filterChange"
      @selection-change="handleSelectionChange"
      :row-key="rowKey"
    >
      <!--自定義插入-->
      <slot name="header"/>
      <el-table-column
        v-for="(item, index) in tableColumn"
        :key="`key_${index}`"
        :prop="item.prop"
        :label="item.label"
        show-overflow-tooltip
        :sortable='sortable'
        align="center"
      >
        <template slot-scope="scope">
          <div v-if="tableColumn[index].prop === 'field_key'">
            <span>{{ keyOptionsObj[scope.row.field_key] }}</span>
          </div>
          <div v-else>
            <span>{{ scope.row[tableColumn[index].prop] }}</span>
          </div>
        </template>
      </el-table-column>
      <!--自定義插入-->
      <slot name="footer"/>
    </el-table>

 

須要注意的問題:組件的通信問題,一般組件須要進行聯動效果,這須要注意傳值的技巧(只傳一個對象)、以及回調方法儘可能設置到中間件(連續回調2次以上就不利於維護了)......orm

相關文章
相關標籤/搜索