我目前正在学习 VUEJS,对此我完全陌生。所以我需要帮助。我想从 URL 中获取 ID,例如:
网址:
abc.net/dashboard/a/123456789
我想要123456789
文本格式。
如果您正在使用,这可以指导您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>
这可以通过普通的 javascript 轻松完成
const url = window.location.href;
const lastParam = url.split("/").slice(-1)[0];
console.log(lastParam);
如果您使用的是vue-router并且您加载的页面是在router.js中定义的。然后简单的调用this.$route.params