Home >Web Front-end >Vue.js >How to select the routing mode in Vue Router?

How to select the routing mode in Vue Router?

WBOY
WBOYOriginal
2023-07-21 11:43:58901browse

Vue Router is the routing manager officially provided by Vue.js. It can help us implement page navigation and routing functions in Vue applications. When using Vue Router, we can choose different routing modes according to actual needs.

Vue Router provides 3 routing modes, namely hash mode, history mode and abstract mode. The following will introduce in detail the characteristics of these three routing modes and how to choose the appropriate routing mode.

  1. Hash mode (default mode)
    In hash mode, the URL address will be separated by # symbols, and changes in the URL will not Trigger the reload of the page, but switch the page by listening to the hashchange event. This mode is relatively simple, does not require special server configuration, and can be accessed directly through the browser. For example, when we visit http://www.example.com/#/home, we are actually accessing the page http://www.example.com, and then Listen to the hashchange event through Vue Router and switch to the corresponding component according to #/home.

The code to enable Hash mode is as follows:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'hash', // 设置路由模式为hash模式
  routes: [
    // ...
  ]
})

export default router
  1. History mode
    In history mode, the URL address is the real URL, not If the # symbol is needed again, by calling the browser's history.pushState and history.replaceState methods, you can change the URL address without triggering a page reload. . This mode is more friendly and beautiful, but it requires special configuration support on the server to avoid a 404 error when directly accessing a URL, because the real URL does not exist on the server.

The code to enable History mode is as follows:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history', // 设置路由模式为history模式
  routes: [
    // ...
  ]
})

export default router
  1. Abstract mode
    abstract mode is one that does not support history Or the routing mode in the browser environment of hash mode. It is mainly used for using Vue Router in non-browser environments, such as Node.js environments or native Apps. In this mode, the URL address is virtual. The URL address is changed through the browser's pushState and replaceState methods, and the browser's popstate event is listened to. Route switching.

The code to enable Abstract mode is as follows:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'abstract', // 设置路由模式为abstract模式
  routes: [
    // ...
  ]
})

export default router

Select the routing mode according to actual needs. If it is just a simple single-page application, it is recommended to use the default hash mode, if you need a better user experience, you can choose the history mode (requires server configuration support). The abstract mode is mainly used for applications in non-browser environments.

To summarize, Vue Router provides three routing modes: hash, history and abstract. Just choose the appropriate mode according to actual needs. . Different modes have different characteristics and usage scenarios. Reasonable selection of routing modes can better meet the needs of page navigation and routing management.

The above is the detailed content of How to select the routing mode in Vue Router?. 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