Vue3配置路由
- 安装
vue-router
npm install vue-router
- 创建一个路由
index.js
:
import { createRouter,createWebHistory} from "vue-router";
import Page from '../components/page.vue';
import PriView from '../components/PriView.vue';
const routes= [{path: '/',component: Page,title: "/"},{path: '/pricy',component: PriView,title: '/pricy',},
]const router = createRouter({history: createWebHistory(),routes
})
export default router
- 在
main.js
中添加路由:
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router/index.js';
const app = createApp(App);
app.use(router);
app.mount('#app');
- 在
App.vue
中添加路由组件:
<template><router-view />
</template>
- 使用路由:
<div><router-link to="/pricy">Privacy policy</router-link></div>