首页 > 解决方案 > 从 VUEJS 中的 url 获取 ID

问题描述

我目前正在学习 VUEJS,对此我完全陌生。所以我需要帮助。我想从 URL 中获取 ID,例如:

网址:

abc.net/dashboard/a/123456789

我想要123456789文本格式。

标签: vue.jsvuejs2vuejs3

解决方案


如果您正在使用,这可以指导您vue-router

import Vue from 'vue';
import VueRouter from 'vue-router';
import DashboardComponent from "./path/DashboardComponent";

Vue.use(VueRouter);


export default new VueRouter({
    routes: [

        { path: '/dashboard/a/:id', component: DashboardComponent }//where :id is the dynamic id you wish to access from the browser
    ]
})

在你的DashboardComponent

<template>
   <div> 
       {{id}} //this will show the id in plain text
   </div>
</template>

<script>
    export default {
       name: 'Dashboard',

       data(){
           return {
              id: this.$route.params.id //this is the id from the browser
           }
       },
    }
</script>

推荐阅读