博客
关于我
Vue路由嵌套刷新后页面没有重新渲染
阅读量:375 次
发布时间:2019-03-05

本文共 667 字,大约阅读时间需要 2 分钟。

路由嵌套中,当地址栏进入子路由页面时,刷新页面可能导致子路由页面无法重新渲染的问题。这通常发生在父路由完成刷新后,子路由页面未能及时更新的情况下。

解决方法

当子路由页面无法重新渲染时,可以通过以下两种方法进行优化:

方法1:条件渲染加重渲染

在子路由容器中,使用条件渲染来控制子路由内容的显示状态。这种方法通过在父路由完成渲染后,重新激活子路由的渲染。

data() {  return {    routerAlive: true  }},mounted() {  this.routerAlive = false;  this.$nextTick(() => {    this.routerAlive = true;  });}

这种方法的原理是通过条件渲染的状态变化,迫使浏览器重新渲染子路由内容。每次刷新父路由后,子路由会被重新激活,从而确保内容的更新。

方法2:添加路由视图键

为子路由视图添加动态键,可以迫使浏览器重新渲染子路由内容。每次刷新时,键值会随机变化,从而让浏览器认为这是一个全新的组件。

这种方法简单易行,每次刷新页面都会生成一个新的键值,从而确保子路由页面能够重新渲染。

选型建议

两种方法各有优劣,需要根据项目需求选择最合适的实现方案:

  • 方法1:在实现上稍显复杂,但在实际应用中表现更为稳定,尤其是在处理大量子路由时。
  • 方法2:实现简单,但需要权衡随机键值生成是否会对性能产生影响。

通过以上两种方法,可以有效解决子路由页面在刷新时无法重新渲染的问题。选择哪种方法取决于具体项目的需求和性能考量。

转载地址:http://ptmg.baihongyu.com/

你可能感兴趣的文章
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>