Vue 获取页面协议、端口和域名方法
在 Vue 中获取页面协议、端口和域名可以通过以下方式:
- 获取协议
可以通过 'window.location.protocol' 获取当前页面的协议,例如:
const protocol = window.location.protocol;
console.log(protocol); // http: 或者 https:
- 获取端口
可以通过 'window.location.port' 获取当前页面的端口号,例如:
const port = window.location.port;
console.log(port); // 80 或者 443 或者其他端口号
- 获取域名
可以通过 'window.location.hostname' 获取当前页面的域名,例如:
const hostname = window.location.hostname;
console.log(hostname); // www.example.com
综合起来,可以使用如下代码获取页面协议、端口和域名:
const protocol = window.location.protocol; // 获取协议
const port = window.location.port; // 获取端口
const hostname = window.location.hostname; // 获取域名
console.log(`${protocol}//${hostname}:${port}`);
原文地址: https://www.cveoy.top/t/topic/nXGa 著作权归作者所有。请勿转载和采集!