当前位置: 首页 > news >正文

父子组件传值问题

文章目录

  • 前言
  • 一、问题描述
  • 二、问题解决


前言

在写毕业设计,涉及了一些前端Vue.js的组件传值知识并出现了相关问题,因此进行记录。

问题

  1. Vue.js的使用不熟练,相关组件、props等掌握不清晰
  2. 前端代码书写不规范

望指正!


一、问题描述

想要搭建一个模型检验的页面,在点击按钮“开始检测”后,后端会获取相应数据、页面跳转并进行渲染。
主要涉及三个页面:index.vue、BorderCard.vue、CardResult.vue,如图1:
在这里插入图片描述
index.vue想要引入“步骤条”实现两个组件的切换效果,如图2:
在这里插入图片描述

index.vue中引入两个组件的部分:

<border-card v-show="active === 0" :tab-index="active"/>
<card-result v-show="active === 1" />

相关部分代码如下:

// BorderCard.vue
<template>
<el-button style="position: absolute;top:-48px;right:20px;z-index: 999999;" @click="next">立即检测</el-button>
</template>
<script>
methods: {next() {/** 点击立即检测按钮后,发送请求从后端获取数据(图表模型的正确率,数据信息等),然后利用 this.$emit -> 父组件 -> CardResult.vue*/console.log('handleModelTrain....');this.$http.post('').then(res => {console.log('handleModelTrain=>', res);this.$emit('next', res.data.data)}).catch(() => {this.$message.error('模型训练异常')})},
}
</script>
// index.vue
<template>
<border-card v-show="active === 0" :tab-index="active" @next="handleNextTabs" />
<card-result v-show="active === 1" :model-report="modelReport" />
</template>
<script>
methods: {handleNextTabs(data) {console.log('recall=>', data);if (!data) {this.$message.error('模型训练异常')}// 数据解析this.show_info.total = data.data_totalthis.modelReport = dataif (this.active++ > 1) this.active = 0;},
}
// CardResult.vue
<script>
props: {modelReport: {type: Object,default: () => {return {score: 0,report: ''}}}},watch: {modelReport: {handler: function(newVal, oldVal) {console.log('watch modelReport=>', newVal, oldVal);// 更新图表this.getEchartDataInit(false)// 更新表格this.getTableDataForReport(newVal)},deep: true}}

单独呈现 CardResult.vue组件如图
在这里插入图片描述
当组件切换后,下面数据报告正常显示,但是图像发生了变形:
在这里插入图片描述

二、问题解决

查阅相关文章,其中原因包括:

  • 采用v-show控制切换时,v-show为false时,echarts图并不能获取外部容器的正常宽高,所以展示出来的图形会以其自身默认的大小展示;解决方法-> 在切换到图时重新调用图组件

根据上面说法,我理解的是切换组件的时候重新进行加载,相关博客建议使用组件 :key 属性,当发生变化后会重新加载组件,代码如下:

<border-card v-show="active === 0" :key="active" :tab-index="active" @next="handleNextTabs" />
<card-result v-show="active === 1" :key="!active" :model-report="modelReport" />

更改后的效果如图:
在这里插入图片描述

  • 此处组件传值失效的具体原因不清楚。是否和组件传值先被props接受,然后发生了组件重新加载有关

因此查找相关问题:Vue刷新后页面数据丢失问题的解决过程
解决方案:使用 SessionStorage 进行处理。
相关部分代码如下:

// index.vue
<script>
methods: {handleNextTabs(data) {console.log('recall=>', data);if (!data) {this.$message.error('模型训练异常')}this.show_info.total = data.data_totalthis.modelReport = data// 将数据存储到sessionStoragesessionStorage.setItem('modelReport', JSON.stringify(this.modelReport))if (this.active++ > 1) this.active = 0;}
}
</script>
// CardResult.vue
<script>
created() {console.log('CardResult created...');if (sessionStorage.getItem('modelReport')) {this.modelReport = sessionStorage.getItem('modelReport') // 获取数据直接更新}},beforeDestroy() {// 毁灭前先移除掉,否则我跳转到其它地方,sessionStorage里面依旧存在着console.log('CardResult beforeDestroy...');sessionStorage.removeItem('modelReport')}
}
</script>

然而发生下列错误,如图:
在这里插入图片描述
为了避免直接改变prop中的值,因此我考虑继续使用 this.$emit -> index.vue,由于此时组件没有发生切换,因此不会触发组件的重新加载,然后实现赋值

修改后,部分相关代码如下:

// CardResult.vue
<script>
created() {console.log('CardResult created...');if (sessionStorage.getItem('modelReport')) {this.$emit('refreshData', JSON.parse(sessionStorage.getItem('modelReport')))}},beforeDestroy() {// 毁灭前先移除掉,否则我跳转到其它地方,sessionStorage里面依旧存在着console.log('CardResult beforeDestroy...');sessionStorage.removeItem('modelReport')}
}
</script>
<template><border-card v-show="active === 0" :key="active" :tab-index="active" @next="handleNextTabs" /><card-result v-show="active === 1" :key="!active" :model-report="modelReport" @refreshData="refreshDataParent" />
</template>
<script>
methods: {refreshDataParent(val) {console.log('refreshDataParent recall...');this.modelReport = val}
}
</script>

运行结果如图,目前能够正常显示:
在这里插入图片描述


http://www.lryc.cn/news/57903.html

相关文章:

  • Redis大key问题
  • 00后卷王的自述,我难道真的很卷?
  • Redis第七讲 Redis存储模型详解
  • Python 进阶指南(编程轻松进阶):十五、面向对象编程和类
  • windows下postgresql安装timescaledb
  • Linux系统常用命令大全
  • 月报总结|Moonbeam 3月份大事一览
  • 多功能料理锅语音播放芯片——NV040C
  • vue23自定义svg图标组件
  • 相机雷达时间同步(基于ROS)
  • 素数环PrimeRing [3*]
  • mongodb 连接池配置
  • 数据在内存中的存储(深度剖析)
  • python 实现二叉搜索树的方法有哪些?
  • ORM概述
  • 程序员必知必会7种UML图(类图、序列图、组件图、部署图、用例图、状态图和活动图)画法盘点
  • 基于asp的搜索引擎开发和实现
  • 代码随想录刷题-字符串-实现 strStr()
  • 前端已死?金三银四?你收到offer了吗?
  • C生万物 | 十分钟带你学会位段相关知识
  • Spring Boot基础学习之(十):修改员工的信息
  • 闭关十几天,我完成了我的毕业设计
  • 认识rust的项目管理工具--cargo
  • 面试常问的Linux之 I/O 复用
  • MySQL-binlog+dump备份还原
  • 互联网络-单级互联网络
  • 上海亚商投顾:沪指四连阳重回3300点 中字头个股再发力
  • LeetCode:150. 逆波兰表达式求值—栈
  • C/C++每日一练(20230410) 二叉树专场(4)
  • 策化整理1