文章目录
- VueRouter前端路由
- VueRouter 简介
- 安装vuerouter
- 配置router文件
- 子路由
- 路由重定向
VueRouter前端路由
VueRouter 简介
安装vuerouter
npm install vue-router@4
yarn add vue-router@4
配置router文件
import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import About from '../views/About.vue';
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
];
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
});
export default router;
import { createApp } from 'vue';
import App from './App.vue';
import router from './router'; // 导入路由实例
const app = createApp(App);
app.use(router); // 使用路由实例
app.mount('#app');
在App中加入我们定义好的几个标签
<script setup>
import { RouterLink, RouterView } from 'vue-router'
import Movie from './components/Movie.vue'
import Button from './components/Button.vue'
// improt axios from 'axios'
</script>
<template>
<!-- <Movie title="金刚狼"></Movie>
<Button></Button> -->
<router-link to="/my">My</router-link>
<router-link to="/friends">Friends</router-link>
<router-link to="/discover">Discover</router-link>
<RouterView></RouterView>
</template>