vue3 使用 vuerouter
时间: 2023-11-18 22:06:13 浏览: 101
在Vue3中使用Vue Router的步骤如下:
1.首先,需要安装Vue Router。可以使用npm或yarn进行安装:
```shell
npm install vue-router@4.0.0-beta.13
```
2.在main.js文件中导入Vue Router并使用它:
```javascript
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')
```
3.创建一个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
```
4.在App.vue文件中使用<router-link>和<router-view>:
```html
<template>
<div id="app">
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>
<router-view></router-view>
</div>
</template>
```
以上是在Vue3中使用Vue Router的基本步骤。
阅读全文