以 分頁 組件爲例:(根據本身具體業務編寫)vue
1.pagination.vuethis
<template> <!-- 分頁 --> <div class="table-pagination" v-if="flag"> <template> <div class="block"> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 30]" :page-size="pageSize" :total="totalCount" layout="total, sizes, prev, pager, next"> </el-pagination> </div> </template> </div> </template> <script> export default { name: 'my-pagination', props: { flag: Boolean, page: Number, pageSize: Number, totalCount: Number }, methods: { handleSizeChange (val) { this.$emit('handleSizeChange', val) }, handleCurrentChange (val) { this.$emit('handleCurrentChange', val) } } } </script>
2.新建一個utils文件夾,而後新建一個global.jsspa
/* ** 公共組件 */ import Pagination from '../components/common/pagination/Pagination.vue' const global = (Vue) => { if (global.installed) return Vue.component('my-pagination', Pagination) // 註冊全局分頁組件 } export default global
3.在main.js中引入jscode
import Global from './utils/global' Vue.use(Global) // 註冊全局組件
4.直接在頁面中引入component
<my-pagination @handleSizeChange='handleSizeChange' @handleCurrentChange='handleCurrentChange' :flag='flag' :page='page' :pageSize='pageSize' :totalCount='totalCount'> </my-pagination>