Summary of various routing implementation methods in Vue-Router2.X, vue-router2.x Routing

Source: Internet
Author: User

Summary of various routing implementation methods in Vue-Router2.X, vue-router2.x Routing

Note: vue-router 2 is only applicable to Vue2.x. The following describes how to use vue-router 2 to implement Routing Based on vue2.0.

We recommend that you use npm for installation.

npm install vue-router

1. Use Routing

In main. js, You need to explicitly install the routing function:

import Vue from 'vue'import VueRouter from 'vue-router'import App from './App.vue'Vue.use(VueRouter)

1. Define the component, which is imported from other files.

import index from './components/index.vue'import hello from './components/hello.vue'

2. Define a route

const routes = [ { path: '/index', component: index }, { path: '/hello', component: hello },]

3. Create a router instance and pass the routes Configuration

const router = new VueRouter({ routes})

4. Create and mount the root instance. Configure parameters to inject routes through the router, so that the entire application has the routing function.

const app = new Vue({ router, render: h => h(App)}).$mount('#app')

After the above configuration, the components matching the route will be rendered to <router-view> </router-view> in App. vue.

The App. vue should be written as follows:

<Template> <div id = "app"> <router-view> </div> </template> index.html: <body> <div id = "app"> </div> </body>

In this way, the rendered page will be mounted to the div with the id of app.

2. redirect

Const routes = [{path: '/', redirect: '/Index'}, // in this way,/will jump to/index {path:'/Index', component: index}]

3. nested Routing

const routes = [ { path: '/index', component: index,  children: [   { path: 'info', component: info}  ]  }]

You can access the info component through/index/info.

4. Lazy Loading

const routes = [ { path: '/index', component: resolve => require(['./index.vue'], resolve) }, { path: '/hello', component: resolve => require(['./hello.vue'], resolve) },]

If you load the component Through laziness, it will not load all the components at one time, but it will be loaded only when you access the component. Applications with many components increase the loading speed for the first time.

V. <router-link>

In vue-router 2, Use <router-link> </router-link> to replace the tag in version 1.

<! -- String --> <router-link to = "home"> Home </router-link> <! -- Rendering result --> <a href = "home" rel = "external nofollow"> Home </a> <! -- Use the JS expression of v-bind --> <router-link v-bind: to = "'home'"> home </router-link> <! -- Do not write v-bind, just like binding other attributes --> <router-link: to = "'home'"> home </router-link> <! -- Same as above --> <router-link: to = "{path: 'home'}"> home </router-link> <! -- Named route --> <router-link: to = "{name: 'user', params: {userId: 123 }}"> user </router-link> <! -- With query parameters, the following result is/register? Plan = private --> <router-link: to = "{path: 'register ', query: {plan: 'private'}"> register </router-link>

Vi. Routing Information Objects

1. $ route. path

String, which corresponds to the current route path. It is always resolved to an absolute path, for example, "/foo/bar ".

2. $ route. params

A key/value object contains dynamic segments and fully-matched segments. If there is no route parameter, it is an empty object.

3. $ route. query

A key/value object, indicating URL query parameters. For example, for path/foo? User = 1, $ route. query. user = 1. If no query parameter exists, it is an empty object.

4. $ route. hash

The hash value of the current route (without #). If there is no hash value, it is a null string.

5. $ route. fullPath

The URL after resolution, including the query parameters and the complete hash path.

6. $ route. matched

An array that contains route records of all nested path segments of the current route. A route record is a copy of the objects in the routes configuration array (also in the children array ).

Based on the above, a main. js that includes redirection, nested routing, and lazy loading is as follows:

import Vue from 'vue'import VueRouter from 'vue-router'import App from './App'Vue.use(VueRouter)const router = new VueRouter({ routes:[ { path: '/', redirect: '/index' }, { path: '/index', component: resolve => require(['./components/index.vue'], resolve),  children:[   { path: 'info', component: resolve => require(['./components/info.vue'], resolve) }  ] }, { path: '/hello', component: resolve => require(['./components/hello.vue'], resolve) }, ]})const app = new Vue({ router, render: h => h(App)}).$mount('#app')

For more detailed vue-router features, see:

Summary of the above Vue-Router2.X a variety of routing implementation is small make up to share with you all the content, hope to give you a reference, also hope you can support a lot of help house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.