In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
小编给大家分享一下vue.js中Router嵌套路由怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
项目结构如下:
我们创建了3个组件,分别是Home.vue,HomeNews.vue,HomeMessage.vue,代码如下:
Home.vue
Home 新闻类 // 注意这里一定要写完整路径不能只写/news,需要加上/home 信息类 export default { name: "Home",};
HomeNews
新闻1 新闻2 新闻3 新闻4 export default { name: "HomeNews"}
HomeMessage
消息1 消息2 消息3 消息4 export default { name: "HomeMessage"}
组件写完以后,我们在router文件夹下的index.js文件中配置路由
import Vue from "vue";import VueRouter from "vue-router";Vue.use(VueRouter);// 这里还是使用路由懒加载const Home = () => import('../views/Home')const HomeNews = () => import('../views/HomeNews')const HomeMessage = () => import('../views/HomeMessage')const routes = [ { path: "/home", name: "Home", component: Home, // 子路由的写法 children: [ { path: "news", name: "HomeNews", component: HomeNews }, { path: "message", name: "HomeMessage", component: HomeMessage }, ] }, { path: "", redirect: "home" }];const router = new VueRouter({ routes, mode: 'history',});export default router;
嵌套路由的写法很简单,你会发现,children 配置就是像 routes 配置一样的路由配置数组,所以呢,你可以嵌套多层路由。
此时,基于上面的配置,当你访问 /home/时,home 的出口是不会渲染任何东西。
这是因为没有匹配到合适的子路由。如果想要渲染点什么,可以提供一个 空的 子路由:
const routes = [ { path: "/home", name: "Home", component: Home, children: [ { path: "news", name: "HomeNews", component: HomeNews }, { path: "message", name: "HomeMessage", component: HomeMessage }, // 新增空的子路由 { path: "", redirect: "news" } ] }, { path: "", redirect: "home" }];
这样页面就默认会重定向到news页面,会展示news的信息
以上是"vue.js中Router嵌套路由怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.