Quasar中使用Vue Router resolve获取命名路由href的方法咨询
在Quasar中通过Vue Router生成带Hash的路由链接
你的思路方向是对的,但router.resolve返回的href是包含完整域名的URL(比如http://localhost:8080/#/download),并非直接得到/#/download。要获取你需要的格式,需要做两步处理:
1. 确保路由模式为Hash模式
首先确认Quasar的路由配置是Hash模式(这是生成带#链接的前提),在src/router/index.js中配置如下:
import { createRouter, createWebHashHistory } from 'vue-router' import routes from './routes' const router = createRouter({ // 使用Hash模式 history: createWebHashHistory(), routes }) export default router
2. 正确提取/拼接目标格式的链接
有两种简单方式获取/#/download格式的字符串:
方式一:从完整URL中截取带#的部分
<script setup> import { useRouter } from 'vue-router' const router = useRouter() const resolved = router.resolve({ name: 'download' }) // 从完整href中截取#及之后的部分 const myhref = resolved.href.slice(resolved.href.indexOf('#')) // 此时myhref的值就是 "/#/download" </script>
方式二:手动拼接(更直接)
因为Hash模式下,router.resolve返回的fullPath是不带#的路由路径(比如/download),直接拼接前缀即可:
<script setup> import { useRouter } from 'vue-router' const router = useRouter() const fullPath = router.resolve({ name: 'download' }).fullPath const myhref = `/#${fullPath}` // 结果同样是 "/#/download" </script>
内容的提问来源于stack exchange,提问作者user3705497
相关产品推荐
相关产品推荐

