// 在 Vue 项目中,你可以使用 Vue Router 来获取当前路由地址。以下是一个简单的示例代码:
// 假设你已经在项目中安装并配置了 Vue Router
// 在组件中获取当前路由地址的方法如下:
export default {
data() {
return {
currentPath: ''
}
},
created() {
// 使用 this.$route.path 获取当前路由的路径
this.currentPath = this.$route.path;
console.log('当前路由地址:', this.currentPath);
}
}
// 如果你需要监听路由变化并实时获取最新的路由地址,可以使用 watch 监听 $route 对象:
export default {
data() {
return {
currentPath: ''
}
},
watch: {
$route(to, from) {
// 当路由发生变化时,更新 currentPath
this.currentPath = to.path;
console.log('路由已更改,新的地址是:', this.currentPath);
}
},
created() {
// 初始化时设置当前路由地址
this.currentPath = this.$route.path;
console.log('初始路由地址:', this.currentPath);
}
}
这段代码展示了如何在 Vue 组件中获取当前路由地址,并且还演示了如何监听路由变化以实时更新路由地址。
上一篇:vue 打开新窗口
下一篇:vue3安装路由
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站