无锡网站建设技术,心动网络属于哪个公司,wordpress数据库权限,哈尔滨城市宣传片文章目录#x1f31f;前言#x1f31f;table组件封装#x1f31f;父组件#xff08;展示表格的页面#xff09;#x1f31f;控制台查看父子组件通信是否成功#x1f31f;Vue2父子组件传递参数#x1f31f;写在最后#x1f31f;JSON包里写函数#xff0c;关注博主不迷…
文章目录前言table组件封装父组件展示表格的页面控制台查看父子组件通信是否成功Vue2父子组件传递参数写在最后JSON包里写函数关注博主不迷路前言 大家好上一期的Vue实战都阅读了吗上一期主要是对Vuex的一个基本操作通过Vuex我们可以实现全局的状态数据共享以便与我们更好的实现一些需求。 不知道大家在日常工作当中是否被频繁的列表增删改查困扰功能很简单但确实是非常繁琐的一项工作今天这一期呢我会从Vue2父子组件如何传值并结合Element来封装一个简单的列表组件一次封装多次复用。 table组件封装
在你的component目录下创建一个Table.vue:
templateel-card classbox-card stylewidth: 100%;height: 100%div classbtnBoxel-button v-for(item,index) in tableOperation :keyindex :typeitem.type sizesmall [eventName]handleClick(item.fun, multipleSelection){{ item.label }}/el-button/divel-tablestylemargin-bottom: 20px:datatableDatasizesmallrow-class-namerowcell-class-namecolumn:highlight-current-rowtruefitselection-changehandleSelectionChange!--这是是为了将表格设置成带有选择框的表格--el-table-columntypeselectionwidth55/el-table-columnv-for(item, index) in tableLabel:keyindexaligncenter:propitem.prop:widthitem.width:labelitem.label//el-tablediv classblock styletext-align: endel-paginationbackground:current-page1:page-sizes[10, 20, 30, 40]:page-size10layouttotal, sizes, prev, pager, next, jumper:totaltableData.lengthsize-changehandleSizeChangecurrent-changehandleCurrentChange//div/el-card/templatescript
export default {name: Table,// 因为是子组件要接受父组件传递的数据所以这里我们定义propsprops: {tableData: { // 父组件传递过来的表格数据type: Array,default: () {return []}},tableLabel: { // 父组件传递过来的表头数据type: Array,default: () {return []}},tableOperation: { // 父组件传递过来的操作按钮数据type: Array,default: () {return []}}},data() {return {eventName: click, // 点击按钮绑定的方法这样写法也可以去绑定一些其他的比如change等方法multipleSelection: [] // 这个数组用来保存被选择行的数据顺序是按照你勾选的顺序来排序的}},methods: {// selection-change方法可以监听到表格里哪一行被选中类型是数组;// 然后我们将它赋值给定义好的 multipleSelection handleSelectionChange(val) {this.multipleSelection val},// 动态绑定操作按钮的点击事件(按钮是父组件传递过来循环出来的,所以我们给按钮定义一个方法)// 接收两个参数一个是fun(string类型)一个是rowarray类型也就是我们选中行的数据// 这里的某个按钮时传递的参数// 比如我点击的是编辑那这时的fun就是 edit,执行的方法就是下边的this.editrowhandleClick(fun,row) {this[fun](row)},edit(row) {if (!row.length) {this.$message.error(请勾选数据后操作)return false} else if (row.length 2) {this.$message.error(当前仅支持单条数据操作)return false} else {console.log(子组件点击编辑触发父组件方法;并传递数据, row)// 通过$meit通知父组件propClick方法并传递两个参数edit和rowthis.$emit(propClick, edit, row)}},look(row) {if (!row.length) {this.$message.error(请勾选数据后操作)return false} else if (row.length 2) {this.$message.error(当前仅支持单条数据操作)return false} else {console.log(子组件点击查看触发父组件方法;并传递数据, row)// 通过$meit通知父组件propClick方法并传递两个参数edit和rowthis.$emit(propClick, look, row)}},delete(row) {if (!row.length) {this.$message.error(请勾选数据后操作)return false} else if (row.length 2) {this.$message.error(当前仅支持单条数据操作)return false} else {console.log(子组件点击删除触发父组件方法;并传递数据, row)// 通过$meit通知父组件propClick方法并传递两个参数edit和rowthis.$emit(propClick, del, row)}},handleSizeChange(val) {console.log(每页 ${val} 条)},handleCurrentChange(val) {console.log(当前页: ${val})}}
}
/scriptstyle scoped/style
table组件封装好了就可以在父组件里使用啦。
父组件展示表格的页面
创建一个父组件也就是你的页面我这里起名index.vue大家可以随意 templatediv v-loadingtableLoading classdashboard-container!--子组件位置--!--自定义table-data,table-label,table-operation三个属性分别传递我们需要的数据--!--自定义propClick方法用来接收子组件的通知并执行定义的btnClick方法--myTable:table-datatableData :table-labeltableLabel:table-operationtableOperationpropClickbtnClick //div
/templatescript
import { mapGetters } from vuex
// 根据你的table组件引入到父组件里
import myTable from /components/Table/table
export default {name: Dashboard,// 并在父组件的compoments里边注册components: {myTable},computed: {...mapGetters([name])},// eslint-disable-next-line vue/order-in-componentsdata() {return {tableLoading: true,// 子组件的表格数据tableData: [{ id: 1, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 2, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 3, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 4, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 5, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 6, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 7, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 8, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 9, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 10, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 11, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 },{ id: 12, date: 2018-07-24, sales: 23.34, sale: 137597.76, const: 102203.71, profit: 35394.05 }],// 子组件的表头数据tableLabel: [{ label: ID, width: 40, prop: id },{ label: 日期, width: , prop: date },{ label: 销售量, width: , prop: sales },{ label: 销售额, width: , prop: sale },{ label: 成本, width: , prop: const },{ label: 利润, width: , prop: profit }],// 子组件的操作按钮tableOperation: [{ label: 编辑, fun: edit, type: primary },{ label: 查看, fun: look, type: success },{ label: 删除, fun: delete, type: danger }]}},mounted() {setTimeout(() {this.tableLoading falsethis.$message.success(数据加载成功)}, 1000)},methods: {// 当父组件接收到了子组件this.$emit的通知后就会执行这个方法来接收子组件点击传递过来的数据btnClick(fun, row) {if (fun edit) {console.log(子组件点击了编辑,父组件收到子组件传递的数据, row)} else if (fun look) {console.log(子组件点击了查看,父组件收到子组件传递的数据, row)} else if (fun del) {console.log(子组件点击了删除,父组件收到子组件传递的数据, row)}}}
}
/scriptstyle langscss scoped
.dashboard {-container {/*width: 100%;*/margin: 30px;height: 88vh;}-text {font-size: 30px;line-height: 46px;}
}
/style控制台查看父子组件通信是否成功 列表增删改查Vue2父子组件传递参数 接下来我们来复习一下父子组件传递参数的方法吧 父组件向子组件传递数据 父组件通过 props 给子组件下发数据 在父组件内为子组件添加自定义属性例如我上边的table-data,table-label,table-operation三个属性并为其绑定数据如果是动态绑定就需要加上 反则不用加。子组件内通过定义props来接受数据并且有以下几个参数配置
属性值说明type原生构造器参数的类型defaultany参数的默认值数组/对象的默认值应当由一个工厂函数返回requiredtrue/false参数是否必传validatorfunction自定义验证函数
Vue.component(example, {props: {// 基础类型检测 (null 指允许任何类型)propA: Number,// 可能是多种类型propB: [String, Number],// 必传且是字符串propC: {type: String,required: true},// 数值且有默认值propD: {type: Number,default: 100},// 数组/对象的默认值应当由一个工厂函数返回propE: {type: Object,default: function () {return { message: hello }}},// 自定义验证函数propF: {validator: function (value) {return value 10}},propG: {validator: function (value) {// 这个值必须匹配下列字符串中的一个return [success, warning, danger].indexOf(value) ! -1}}}
})子组件向父组件传递数据 子组件通过事件给父组件发送消息 每个 Vue 实例都实现了事件接口即
使用 $on(eventName) 监听事件使用 $emit(eventName, optionalPayload) 触发事件 1. 主要方法在父组件里。 2. 在父组件里添加自定义事件自定义事件触发执行主要方法。 3. 在子组件里写一个通知方法( this.$emit(‘自定义事件’参数) )用来告诉父组件执行自定义事件。 4. 在需要触发这个事件的元素上添加触发事件例click“子组件里的通知方法” !-- 子组件 --
templateel-card classbox-card stylewidth: 100%;height: 100%div classbtnBoxel-buttonv-for(item,index) in tableOperation:keyindex:typeitem.typesizesmallclickhandleClick(item.fun, multipleSelection){{ item.label }}/el-button/div/el-card/templatescript
export default {name: Table,methods: {add(row) {console.log(子组件点击新增触发父组件里的自定义事件-propClick)this.$emit(propClick, add)}}
}
/scriptstyle scoped/style
!-- 父组件 --
templatediv v-loadingtableLoading classdashboard-containermyTable propClickbtnClick //div
/templatescript
import myTable from /components/Table/table
export default {name: Dashboard,components: {myTable},methods: {//自定义事件-propClick触发该方法, 接收子组件点击按钮数据btnClick(fun, row) {if (fun edit) {console.log(子组件点击了编辑,父组件收到子组件传递的数据, row)// 这里就可以去写我们需要调用的接口呀数据处理逻辑呀等等} else if (fun look) {console.log(子组件点击了查看,父组件收到子组件传递的数据, row)// 这里就可以去写我们需要调用的接口呀数据处理逻辑呀等等} else if (fun del) {console.log(子组件点击了删除,父组件收到子组件传递的数据, row)// 这里就可以去写我们需要调用的接口呀数据处理逻辑呀等等}}}
}
/script写在最后
这篇文章通过父子组件通信并结合Element封装了一个简单的增删改查列表组件你是否学会了呢很多复杂的功能都是通过基础知识举一反三得来的小伙伴一定记的尝试哦。后续会为小伙伴们持续更新Vue的一些实战小魔法各位小伙伴让我们 let’s be prepared at all times
JSON包里写函数关注博主不迷路 ✨原创不易还希望各位大佬支持一下 点赞你的认可是我创作的动力 ⭐️ 收藏你的青睐是我努力的方向 ✏️ 评论你的意见是我进步的财富
文章转载自: http://www.morning.wtdyq.cn.gov.cn.wtdyq.cn http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn http://www.morning.zbnts.cn.gov.cn.zbnts.cn http://www.morning.ryqsq.cn.gov.cn.ryqsq.cn http://www.morning.nkkpp.cn.gov.cn.nkkpp.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.webpapua.com.gov.cn.webpapua.com http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.yrjfb.cn.gov.cn.yrjfb.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.jkzjs.cn.gov.cn.jkzjs.cn http://www.morning.kwqcy.cn.gov.cn.kwqcy.cn http://www.morning.nynpf.cn.gov.cn.nynpf.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.pwdrc.cn.gov.cn.pwdrc.cn http://www.morning.nfqyk.cn.gov.cn.nfqyk.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.ysrtj.cn.gov.cn.ysrtj.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.pjftk.cn.gov.cn.pjftk.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.gfrjs.cn.gov.cn.gfrjs.cn http://www.morning.lnwdh.cn.gov.cn.lnwdh.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.zpyh.cn.gov.cn.zpyh.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.rkzk.cn.gov.cn.rkzk.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.pndhh.cn.gov.cn.pndhh.cn http://www.morning.pyxtn.cn.gov.cn.pyxtn.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.sfcfy.cn.gov.cn.sfcfy.cn http://www.morning.yktr.cn.gov.cn.yktr.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.nzlsm.cn.gov.cn.nzlsm.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.bqpgq.cn.gov.cn.bqpgq.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn