vue-router is the official routing plug-in of Vue.js. It is deeply integrated with vue.js and is suitable for building single-page applications. Vue's single-page application is based on routing and components. Routing is used to set access paths and map paths and components. Traditional page applications use some hyperlinks to achieve page switching and jumps. In the vue-router single-page application, it is switching between paths, that is, switching of components.
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. 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 components, here use import from other files
import index from './components/index.vue' import hello from './components/hello.vue'
2. Define routes
const routes = [ { path: '/index', component: index }, { path: '/hello', component: hello }, ]
3. Create a router instance, and then pass the 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
Then the App.vue should be written like this:
<template> <p id="app"> <router-view></router-view> </p> </template>
index.html should be written like this:
<body> <p id="app"></p> </body>
This will render the The page that comes out is mounted in 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) }, ]
Through lazy loading, all components will not be loaded at once, but only when you access that component will it be loaded. For applications with many components, the first loading speed will be improved.
5.
In vue-router 1, use
In vue-router 2, use
<!-- 字符串 --> <router-link to="home">Home</router-link> <!-- 渲染结果 --> <a href="home" rel="external nofollow" >Home</a> <!-- 使用 v-bind 的 JS 表达式 --> <router-link v-bind:to="'home'">Home</router-link> <!-- 不写 v-bind 也可以,就像绑定别的属性一样 --> <router-link :to="'home'">Home</router-link> <!-- 同上 --> <router-link :to="{ path: 'home' }">Home</router-link> <!-- 命名的路由 --> <router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link> <!-- 带查询参数,下面的结果为 /register?plan=private --> <router-link :to="{ path: 'register', query: { plan: 'private' }}">Register</router-link>
6. Routing information object
1.$route.path
String, corresponding to the current The path of the route is always resolved as 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')
Related recommendations:
Example details thinkphp5 URL and routing function
Node.js custom method to implement file routing function
detailed explanation of nodeJS implementation of routing function example
The above is the detailed content of Vue-Router2 implements routing function example explanation. For more information, please follow other related articles on the PHP Chinese website!