// 在 Vue 组件中获取当前页面的 URL
// 方法 1: 使用 window.location.href
export default {
methods: {
getCurrentUrl() {
// 返回当前页面的完整 URL
return window.location.href;
}
},
mounted() {
console.log(this.getCurrentUrl()); // 打印当前页面的 URL 到控制台
}
}
// 方法 2: 使用 Vue Router (如果项目中使用了 Vue Router)
import { useRoute } from 'vue-router';
export default {
setup() {
const route = useRoute();
// 返回当前页面的 URL 路径
const currentPath = route.fullPath;
console.log(currentPath); // 打印当前页面的路径到控制台
return {
currentPath
};
}
}
方法 1:直接使用浏览器提供的 window.location.href
获取当前页面的完整 URL。这个方法适用于所有浏览器环境,不需要依赖任何框架或库。
方法 2:如果你在项目中使用了 Vue Router,可以通过 useRoute
钩子来获取当前路由的信息。route.fullPath
返回的是当前页面的完整路径(包括查询参数和哈希)。
上一篇:vue query传参
下一篇:vue loading效果
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站