截取浏览器url
function getBowserUrl(path) { let result = ''; if (path) { // 获取当前URL var currentURL = window.location.href; // 使用URL API来解析URL var urlObject = new URL(currentURL); // 如果URL中有端口号,它会包含在host属性中 // 如果你想要单独处理端口号,可以这样做: var port = urlObject.port ? ':' + urlObject.port : ''; let pathPrefixWithPort = urlObject.protocol + '//' + urlObject.hostname + port; result = pathPrefixWithPort + path; } return result; }
组件里面使用:这里用的element plus:
<el-link :href="getBowserUrl('/bidding/management')" target="_blank" :underline="false"> // '/bidding/management'对应路由 <div class="gateIcon"> <el-icon><Bell /></el-icon> </div> <div class="gateIconTitle">招标公告</div> </el-link>
标签:URL,环境,拼接,urlObject,result,var,path,port,外链 From: https://www.cnblogs.com/shyhuahua/p/18068190