需求:通过不同的参数复用同一组件,实现动态加载数据和图片,同时,在页面刷新时,图片依旧可以加载成功。

过程出现的bug和问题:

  1、使用params传参后,再次刷新页面,参数丢失导致数据无法再次加载

  2、改用query进行传参后,页面刷新后图片无法加载,这是由于图片的url是在created钩子函数调用查询数据api后才进行赋值,而赋值之后页面已经渲染完成,导致图片无法加载成功

解决方案:

  1、通过localStorage将数据持久化,在跳转到当前路由时,先使用localStorage将数据持久化,这里可以配合vuex进行操作

  2、页面渲染前,图片的url直接读取localStorage的值,就不会出现页面渲染完成,url还没被赋值导致图片无法加载的情况

附上代码:

  需要从 /zyview 跳转至 /viewmessage

  /zyview跳转部分:

      this.$router.push({
// path: `/viewmessage/${this.names[index]}`,
// 使用下列方法方法跳转路由,就不会出现点击tab后再次跳转的bug
name: "viewmessage",
query: {
name: this.lists[index].m_name, // 此为药品名称
index: index, // 药材索引
mid: this.lists[index].id, // 药材id
// imgpath: this.lists[index].img_path
},
});

  触发跳转的方法:

    gotodescribe(e,index) {
console.log(this.lists[index].id)
console.log(this.test)
const path = this.lists[index].img_path
// 这里path和index是和store中actions方法里面的变量名意义对应,名字不对应无法传值
this.$store.dispatch('addimagepath', {path,index})
console.log("存储内容" + this.$store.state.imgpath.imgpaths[this.$route.query.index])
this.$router.push({
// path: `/viewmessage/${this.names[index]}`,
// 使用下列方法方法跳转路由,就不会出现点击tab后再次跳转的bug
name: "viewmessage",
query: {
name: this.lists[index].m_name, // 此为药品名称
index: index, // 药材索引
mid: this.lists[index].id, // 药材id
// imgpath: this.lists[index].img_path
},
});
},

  这是跳转前将数据持久化的步骤:

this.$store.dispatch('addimagepath', {path,index})

以下为vuex部分,新建一个文件写此存储图片路径的模块:

const state = {
// 图片路径
imgpaths: [],
// imgpaths: localStorage.getItem(imgpaths[index]),
test: "b4b797913756456bb5ffd8d661ab79e5.jpg"
}
const getters = { }
// 改变state状态
const mutations = {
addimgpath(state,{path,index}){
state.imgpaths[index] = path
localStorage.setItem(index,path)  // 这里使用localStorage持久化数据,后面页面就直接读取localStorage中的数据
}
}
const actions = {
addimagepath(context,{path,index}){
context.commit('addimgpath',{path,index})
}
} export default {
state,
getters,
mutations,
actions
}

   /viewmessage部分:

        <el-image
:src="`http://image.zysuyuan.cn:8031/${this.path}`"
style="width: 400px; height: 400px"
:fit="fit"
alt="药材"
></el-image>
  data() {
return {
path: localStorage.getItem(this.$route.query.index),
.........
}
}

  created和watch:

  created() {
this.fetchData();
}, watch: {
$route(to, from) {
this.path = localStorage.getItem(this.$route.query.index);
this.fetchData();
}
},

  

最新文章

  1. cout输出控制——位数和精度控制
  2. C# CRC-CCITT (Kermit)校验方法
  3. 比较C++和C#的一个性能问题
  4. hdu 4031 2011成都赛区网络赛A题 线段树 ***
  5. Extjs读取本地下拉选框数据源,分为text和value,显示text,传值value
  6. oracle在线重定义表
  7. poj 1176 Party Lamps
  8. PHP实现分页:文本分页和数字分页
  9. SSIS 学习(8):事务【转】
  10. Linux下apache的停止、开启、重启
  11. 腾讯云数据库团队:MySQL AHI 实现解析
  12. 字典树 Trie树
  13. 开源分享,使用Servlet实现360商城
  14. CrashMonkey4Android 的安装及使用
  15. sql针对某一字段去重,并且保留其他字段
  16. 新DevOps八荣八耻
  17. 3466 ACM Proud Merchants 变形的01背包
  18. Python3,x:Fiddler抓包工具如何进行手机APP的数据爬取
  19. jupyter 快捷键
  20. 使用vs code搭建C开发环境

热门文章

  1. Mysql: [Warning] Using a password on the command line interface can be insecure
  2. SQL Server 2008 install
  3. 树形dp经典换根法——cf1187E
  4. 微信小程序——单选项
  5. php算法题---对称的二叉树
  6. ul列表元素在float:right后li元素倒转
  7. 第十三篇:一点一滴学ibatis(二)映射文件
  8. Git初次使用,记录自己看
  9. 初识OpenCV-Python - 003:Mouse as a paint-brush
  10. java.nio.file.FileSystemException: D:\kafka_2.12-2.1.0\kafka_2.12-2.1.0\logs\__consumer_offsets-30\00000000000000000000.timeindex.cleaned: 另一个程序正在使用此文件,进程无法访问。