vue route方法
## How to use Vue router?
### Introduction.
Vue router is a routing library for Vue.js applications. It provides features such as declarative routing, nested views, and route parameters.
### Installation.
To install Vue router, run the following command:
sh.
npm install vue-router.
### Creating a Vue router instance.
To create a Vue router instance, you can use the `VueRouter` constructor. The constructor
takes an object as an argument, which can contain the following properties:
`routes`: An array of route objects.
`mode`: The routing mode (either 'hash' or 'history')。
`base`: The base URL for the router.
`linkActiveClass`: The class applied to active links.
`linkExactActiveClass`: The class applied to exact active links.
js.
import Vue from 'vue'。
import VueRouter from 'vue-router'。
Vue.use(VueRouter)。
const router = new VueRouter({。
routes: [。
{ path: '/', component: Home },。
{ path: '/about', component: About }。
],。
mode: 'history',。
base: '/my-app'。
})。
### Routing in Vue components.
To use Vue router in your Vue components, you can use the following methods:
`$router`: The Vue router instance.
`$route`: The current route.
route add 添加路由 `$router.push(path)`: Push a new route to the history stack.
`$place(path)`: Replace the current route with a new one.
`$(n)`: Go back or forward in the history stack by `n` steps.
js.
<template>。
<div>。
<h1>{{ $route.name }}</h1>。
<a @click="$router.push('/about')">About</a>。
</div>。
</template>。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论