Home > Article > Web Front-end > Vue nested routing and 404 redirect use case sharing
This time I will bring you vue nesting routing and 404 redirection use cases sharing, what are the precautions for using vue nesting routing and 404 redirection, the following are Let’s take a look at practical cases.
Part 1: vue nested routing
What is nested routing?
Nested routing means that you can continue to use routing under a routed page. Nesting means routing in routing. For example, in vue, if we do not use nested routing, there will only be one <router-view>
, but if we use it, then there will be <router-view> in a component.
, this also constitutes nesting.
Why use nested routing?
For example, in a page, there are three buttons in the upper part of the page, and the lower part displays different content based on clicking different buttons, then we can use this component The lower part is regarded as a nested route, which means that there needs to be another <router-view>
below this component. When I click on different buttons, their The components pointed to by router-link will be rendered into this <router-view>
.
How is the official website introduced?
Every time we talk about the official website, we have to talk about Baidu. BaiduSearchDon’t use it. .
# The official website cited this picture, the intention is good, but the description is too cumbersome. Make a complaint. .
This is a good application interface in real life, usually composed of multiple layers of nested components. Similarly, each dynamic path segment in the URL also corresponds to each layer of nested components according to a certain structure. As shown above.
That is, user represents the user page, and user can be regarded as a single page in vue. For a user, there must be a user. The foo (Xiao Ming, Xiao Hong) here represents a User, the profile here can be understood as a personal homepage, and the posts here can be understood as articles published by this person, and the title may not change, for example, whether you switch to the articles published by this person or switch to this person's personal homepage , we all want to display the same thing at the top, and what we change when switching is the lower part. We can use <router-view> to write this part, then this is nested routing.
With the help of vue-router, this relationship can be easily expressed using nested routing configuration.
<p id="app"> <router-view></router-view> </p>
const User = { template: ` <p class="user"> <h2>User {{ $route.params.id }}</h2> <router-view></router-view> </p> ` } const router = new VueRouter({ routes: [ { path: '/user/:id', component: User, children: [ { // 当 /user/:id/profile 匹配成功, // UserProfile 会被渲染在 User 的 <router-view> 中 path: 'profile', component: UserProfile }, { // 当 /user/:id/posts 匹配成功 // UserPosts 会被渲染在 User 的 <router-view> 中 path: 'posts', component: UserPosts }, // 当 /user/:id 匹配成功, // UserHome 会被渲染在 User 的 <router-view> 中 { path: '', component: UserHome }, // ...其他子路由 ] } ] })
OK! This is roughly nested routing! Among them, the first piece of code is to put <router-view>
in the file. This is the top-level exit, which renders the components matched by the advanced route.
From the routing configuration, we can see: When the path is /user/Xiaoming or /user/小红, the User will be rendered into the page, and this rendering is the top-level route. (:id is Xiao Ming and Xiao Hong,). Then there is a nested route in this page. Of course, if the URL is /user/Xiao Ming, then this secondary route will not display anything. If we want even if it is just /user/Xiao Ming, we have to render something. , then we can set the route corresponding to path: "", so that even /user/Xiao Ming can display more.
If we want to see Xiao Ming's personal homepage, it is /user/Xiao Ming/profile. Then the UserProfile component will be rendered into this secondary route.
If we want to read the articles published by Xiao Ming, it is /user/Xiao Ming/posts. At this time, the UserPost component will be rendered into this secondary route, which is also the secondary route.
In fact, there are only so many concepts, isn’t it very simple!
But you also need to pay attention to the following points:
1. As can be seen from the above code: In the component, we can pass $route.params .id
to get Xiao Ming in user/Xiao Ming/post, so that we can make personalized settings.
2. Nested paths starting with / will be regarded as the root path, which allows you to fully use nested components without setting nested paths.
也就是说: 在上面的代码中,我们在children下,写path: "profile", 就相当于拼接成了 /user/:id/profile ,当然,我们也可以直接写为 path: "/user/:id/profile" ,最终的结果是一样的,但是我个人认为后者的表示方法可以跟清晰的观察结构。 如果嵌套层数过多,可能会出错。
第二部分: 404重定向
有时,我们可能会输入错的 url, 或者不再存在等各种原因导致其不再能被访问,那么我们就可以对于这种情况进行重定向。 很简单,只要在路由中添加下面的代码就可以了:
{ path: "*", redirect: "/" }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Vue nested routing and 404 redirect use case sharing. For more information, please follow other related articles on the PHP Chinese website!