安裝vue-clihtml
npm install --save-dev vue-cli
vue-cli初始化項目vue
vue init webpack
安裝依賴包webpack
npm install
因爲vue2.0關於數據流的限制<防止數據流混亂,數據只能由上往下,事件從下往上觸發>,子組件的數據不能直接傳到父組件。web
//MyTable.vue <template> <el-row> <el-row> <el-table :data="tableData" border stripe style="width: 100%;text-align:center" :header-cell-style="headerStyle"> <el-table-column v-for="(column, index) in columns" :prop="column.prop" :key="index" :label="column.label"> <template slot-scope="scope"> <my-render v-if="column.render" :row="scope.row" :render="column.render"></my-render> <span v-else> {{scope.row[column.prop]}} </span> </template> </el-table-column> </el-table> </el-row> <el-row> <div class="block"> <el-pagination :current-page="pageNum" :page-size="pageSize" @current-change="currentChange" layout="total, sizes, prev, pager, next, jumper" :total="totalPage"> </el-pagination> </div> </el-row> </el-row> </template> <script> import MyRender from "./MyRender"; export default { props: { tableData: Array, columns: Array, pageNum: { type: Number, default: 1 }, pageSize: { type: Number, default: 10 }, totalPage:{ type: Number, default:0} }, methods: { headerStyle() { return { "text-align": "center" }; }, currentChange(val){ this.$emit('currentChange',val) } }, components: { MyRender } }; </script> <style> </style>
其中使用了vue2.0的渲染函數,渲染函數組件以下vue-cli
//MyRender.vue <script> export default { functional: true, props: { row: Object, render: Function }, render: (h, ctx) => { const params = { row: ctx.props.row, index: ctx.props.index }; return ctx.props.render(h, params); } }; </script>
其中包括下拉組件npm
//MyDropDown.vue <template> <el-dropdown trigger="click" @command="handleCommand"> <el-button type="primary"> <span v-text="dropDownData.label"></span><i class="el-icon-arrow-down el-icon--right"></i> </el-button> <el-dropdown-menu slot="dropdown"> <el-dropdown-item :command="item.func" v-text="item.label" v-for="(item,index) in dropDownData.items" :key="index"></el-dropdown-item> </el-dropdown-menu> </el-dropdown> </template> <script> export default { props: ["dropDownData"], methods: { handleCommand(command) { this.$emit(command.func, command.uuid); } } }; </script> <style> </style>
調用MyTable組件element-ui
<template> <my-table :tableData="tableData" :columns="columns" :totalPage="totalPage" @currentChange="currentChange"></my-table> </template> <script> import MyTable from "base/MyTable"; export default { data(){ return { columns: [ { prop: "title", label: "標題" }, { prop: "create_name", label: "發佈人" }, { prop: "item_name", label: "欄目" }, { prop: "create_time", label: "建立時間" }, { prop: "weight", label: "權重" }, { prop: "isoriginal", label: "原創", render: function(h, param) { let html = ""; if (param.row.isoriginal == "201") { html = "原創"; } else { html = "非原創"; } return html; } }, { prop: "", label: "操做", render: (h, param) => { const dropDownData = { label: "操做", items: [ { label: "修改", func: { func: "update", uuid: param.row.uuid } }, { label: "刪除", func: { func: "del", uuid: param.row.uuid } } ] }; // 觸發MyDropDown的update和del事件 return h(MyDropDown, { props: { dropDownData: dropDownData }, on: { update: this.update, del: this.del } }); } } ] } } } </script>