element-vue3-admin用router.push跳转新页面并刷新新页面

element-vue3-admin用router.push跳转新页面并刷新新页面

方法一:使用key属性强制刷新

在router-view上使用key属性,并绑定一个响应式的变量,每次跳转时更新这个变量的值,这样router-view会因为key的变化而重新渲染组件。

比如:从add.vue 页面跳转index.vue页面。

1.首先在invex.vue页面加上router-view标签对
html 复制代码
<router-view :key="componentKey"></router-view>
2.然后在add.vue页面加入如下代码
javascript 复制代码
<script lang="ts">
import { ref } from 'vue';
import router from "/@/router";
 
export default {
  setup() {
    const componentKey = ref(0);
 
    function refreshPage() {
      componentKey.value++;
    }
 
    function locateToPage() {
      router.push('/product/list').then(() => {
        refreshPage();
      });
    }
 
    return { locateToPage };
  },
};
</script>

方法二:使用location.reload()

在add.vue页面加入location.reload()强制浏览器刷新整个页面
javascript 复制代码
<script lang="ts">
import router from "/@/router";

function locateToPageAndRefresh() {
  router.push('/product/list').then(() => {
    window.location.reload();
  });
}
</script>

location.reload()强制刷新整个页面可能会导致用户数据丢失,请谨慎使用!

相关推荐
Java展博4 小时前
[前端够用就好] 跨域报错时,浏览器到底在拦谁
前端·状态模式
用户112936112194 小时前
npm 都提示成功了,node_modules 为什么还占着 370M?
linux·前端·后端
掘金安东尼5 小时前
Agent Harness 也有 benchmark:评分榜与评测工作台现状
前端·agent
digger_4195 小时前
从零理解 Electron:主进程、渲染进程、preload 与 IPC 实战
前端·electron
去伪存真6 小时前
别让你的 RAG 平台“花五份钱、答一份结巴的题”:幂等性架构的自我修养
前端·agent
snow@li7 小时前
前端:导入EXCEL并解析文件内容,demo
前端·javascript·vue.js
补码缩写7 小时前
压缩PNG不损失透明:只量化RGB、alpha原样留的减色流程
前端
RuoyiOffice7 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office
TDengine (老段)8 小时前
TDengine IDMP 知识库与知识图谱
大数据·前端·物联网·知识图谱·时序数据库·tdengine·涛思数据
goehou9 小时前
PNG 转 WebP 不一定变小,我量了三类图
前端·性能优化·webp·sharp