Home >Web Front-end >JS Tutorial >Analysis of vue nested routing and 404 redirection implementation methods

Analysis of vue nested routing and 404 redirection implementation methods

不言
不言Original
2018-05-05 14:12:471439browse

This article mainly introduces the implementation method of vue nested routing and 404 redirection, and analyzes the concepts, principles, implementation steps and related operation skills of vue.js nested routing and 404 redirection in the form of examples. Friends in need You can refer to the following

The example of this article describes the implementation method of vue nested routing and 404 redirection. Share it with everyone for your reference, the details are as follows:

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

How is the official website introduced?

Every time we talk about the official website, we have to talk about Baidu. Never use Baidu search. .

# 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 975b587bf85a482ea10b0a28848e78a4 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: &#39;/user/:id&#39;, component: User,
   children: [
    {
     // 当 /user/:id/profile 匹配成功,
     // UserProfile 会被渲染在 User 的 <router-view> 中
     path: &#39;profile&#39;,
     component: UserProfile
    },
    {
     // 当 /user/:id/posts 匹配成功
     // UserPosts 会被渲染在 User 的 <router-view> 中
     path: &#39;posts&#39;,
     component: UserPosts
    },
    // 当 /user/:id 匹配成功,
    // UserHome 会被渲染在 User 的 <router-view> 中
    {
      path: &#39;&#39;,
      component: UserHome
    },
     // ...其他子路由
   ]
  }
 ]
})

OK! This is roughly nested routing! Among them, the first piece of code is to put 975b587bf85a482ea10b0a28848e78a4 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.

That is to say: In the above code, we write path: "profile" under children, which is equivalent to splicing it into /user/:id/profile. Of course, We can also write it directly as path: "/user/:id/profile". The final result is the same, but I personally think that the latter representation method can clearly observe the structure. If there are too many nesting levels, errors may occur.

Part 2: 404 Redirect

有时,我们可能会输入错的 url, 或者不再存在等各种原因导致其不再能被访问,那么我们就可以对于这种情况进行重定向。 很简单,只要在路由中添加下面的代码就可以了:

{
   path: "*",
   redirect: "/"
}

即对于所有的(*代表所有)错误页面,我们都可以重定向到 "/" 中。

相关推荐:

React为 Vue 引入容器组件和展示组件的教程详解

Vue项目全局配置微信分享思路

The above is the detailed content of Analysis of vue nested routing and 404 redirection implementation methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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