1. 程式人生 > 其它 >vue獲取url裡面的引數值

vue獲取url裡面的引數值

路由獲取

  • query方式
//傳遞引數
 this.$router.push({
        path: "/course",
        query: { id: '1'},
    });
      
//接收引數
this.id = this.$route.query.id;
  • params方式
//傳遞引數
this.$route.push({name:'/homeView',params:{id:'1'}})
//接收引數
this.id = this.$route.params.id; 

使用js獲取頁面引數

  • 採用正則表示式獲取位址列引數
//    http://192.168.1.15:8080/#/id=123
//在utils檔案下新建一個query.js的js檔案 寫入以下程式碼 export function getQueryString(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(window.location.href) || [, ""])[1].replace(/\+/g, '%20')) || null } //在專案檔案頁面中引入 import { getQueryString } from "@/utils/query"
; let id = getQueryString("id") console.log(id) //即可打印出url 引數id的值