Laravel Inertia Ziggy链接路由问题:带ID参数后导航404
解决Inertia.js中嵌套路由下的导航路径问题
问题核心:你在/category/{id}页面点击导航链接时出现404,是因为导航用了相对路径,浏览器会基于当前URL拼接出/category/{id}/dashboard这类错误路径,而非根路径下的/dashboard。
直接解决方案:把导航数组里的href改成绝对路径(开头加斜杠/):
const navigation = [ { name: 'Dashboard', href: '/dashboard', current: false }, { name: 'Category', href: '/category', current: false }, ]
修改后,Inertia的Link组件会从根路径开始解析URL,无论当前处于哪个嵌套路由下,点击导航都会跳转到正确的根路径页面,完全符合Inertia的路由规则。
附修改后的完整导航代码:
<nav class="hidden lg:flex lg:space-x-8 lg:py-2" aria-label="Global"> <Link v-for="item in navigation" :key="item.name" :href="item.href" :class="[ item.current ? 'bg-gray-100 text-gray-900' : 'text-gray-900 hover:bg-gray-50 hover:text-gray-900', 'rounded-md py-2 px-3 inline-flex items-center text-sm font-medium' ]" :aria-current="item.current ? 'page' : undefined"> {{ item.name }} </Link> </nav>
内容的提问来源于stack exchange,提问作者JSON-SE
相关产品推荐
相关产品推荐

