Implement multiple routing implementations in Vue-Router2.X
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
Then the App.vue should write like this:
<template> <p id="app"> <router-view></router-view> </p> </template> index.html里呢要这样写: <body> <p id="app"></p> </body>
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.
In vue-router 2,
<!-- 字符串 --> <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 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() => { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.
