vue3中使用route、router、store的方式

route:

 (1) vue3写法:

import { useRoute } from "vue-router";

let route = useRoute();

route.query;
route.params;

 (2) vue2写法:

import { getCurrentInstance } from "vue";

let { proxy: this_ }: any = getCurrentInstance();

this_.$route.query;
this_.$route.params;

 router:

 (1) vue3写法:

import { useRouter } from "vue-router";

let router = useRouter();

router.push("/home");
router.replace("/home");
router.go(1);

 (2) vue2写法:

import { getCurrentInstance } from "vue";

let { proxy: this_ }: any = getCurrentInstance();

this_.$router.push("/home");
this_.$router.replace("/home");
this_.$router.go(1);

 store:

 (1) vue3写法:

import { useStore} from "vuex";

let store= useStore();

store.state.list;
store.commit("getList",res.data);
store.dispatch("getList",res.data);
store.getters.getList;

 (2) vue2写法:

import { getCurrentInstance } from "vue";

let { proxy: this_ }: any = getCurrentInstance();

this_.$store.state.list;
this_.$store.commit("getList",res.data);
this_.$store.dispatch("getList",res.data);
this_.$store.getters.getList;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Router使用params传参时,页面刷新参数丢失的问题可以通过以下几种方式来解决: 1. 使用query参数代替params参数:将参数传递给路由时,可以考虑使用query参数而不是params参数。query参数会以查询字符串的形式出现在URL,刷新页面时参数不会丢失。例如,将参数传递给路由时,可以使用`this.$router.push({ path: '/example', query: { id: 123 } })`,然后在目标组件使用`this.$route.query.id`来获取参数值。 2. 使用localStorage或sessionStorage进行数据持久化:将参数保存在localStorage或sessionStorage,在组件加载时从存储获取参数值。这样即使页面刷新,参数仍然可以从存储恢复。例如,在路由导航守卫,可以将参数保存到localStorage:`localStorage.setItem('id', id)`,然后在组件加载时,从localStorage读取参数:`const id = localStorage.getItem('id')`。 3. 使用Vuex进行数据管理:将参数保存在Vuex的状态管理,在组件之间共享参数值。这样即使页面刷新,参数仍然可以从Vuex获取。例如,在某个组件将参数保存到Vuex的state:`this.$store.commit('saveId', id)`,然后在其他组件通过`this.$store.state.id`来获取参数值。 4. 使用路由解析函数:在Vue Router定义一个解析函数,用于从URL提取参数值,并在组件加载时调用该函数进行参数解析。这样即使页面刷新,解析函数仍然可以从URL提取参数值。具体实现可以参考Vue Router的官方文档的"Advanced Matching Patterns"一节。 以上是几种常见的解决方式,您可以根据具体需求选择适合的方法。希望对您有帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值