Home  >  Article  >  Web Front-end  >  Implement multiple routing implementations in Vue-Router2.X

Implement multiple routing implementations in Vue-Router2.X

亚连
亚连Original
2018-06-06 16:03:131440browse

Below I will share with you a summary of various routing implementation methods of Vue-Router2.X. It has a good reference value and I hope it will be helpful to everyone.

Note: vue-router 2 is only applicable to Vue2.x version. Below we will talk about how to use vue-router 2 to implement the routing function based on vue2.0.

It is recommended to use npm to install.

npm install vue-router

1. Using 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, here we use import from other files

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

2. Define the route

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

3. Create a router instance, and then Pass routes configuration

const router = new VueRouter({
 routes
})

4. Create and mount the root instance. Inject routes through router configuration parameters, so that the entire application has routing functions

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

After the above configuration, the components matched by the routes will be rendered to the 213f57d44141993d0a8a50589ff82e3bdd6e4ababe59793a4ac75fb9e5c5550e

Then the App.vue should write like this:


index.html里呢要这样写:

 

This will render it The page is mounted to the p with the id of app.

2. Redirect redirect

const routes = [
 { path: '/', redirect: '/index'},  // 这样进/ 就会跳转到/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) },
]

No need to use lazy loading All components will be loaded at once, and only that component will be loaded when you access that component. For applications with many components, the first loading speed will be improved.

5. b988a8fd72e5e0e42afffd18f951b277

In vue-router 2, b988a8fd72e5e0e42afffd18f951b2770e259af55a139990f31da244f503061cReplace the a tag in version 1


Home

Home

Home

Home

Home

User

Register

6. Routing information object

1.$route.path

String, corresponding to the path of the current route, is always resolved to an absolute path, such as "/foo/bar".

2.$route.params

A key/value object, including dynamic fragments and full matching fragments. If there are no routing parameters, it is an empty object.

3.$route.query

A key/value object representing URL query parameters. For example, for the path /foo?user=1, $route.query.user == 1, or an empty object if there are no query parameters.

4.$route.hash

The hash value of the current route (without #), if there is no hash value, it is an empty string.

5.$route.fullPath

The URL after completion of parsing, including the full path of query parameters and hash.

6.$route.matched

An array containing the routing records of all nested path fragments of the current route. Route records are copies of objects in the routes configuration array (and in the children array).

Based on the above, a main.js including 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')

The above is what I compiled Everyone, I hope it will be helpful to everyone in the future.

Related articles:

How to integrate vue into jquery/bootstrap projects?

Implementing paging in vue.js by clicking the page number to change the page content

How to implement value passing and communication in the vue2.0 component

The above is the detailed content of Implement multiple routing implementations in Vue-Router2.X. 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
Previous article:react project developmentNext article:react project development