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

Vue nested routing and 404 redirect implementation method analysis_vue.js

php中世界最好的语言
Release: 2018-05-23 15:10:35
Original
1697 people have browsed it

This time I will show you how to use vue nesting routing, what are the precautions for using vue nesting routing, the following is a practical case, let's take a look.

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>
Copy after login
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
    },
     // ...其他子路由
   ]
  }
 ]
})
Copy after login

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

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!

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website Other related articles!

Recommended reading:

How to use vue to register components

Summary of how to use data-* attributes in H5

The above is the detailed content of Vue nested routing and 404 redirect implementation method analysis_vue.js. 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