hello~ I wrote a ts page for vue3 today, which needs to be drilled down. In order to facilitate the maintenance of the code, I encapsulated it a little bit;
Let me talk about the writing method of subcomponents first, write a form normally, and add search conditions~
<template>
<el-drawer v-model="drawer" size="80%" direction="btt" title="数据列表">
<el-form :inline="true" :model="formInline" class="demo-form-inline">
<el-form-item label="区县">
<el-input v-model="formInline.regionName" placeholder="请输入区县" />
</el-form-item>
<el-form-item label="公司名称">
<el-input v-model="formInline.companyName" placeholder="请输入公司名称" />
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">查询</el-button>
<el-button type="primary" @click="resetting">重置</el-button>
</el-form-item>
</el-form>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="regionName" label="区县名称" />
<el-table-column prop="companyName" label="公司名称" />
</el-table>
</el-drawer>
</template>
<script lang="ts" setup>
import { ref, reactive } from "vue";//引入
const props = defineProps({
tableData: Array,//子接收数据
});
const drawer = ref(false);//定义窗户出现
const formInline = reactive({//定义搜索表单,
regionName: "",
heatstationName: ""
});
const open = () => {//子组件打开的方法
drawer.value = true;
};
//父组件要想调用子组件的方法,必须得先让子组件把方法推出去
defineExpose({
open
});
const emit = defineEmits(["clickToFather"]);
const onSubmit = () => {//子组件搜索,调用父组件的方法,要用emit,一定在上面调用哦
emit("SubmitSearch", formInline);
};
</script>
<style></style>
The ts part of the child component includes defineProps for receiving, and the called method must be launched to defineExpose ({method}), and the child changes the parent component (the child calls the parent) to define with const emit = defineEmits(["clickToFather"]), use
emit("SubmitSearch", formInline) calls and passes values.
<script lang="ts" setup>
import { ref, reactive } from "vue";//引入
const props = defineProps({
tableData: Array,//子接收数据
});
const drawer = ref(false);//定义窗户出现
const formInline = reactive({//定义搜索表单,
regionName: "",
heatstationName: ""
});
const open = () => {//子组件打开的方法
drawer.value = true;
};
//父组件要想调用子组件的方法,必须得先让子组件把方法推出去
defineExpose({
open
});
const emit = defineEmits(["clickToFather"]);
const onSubmit = () => {//子组件搜索,调用父组件的方法,要用emit,一定在上面调用哦
emit("SubmitSearch", formInline);
};
</script>
Now it is the parent component~
import RunDrawering from "./components/RunDrawer.vue";//引入不需要注册,直接使用。
Use directly
<RunDrawering ref="rundrawers" :tableData="tableData" @SubmitSearch="SubmitSearch" />
Because the sub-component is a pop-up layer, I directly click the button to call the method of the sub-component, so give the sub-component a ref
const rundrawers = ref<any>();//定义子组件的ref,不定义会出错
const RunDrawer = () => {
rundrawers.value.open();//调用子组件的方法
baseService.get("接口", { page: 1, limit: 100 }).then((res) => {
tableData.value = res.data.list;//这里调取了接口传值
});
};
The parent receives the method call of the child component, and the parameters are passed like this
const SubmitSearch = (formInline: Object) => {
baseService.get("接口", { page: 1, limit: 100, regionName: formInline.regionName, companyName: formInline.companyName }).then((res) => {
tableData.value = res.data.list;
});
};
I am getting an error here,
Identifier 'RunDrawer' has already been declared.
I searched because the definition of my component name was repeated, everyone must pay attention~