Home > Web Front-end > JS Tutorial > body text

Vue nested routing and 404 redirect use case sharing

php中世界最好的语言
Release: 2018-06-01 11:01:08
Original
1199 people have browsed it

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 , but if we use it, then there will be 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 below this component. When I click on different buttons, their The components pointed to by router-link will be rendered into this .

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 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.

 

Copy after login
const User = {
 template: `
  

   

User {{ $route.params.id }}

      

 ` } const router = new VueRouter({  routes: [   { path: '/user/:id', component: User,    children: [     {      // 当 /user/:id/profile 匹配成功,      // UserProfile 会被渲染在 User 的  中      path: 'profile',      component: UserProfile     },     {      // 当 /user/:id/posts 匹配成功      // UserPosts 会被渲染在 User 的  中      path: 'posts',      component: UserPosts     },     // 当 /user/:id 匹配成功,     // UserHome 会被渲染在 User 的  中     {       path: '',       component: UserHome     },      // ...其他子路由    ]   }  ] })
Copy after login

OK! This is roughly nested routing! Among them, the first piece of code is to put 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: "/"
}
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

怎样使用Node.js内Koa实现JWT用户认证

优化vue项目步骤详解

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!