当前位置:网站首页>Defineexpose, the parent component gets the attribute value in the child component
Defineexpose, the parent component gets the attribute value in the child component
2022-07-22 04:09:00 【mxlWhb】
<template>
<div>
<div> Parent component </div>
<detail ref="sonRef"></detail>
<el-button @click="getSonMessage"> Get subcomponent data </el-button>
<div>{
{ message }}</div>
<div v-for="item in state.fatherList " :key ="item.id">{
{ item.name}}</div>
</div>
</template>
<script setup> import {
ref,toRefs ,reactive} from "vue"; import detail from "@/views/role/detail.vue"; let sonRef = ref("sonRef"); let message = ref("") let state=reactive({
fatherList:[], }) let getSonMessage=()=>{
console.log(sonRef.value.sonInfo,sonRef.value.dataList,"+sonInfo") state.fatherList = sonRef.value.dataList message.value = sonRef.value.message sonRef.value.show() } </script>
<template>
<div> Child components </div>
<div v-for="item in state.dataList" :key="item.id">{
{item.name}} Child components </div>
</template>
<script setup> import {
defineExpose, onMounted, reactive, ref, toRefs } from "vue"; import {
getuserListApi } from "@/api/user"; let message = ref(" Simple type "); let state = reactive({
dataList: [{
id: 1, name: 123 }], sonInfo: {
name: " Child components ", tip: " object ", }, }); onMounted(async () => {
let {
code, data } = await getuserListApi(); if (code == 0) {
state.dataList = data; } }); let show = () => {
console.log(" Subcomponents show Method "); }; let {
dataList } = toRefs(state); defineExpose({
// use state.dataList The parent component receives the initialized data , // Not from onMounted The data from it , But the datalist It's from onMounted The latest data obtained . // hold dataList from state Inside through toRefs deconstruction , The parent component can also get the latest data dataList: dataList, message, show, sonInfo: state.sonInfo, }); </script>
边栏推荐
猜你喜欢
Shell programming - functions
vite打包报错[rollup-plugin-dynamic-import-variables] Unexpected token ,竟然是因为console.log
Yan Weimin Chapter II after class exercises (2.29-2.38)
论文阅读:A Large-Scale Chinese Short-Text Conversation Dataset(CDial-GPT)
极简投资笔记-做一个富有的你自己
defineExpose ,父组件获取子组件中的属性值
【综合笔试题】难度 3.5/5,多解法热门二叉树笔试题
Shell编程-函数
Clion(CMake工具)中创建父子项目,引入第三方库的方法
814. 二叉树剪枝 : 简单递归运用题
随机推荐
influxdb
残差网络解决了什么,为什么有效?—摘要
2021-03-14-jwolf-temp-note
Application of stack
攻防世界新手区pwn
Composite of gadgets
Shell operators - mathematical operations, relational operations, string operations, file detection operations
10 papers of ant security laboratory were included by ccf-a top meeting to explore the realization of AI credibility from the perspective of algorithm
[Nuxt 3] (七)获取数据
USB of gadget_ function
合并二叉树
XLNet:运行机制及和Bert的异同比较——摘要总结
Gadget driver framework (6)
Shell编程基础及变量
【CCF CSP】201512-1数位之和
[CCF CSP] 201604-1 break point count
EOS帐号体系
判断是否完全二叉树
论文阅读:CTRL: A CONDITIONAL TRANSFORMER LANGUAGE MODEL FOR CONTROLLABLE GENERATION
一款令我相见恨晚的工具Utools