Installation and configuration method of vue-route routing management

Installation and configuration method of vue-route routing management

introduce

Vue Router is the official routing manager for Vue.js It is deeply integrated with the core of Vue.js , making it easy to build single-page applications. Features included are:

  • Nested routes/view tables
  • Modular, component-based routing configuration
  • Route parameters, queries, wildcards
  • View transition effects based on Vue.js transition system
  • Fine-grained navigation control
  • Links with auto-activated CSS classes
  • HTML5 history mode or hash mode, automatically downgraded in IE9
  • Custom scroll bar behavior

Install

Installation Commands

npm install vue-router --save

If you use it in a modular project, you must explicitly install the routing function via Vue.use() :

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

Modular use

When we used the scaffolding vue-cli to create a project before, router was actually configured. After the project was created, there would be a router folder in the project root directory, and there would be an index.js file under router , with the following content:

import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

// 1. When we use other plug-ins, we must use Vue.use to install the plug-in Vue.use(VueRouter);

// 2. Define routes, each route should map to a component const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/about",
    name: "About",
    component: About
  },
];

// 3. Create a router instance const router = new VueRouter({
  //Configure the application relationship between routes and components routes, // (abbreviation) equivalent to routes: routes
});

// 4. Export the router object, and then reference export default router in main.js;

This file is specifically for configuring routing. Finally, after exporting router object, we can reference it in main.js of the project.

import Vue from "vue";
import App from "./App.vue";
import router from "./router";

Vue.config.productionTip = false;

new Vue({
  router, // Add the router object to the vue instance, and you can use routing render: (h) => h(App),
}).$mount("#app");

Our two component codes About and Home are as follows:

// About.vue
<template>
  <div class="about">
    <h1>About</h1>
  </div>
</template>

<script>
export default {
  name: "About"
}
</script>

<style scoped>
</style>

// Home.vue
<template>
  <div class="home">
    <h1>Home</h1>
  </div>
</template>

<script>

export default {
  name: "Home",
};
</script>

<style scoped>
</style>

Finally, we write the following code in App.vue :

template>
  <div id="app">
    <router-link to="/">Home</router-link>
    <router-link to="/about">About</router-link>
    <router-view></router-view>
  </div>
</template>

<style lang="scss">
</style>

Use <router-link> to load the link, and then use to represent the link to jump to. Finally, <router-link> will be rendered into an <a> tag.
<router-view> is the outlet of the route, that is, the code under the corresponding url will be rendered to this place.

HTML5 history mode

But when we start the program and access the page, # will appear on url address

This is because vue-router defaults to hash mode - using URL 's hash to simulate a full URL , so the page will not reload when URL changes.
If we don't want the ugly hash , we can use the history mode of the router, which makes full use of history.pushState API to complete URL jump without reloading the page.

const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

We just need to add mode to history in index.js under router folder, and then revisit http://localhost:8080/ without the # sign.

Note: history mode also requires background configuration support. Because our application is a single-page client application, if the backend is not configured correctly, when the user directly accesses other url addresses in the browser, 404 will be returned, which is not good.

Therefore, you need to add a candidate resource on the server to cover all situations: if URL does not match any static resource, it should return the same index.html page, which is the page your app depends on.

This is the end of this article about the installation and configuration of vue-route routing management. For more relevant vue route installation and configuration content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solve the problem that vue-router nested routing does not respond
  • Solve the dead loop problem caused by vue-router routing interception
  • vue solves addRoutes multiple times to add routes and repeat operations
  • Solve one of the reasons why vue+router routing jump does not work
  • Solve the problem that the content of the reused component of vue update router-view does not refresh
  • Vue implements permission control routing (vue-router dynamically adds routing)
  • Vue routing object attribute.meta $route.matched detailed explanation

<<:  Detailed example of MySQL (5.6 and below) parsing JSON

>>:  Detailed explanation of the solution to the problem of Ubuntu system interface being too small in vmware14Pro

Recommend

How to migrate the data directory in Docker

Table of contents View Disk Usage Disk Cleanup (D...

Getting Started Tutorial for Beginners ⑨: How to Build a Portal Website

Moreover, an article website built with a blog pro...

The whole process record of vue3 recursive component encapsulation

Table of contents Preface 1. Recursive components...

How to obtain a permanent free SSL certificate from Let's Encrypt in Docker

1. Cause The official cerbot is too annoying. It ...

The impact of limit on query performance in MySQL

I. Introduction First, let me explain the version...

A brief discussion on the optimization of MySQL paging for billions of data

Table of contents background analyze Data simulat...

mysql 5.7.20 win64 installation and configuration method

mysql-5.7.20-winx64.zipInstallation package witho...

How does the MySQL database implement the XA specification?

MySQL consistency log What happens to uncommitted...

After reading the introduction of CSS box model, you will not be confused

The property names often heard in web design: con...

Mysql 5.6.37 winx64 installation dual version mysql notes

If MySQL version 5.0 already exists on the machin...

What is flex and a detailed tutorial on flex layout syntax

Flex Layout Flex is the abbreviation of Flexible ...