配置全局路由
第一步:安裝插件螟蒸。
npm install vue-router@4
Vue Router 中文文檔地址
(鏈接如果失效就自行搜索官網(wǎng))
第二步:配置相關(guān)文件和項(xiàng)目引入肮之。
- 1、在src目錄下創(chuàng)建一個新的文件夾誉结,命名為
router
。 - 2券躁、在
router
文件夾中惩坑,創(chuàng)建一個新的TS文件掉盅,命名為index.ts
,并編寫以下代碼:
import { createRouter, createWebHistory } from 'vue-router';
import Home from '../layout/home.vue';
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
// 添加其他路由
];
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;
- 3以舒、在src目錄下創(chuàng)建一個新的文件夾趾痘,命名為
layout
。 - 4蔓钟、在
layout
文件夾中永票,創(chuàng)建一個新的VUE文件,命名為home.vue
滥沫,并編寫以下代碼:
<template>
<h1>VVT-WEB</h1>
</template>
<script>
</script>
<style>
</style>
- 5侣集、在src目錄下找到
main.ts
,引入router兰绣。
main.ts
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router/index'
createApp(App).use(router).mount('#app')
- 7世分、在src目錄下找到
App.vue
,加上路由展示標(biāo)簽<router-view></router-view>
缀辩。
App.vue
...
<template>
<router-view></router-view>
</template>
...
如果順利的話臭埋,你會得到一個這樣的項(xiàng)目: