vue3怎么进行页面跳转
时间: 2023-07-25 16:15:59 浏览: 38
在 Vue3 中,可以使用 `router` 进行页面跳转。
首先需要在项目中安装 Vue Router:
```
npm install vue-router@4.0.0-beta.8
```
然后在 main.js 中引入并使用:
```javascript
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')
```
创建一个 router.js 文件,用于配置路由信息:
```javascript
import { createRouter, createWebHistory } from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
```
其中,`routes` 数组中配置了路由信息,`path` 表示路由路径,`name` 表示路由名称,`component` 表示对应的组件。
在组件中,可以使用 `<router-link>` 标签实现页面跳转:
```html
<template>
<div>
<h1>Home</h1>
<router-link to="/about">About</router-link>
</div>
</template>
```
其中,`to` 属性表示要跳转的路由路径。
也可以使用 `router.push()` 方法进行编程式跳转:
```javascript
this.$router.push('/about')
```
这样就可以实现页面跳转了。