"页面刷新或点击链接后,vue router的最后一个路由不会被渲染"
P粉277824378
P粉277824378 2023-08-26 10:27:45
[Vue.js讨论组]
<p>当我点击菜单链接时,我的最后一个vue-router组件正常加载,但当我分享链接或刷新页面时,组件不会渲染。我不知道可能是什么问题,因为它只发生在最后一个路由上。</p> <p>这是一个链接:<a href="https://www.hvarboating.com/speed-boat-hire-hvar-flyer747">https://www.hvarboating.com/speed-boat-hire-hvar-flyer747</a></p> <p>我的路由器:</p> <pre class="brush:php;toolbar:false;">import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) export const routes = [ { path: '/', name: 'Home', component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/Home') }, { path: '/blue-caves-croatia', name: 'GroupToursDetails', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/GroupToursDetails') }, { path: '/boat-tours', name: 'BoatTours', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/BoatTours') }, { path: '/hvar-boat-rental', name: 'BoatRentals', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/BoatRentals') }, { path: '/from-split-to-Hvar-transfer', name: 'BoatTransfers', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/BoatTransfers') }, { path: '/hvar-weather', name: 'Weather', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/Weather') }, { path: '/frequently-asked-questions', name: 'Faq', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/Faq') }, { path: '/contact', name: 'Contact', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/Contact') }, { path: '/:id', meta: { sitemap: { slugs: [ 'blue-cave-tour-from-hvar', 'best-beaches-in-hvar-private', 'zlatni-rat-brac-island', 'boat-party-tour' ] } }, name: 'details', props:true, // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/PrivateToursDetails') }, { path: '/:id', meta: { sitemap: { slugs: [ 'speed-boat-hire-hvar-flyer747', 'luxury-boat-hire-hvar-tornado38', ] } }, name: 'rentals', props:true, // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () =&gt; import(/* webpackChunkName: &quot;about&quot; */ '../views/BoatRentDetails') }, ] const router = new VueRouter({ scrollBehavior() { return {x: 0, y: 0} }, mode: 'history', base: process.env.BASE_URL, routes, }) export default router</pre>
P粉277824378
P粉277824378

全部回复(1)
P粉020085599

你的最后两个路由具有相同的路径/:id,所以如果你通过路由名称切换路由,它可以正常工作,但是当你刷新或使用链接时,它不知道你想要使用哪个路由,因此无法渲染组件。

解决方案:为每个路由使用唯一的路径

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号