基于Vue的ElementUI项目实现父子组件信息传递
Posted lovoo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Vue的ElementUI项目实现父子组件信息传递相关的知识,希望对你有一定的参考价值。
前言
如项目中有一个产品分类的树形组件,在分类属性组件中引入之后,要实现点击左侧的树节点,更新右端的表格,这种功能如何实现呢?
如图:
1、子组件:
1.1、在el-tree节点上加上
@node-click="nodeClick"
1.2、实现方法中使用this.$emit进行数据提交,
nodeClick(data, node, component){
//向父组件发送数据请求
console.log(data, node, component)
this.$emit('tree-node-click', data, node, component)
}
此方法有四个参数,第一个’tree-node-click’是在父组件中的方法属性,data表示节点的数所, node表示节点信息, component表示组件本身
1.3、全部源码
<template>
<el-tree :data="menus" :props="defaultProps" node-key="catId" ref="menuTree" @node-click="nodeClick"></el-tree>
</template>
<script>
//这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
//例如:import 《组件名称》 from '《组件路径》';
export default {
//import引入的组件需要注入到对象中才能使用
components: {},
data() {
return {
menus: [],
expandedKeys: [], // expandedKeys默认展开的数组
defaultProps: {
children: 'children',
label: 'name'
}
};
},
//监听属性 类似于data概念
computed: {},
//监控data中的数据变化
watch: {},
//方法集合
methods: {
getMenus() {
this.dataListLoading = true;
this.$http({
url: this.$http.adornUrl('/product/category/list/tree'),
method: 'get'
}).then(({ data }) => {
if (data && data.code === 0) {
this.menus = data.data;
} else {
}
this.dataListLoading = false;
});
},
nodeClick(data, node, component){
//向父组件发送数据请求
console.log(data, node, component)
this.$emit('tree-node-click', data, node, component)
}
},
//生命周期 - 创建完成(可以访问当前this实例)
created() {
this.getMenus();
},
};
</script>
2、父组件
2.1、引入组件
import Category from '../../common/category.vue';
2.2、定义组件
components: {
Category
},
2.3、使用组件,并给组件添加子组件定义的方法属性@tree-node-click=“treeNodeClick”
<el-col :span="6"><category @tree-node-click="treeNodeClick"></category></el-col>
2.4、实现treeNodeClick方法
treeNodeClick(data, node, component){
console.log("响应数据:", data, node, component)
},
2.5、全部源码
<template>
<el-row :gutter="20">
<el-col :span="6"><category @tree-node-click="treeNodeClick"></category></el-col>
<el-col :span="18">
<div class="mod-config">
<el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
<el-form-item><el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input></el-form-item>
<el-form-item>
<el-button @click="getDataList()">查询</el-button>
<el-button v-if="isAuth('product:attrgroup:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
<el-button v-if="isAuth('product:attrgroup:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
</el-form-item>
</el-form>
<el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
<el-table-column prop="attrGroupId" header-align="center" align="center" label="分组id"></el-table-column>
<el-table-column prop="attrGroupName" header-align="center" align="center" label="组名"></el-table-column>
<el-table-column prop="sort" header-align="center" align="center" label="排序"></el-table-column>
<el-table-column prop="descript" header-align="center" align="center" label="描述"></el-table-column>
<el-table-column prop="icon" header-align="center" align="center" label="组图标"></el-table-column>
<el-table-column prop="catelogId" header-align="center" align="center" label="所属分类id"></el-table-column>
<el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
<template slot-scope="scope">
<el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.attrGroupId)">修改</el-button>
<el-button type="text" size="small" @click="deleteHandle(scope.row.attrGroupId)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
@size-change="sizeChangeHandle"
@current-change="currentChangeHandle"
:current-page="pageIndex"
:page-sizes="[10, 20, 50, 100]"
:page-size="pageSize"
:total="totalPage"
layout="total, sizes, prev, pager, next, jumper"
></el-pagination>
<!-- 弹窗, 新增 / 修改 -->
<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
</div>
</el-col>
</el-row>
</template>
<script>
import Category from '../../common/category.vue';
export default {
//import引入的组件需要注入到对象中才能使用
components: {
Category
},
data() {
return {
dataForm: {
key: ''
},
dataList: [],
pageIndex: 1,
pageSize: 10,
totalPage: 0,
dataListLoading: false,
dataListSelections: [],
addOrUpdateVisible: false
};
},
activated() {
this.getDataList();
},
methods: {
// 获取数据列表
getDataList() {
this.dataListLoading = true;
this.$http({
url: this.$http.adornUrl('/product/attrgroup/list'),
method: 'get',
params: this.$http.adornParams({
page: this.pageIndex,
limit: this.pageSize,
key: this.dataForm.key
})
}).then(({ data }) => {
if (data && data.code === 0) {
this.dataList = data.page.list;
this.totalPage = data.page.totalCount;
} else {
this.dataList = [];
this.totalPage = 0;
}
this.dataListLoading = false;
});
},
//响应父节点事件 $emit('tree-node-click', data, node, component)
treeNodeClick(data, node, component){
console.log("响应数据:", data, node, component)
},
};
</script>
以上是关于基于Vue的ElementUI项目实现父子组件信息传递的主要内容,如果未能解决你的问题,请参考以下文章
Vue.js 学习13 ElementUI项目中使用自定义组件