// 在 Vue 组件中获取当前 URL 地址
// 方法 1: 使用 window.location.href
export default {
methods: {
getCurrentUrl() {
console.log(window.location.href); // 返回完整的 URL 字符串
}
}
}
// 方法 2: 使用 Vue Router (如果项目中使用了 Vue Router)
export default {
computed: {
currentPath() {
return this.$route.fullPath; // 返回当前路由的完整路径
}
}
}
方法 1:通过 window.location.href 可以直接获取浏览器当前的完整 URL。这个方法不需要依赖任何框架,适用于所有 JavaScript 环境。
方法 2:如果你在项目中使用了 Vue Router,可以通过 this.$route.fullPath 获取当前路由的完整路径。这种方式更适合在 Vue 单页应用(SPA)中使用。
上一篇:vue 字符串转对象
下一篇:vue3设置全局变量
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站