vue3 vue-router集成使用

本文介绍了如何在Vue3项目中集成并使用vue-router,包括安装路由、创建router文件夹及index.js配置、在main.js中挂载路由对象,以及在App.vue中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.安装路由

npm i vue-router

2.新建router文件夹在里面新建一个index.js文件

 3.刚刚在router中的index.js配置

注意:路由自行配置

import { createRouter, createWebHistory } from 'vue-router'
const routerHistory = createWebHistory()
const router = createRouter({
    history: routerHistory,
    routes: [
        {
            path: '/',
            component: () => import('../components/HelloWorld.vue')
        }, {
            path: '/aa',
            component: () => import('../view/aa.vue')
        }, {
            path: '/bb',
            component: () => import('../view/bb.vue')
        }
    ]
})
export default router

4.在main.js将路由对象,挂载到 Vue 实例上:

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

createApp(App).use(router).mount('#app')

5.使用App.vue

<template>

	<div id="app">
		<!-- 导航 -->
		<router-link to="/" active-class="current" replace>Home</router-link> |
		<router-link to="/aa" active-class="current" replace>aa</router-link> |
		<router-link to="/bb">bb</router-link>
		<!-- 路由出口 -->
		<router-view />
	</div>
</template>

<style>
	#app {
		font-family: Avenir, Helvetica, Arial, sans-serif;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
		text-align: center;
		color: #2c3e50;
		margin-top: 60px;
	}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值