How to use Vue-router routing

How to use Vue-router routing

1. Description

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 effect based on Vue js transition system
  • Fine-grained navigation control
  • Links with auto-activated CSS classes
  • HTML5 history mode or hash mode, automatic downgrade in IE 9
  • Custom scrolling behavior

2. Installation

Test and learn based on the first vue-cli; First check whether vue-router exists in node modules
vue-router is a plug-in package, so we still need to use npm/cnpm to install it. Open the command line tool, enter your project directory, and enter the following command.

npm install vue-router --save-dev

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);

3. Testing

1. Delete useless things first
2. The components we wrote ourselves are stored in the components directory
3. Define a Content.vue component

<template>
	<div>
		<h1>Contents Page</h1>
	</div>
</template>

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

Main.vue component

<template>
	<div>
		<h1>Home</h1>
	</div>
</template>

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

4. Install the router. In the src directory, create a new folder: router to store the router and configure the router index.js as follows

import Vue from 'vue'
//Import routing plugin import Router from 'vue-router'
//Import the components defined above import Content from '../components/Content'
import Main from '../components/Main'
//Install routing Vue.use(Router);
//Configure routing export default new Router({
	routes:[
		{
			//Route path path:'/content',
			//Route name name:'content',
			// Jump to component component:Content
			},
  {
			//Route path path:'/main',
			//Route name name:'main',
			// Jump to component component:Main
		}
	]
});

5. Configure routing in main.js

import Vue from 'vue'
import App from './App'

//Import the routing configuration directory created above import router from './router' //Automatically scan the routing configuration inside //To turn off the prompt given in production mode Vue.config.productionTip = false;

new Vue({
	el:"#app",
	//Configure routing router,
	components:{App},
	template:'<App/>'
});

6. Use routing in App.vue

<template>
	<div id="app">
		<!--
			router-link: By default, it will be rendered as an <a> tag, and the to attribute is the specified link router-view: used to render the component matched by the route -->
		<router-link to="/main">Home</router-link>
		<router-link to="/content">content</router-link>
		<router-view></router-view>
	</div>
</template>

<script>
	export default{
		name:'App'
	}
</script>

<style></style>

The above is the detailed content of how to use Vue-router routing. For more information about the use of Vue-router routing, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of vue-router 4 usage examples
  • Detailed explanation of the use of router-view components in Vue
  • A simple example of using Vue3 routing VueRouter4
  • Detailed explanation of the installation and use of Vue-Router
  • Complete steps to use vue-router in vue3
  • Do you know how to use Router in vue3.0?

<<:  MySQL 5.7.18 installation and configuration method graphic tutorial (CentOS7)

>>:  Tutorial on how to deploy LNMP and enable HTTPS service

Recommend

The use of v-model in vue3 components and in-depth explanation

Table of contents Use two-way binding data in v-m...

Teach you 10 ways to center horizontally and vertically in CSS (summary)

A must-have for interviews, you will definitely u...

Detailed installation process of nodejs management tool nvm

nvm nvm is responsible for managing multiple vers...

MySQL uses inet_aton and inet_ntoa to process IP address data

This article will introduce how to save IP addres...

How to set up a deployment project under Linux system

1. Modify the firewall settings and open the corr...

How to split and merge multiple values ​​in a single field in MySQL

Multiple values ​​combined display Now we have th...

The difference between Display, Visibility, Opacity, rgba and z-index: -1 in CSS

We often need to control the hidden, transparent ...

HTML Tutorial: Collection of commonly used HTML tags (5)

Related articles: Beginners learn some HTML tags ...

Vue uses OSS to upload pictures or attachments

Use OSS to upload pictures or attachments in vue ...

Mac+IDEA+Tomcat configuration steps

Table of contents 1. Download 2. Installation and...

Detailed explanation of the murder caused by a / slash in Nginx proxy_pass

background An nginx server module needs to proxy ...

Detailed explanation of VUE responsiveness principle

Table of contents 1. Responsive principle foundat...

How to adapt CSS to iPhone full screen

1. Media query method /*iPhone X adaptation*/ @me...