vue路由出口(router-view)为和路由入口(router-link)呈兄弟元素无法准确匹配跳转解决方案

5 篇文章 0 订阅
5 篇文章 0 订阅

1.问题背景

如图所示,路由出口和入口为兄弟元素,页面还有其它路由,会发生子级路由不能准确匹配,无法渲染数据   

 

2.踩坑记录

布局页面,设置 "router-link" 和 "router-view"

配置路由

页面效果

 

3.解决方案

   设置路由出口时,给router-view,设置name属性 

<!--路由出口-->
<transition name="el-fade-in-linear">
   <div class="main-content">
       <router-view name="mainArea"/>  /* 设置路由出口时,给router-view,设置name属性 */
   </div>
</transition>

  配置路由跳转:使用components,name/default:component (此处语法使用详情:vue嵌套命名视图)

routes: [
        {
            path: '/',
            /*重定向*/
            redirect: Index
        },
        {
            path: '/index',
            name: 'index',
            component: Index,
            children: [
                {
                    path: 'mainContent',
                    components:{
                        mainArea:mainContent /*使用components,name/default:component*/
                    }
                }
            ]
        }
    ]

 

4.最终效果

 

Vue 3的router-viewrouter-linkVue Router提供的两个核心组件,用于实现路由导航和视图渲染的功能。 router-link是一个用于生成链接的组件,它会自动根据路由配置生成对应的链接,并在用户点击时触发路由跳转。你可以使用to属性指定链接的目标路由,例如to='home'表示跳转到名为home的路由router-view是一个用于渲染视图的组件,它会根据当前路由的路径匹配路由配置中的组件进行渲染。在你的应用中,你可以将router-view放置在合适的位置,它会根据当前路由的变化自动切换渲染的组件。 通过在Vue组件中使用router-linkrouter-view组件,你可以实现简单而灵活的路由导航和视图切换功能,让用户可以方便地浏览不同的页面内容。 请注意,以上是Vue 3中使用router-viewrouter-link的基本用法。具体的配置和使用方式可能会因你的项目需求和Vue Router的版本而有所不同。如果你遇到了具体的问题和报错,请提供更多的信息以便我能够更详细地回答你的问题。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [【JavaScript源代码】Vue router-viewrouter-link的实现原理.docx](https://download.csdn.net/download/mmoo_python/72117437)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue3引入vue-router无法使用router-linkrouter-view组件](https://blog.csdn.net/m0_67108146/article/details/125198929)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值