vue3 router-view
时间: 2023-08-29 09:12:48 浏览: 145
The `router-view` component in Vue 3 is used for rendering the component associated with the current route. It acts as a placeholder where the route-specific content is inserted.
To use `router-view` in a Vue 3 application, you need to follow these steps:
1. Install the `vue-router` package by running the following command:
```
npm install vue-router
```
2. Import the `createRouter` and `createWebHistory` functions from the `vue-router` package in your main.js file:
```js
import { createRouter, createWebHistory } from 'vue-router'
```
3. Create a router instance using the `createRouter` function and pass an array of route objects as its argument. Each route object should have a `path` property and a `component` property that specifies the component to be rendered for that route:
```js
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About },
{ path: '/contact', component: Contact }
]
const router = createRouter({
history: createWebHistory(),
routes
})
```
4. Import the router instance and use it in your Vue app by wrapping it with the `router-view` component:
```html
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
```
The `router-view` component will then render the appropriate component based on the current route.
Note: Make sure that the components used in the routes are imported and registered in your Vue app.
阅读全文