亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue3使用vue-router如何實(shí)現(xiàn)路由跳轉(zhuǎn)與參數(shù)獲取

 更新時間:2022年03月30日 12:07:05   作者:青顏的天空  
這篇文章主要介紹了Vue3使用vue-router如何實(shí)現(xiàn)路由跳轉(zhuǎn)與參數(shù)獲取,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

vue-router實(shí)現(xiàn)路由跳轉(zhuǎn)與參數(shù)獲取

路由跳轉(zhuǎn)和傳參

import { defineComponent, onMounted, reactive, readonly, ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
export default defineComponent({
  name: 'Login',
  setup() {
    const router = useRouter(), route = useRoute();
    const submitForm = () => {
      formRef.value?.validate((valid) => {
        if (valid) {
          login({ strategy: 'local', ...ruleForm })
            .then((res: any) => {
            // 獲取參數(shù)和路由跳轉(zhuǎn)
              const redirect: string = route.query && route.query.redirect;
              if (redirect) {
                router.replace(redirect);
              } else {
                router.push('/home');
              }
              return true;
            })
            .catch((e) => {
              ...
            });
        } else {
         ...
          return false;
        }
      });
    };
    return { ..., submitForm };
  }
});

路由跳轉(zhuǎn)三種方法的總結(jié)

一、第一種

1、路由設(shè)置方式

{`在這里插入代碼片`
? path: '/detail/:id',
? name: 'detail',
? meta: { keepAlive: true },
? component: () => import('../pages/detail/index')
}

2、路由跳轉(zhuǎn)模式

this.$router.push(
? {
? ? path: `/detail/1`
? }
)

3、獲取參數(shù)方式

let detailId = this.$route.params.id

注意: params 傳參相當(dāng)于是路由的一部分是必須傳的東西,經(jīng)過驗(yàn)證不傳頁面會跳轉(zhuǎn)到空白頁

該方式刷新頁面id 不丟失

二、第二種

1、路由設(shè)置方式

{
? path: '/detail/:id',
? name: 'detail',
? meta: { keepAlive: true },
? component: () => import('../pages/detail/index')
}

2、路由跳轉(zhuǎn)模式

this.$router.push(
? {
? ? name: 'Detail',
? ? params: {
? ? ? id
? ? }
? }
)

3、獲取參數(shù)方式

let detailId = this.$route.params.id

注意:此方式傳參 路由設(shè)置方式中的 id 可以傳也可以不傳,不傳刷新頁面id 會丟失

該方式刷新頁面id 不丟失

三、第三種

1、路由設(shè)置方式

{
? path: '/detail',
? name: 'detail',
? meta: { keepAlive: true },
? component: () => import('../pages/detail/index')
}

2、路由跳轉(zhuǎn)模式

this.$router.push(
? {
? ? path: 'Detail',
? ? query: {
? ? ? id
? ? }
? }
)

3、獲取參數(shù)方式

let detailId = this.$route.query.id

注意:此方式傳參 路由設(shè)置方式中的 id 不能寫,因?yàn)閷懥司褪莚outer 的一部分,這樣就會匹配不到, 此方式刷新頁面id 不丟失

http://localhost:8080/#/detail?id=1

總結(jié): params一旦設(shè)置在路由,params就是路由的一部分,如果這個路由有params傳參,但是在跳轉(zhuǎn)的時候沒有傳這個參數(shù),會導(dǎo)致跳轉(zhuǎn)失敗或者頁面會沒有內(nèi)容。

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評論