Vue3路由组件练习

Vue3 路由组件练习

1. 演示效果

2. 代码分析

2.1. 安装 vue-router

命令:npm i vue-router

应用插件:Vue.use(VueRouter)

2.2. 创建路由文件

在 src 文件夹下,创建router 文件夹,并在该文件夹创建index.js文件

2.3. 创建路由实例

使用 createRouter 函数创建一个路由器实例,并传入一个 history 参数,该参数是一个用于记录路由变更的 history 对象。

这里使用了环境变量 BASE_URL,从 import.meta.env.BASE_URL 中获取基础 URL。

组件的加载方式是通过闭包实现。在每个路由对象中,使用 component: () => import("../components/{componentName}.vue") 加载组件。

js 复制代码
import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: "/useHook",
			name: "useHook",
			component: () => import("../components/UseHooks.vue"),
		},
		{
			path: "/musicList",
			name: "musicList",
			component: () => import("../components/MusicList.vue"),
		},
		{
			path: "/purchaseCar",
			name: "purchaseCar",
			component: () => import("../components/PurchaseCar.vue"),
		},
	],
});

export default router;

App.vue 文件中,我们使用 router-link 组件导航,通过传入 to 属性指定链接。<router-link> 默认会被渲染成一个 <a> 标签

<router-view> 标签中,我们使用了默认的路由组件。当你点击路由链接时,Vue Router 会根据指定的目标路由渲染相应的路由组件。

js 复制代码
<template>
  <div class="wrapper">
    <router-link to="/" style="margin-right: 10px;">Go to Home</router-link><br>
    <router-link to="/musicList">Go to MusicList</router-link><br>
    <router-link to="/purchaseCar">Go to PurchaseCar</router-link><br>
    <router-link to="/useHook">Go to UseHook</router-link><br>

  </div>
  <br /><br />
  <router-view>
  </router-view>
</template>

<script setup>

</script>


<style scoped>
#wrapper {
  width: 100vw;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
}
</style>

3. 代码实现

3.1. index.js 文件

js 复制代码
import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: "/useHook",
			name: "useHook",
			component: () => import("../components/UseHooks.vue"),
		},
		{
			path: "/musicList",
			name: "musicList",
			component: () => import("../components/MusicList.vue"),
		},
		{
			path: "/purchaseCar",
			name: "purchaseCar",
			component: () => import("../components/PurchaseCar.vue"),
		},
	],
});

export default router;

3.2. App 文件

js 复制代码
<template>
  <div class="wrapper">
    <router-link to="/" style="margin-right: 10px;">Go to Home</router-link><br>
    <router-link to="/musicList">Go to MusicList</router-link><br>
    <router-link to="/purchaseCar">Go to PurchaseCar</router-link><br>
    <router-link to="/useHook">Go to UseHook</router-link><br>

  </div>
  <br /><br />
  <router-view>
  </router-view>
</template>

<script setup>

</script>


<style scoped>
#wrapper {
  width: 100vw;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
}
</style>
相关推荐
人工智能训练师5 小时前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
Seveny076 小时前
pnpm相对于npm,yarn的优势
前端·npm·node.js
yddddddy6 小时前
css的基本知识
前端·css
昔人'7 小时前
css `lh`单位
前端·css
前端君7 小时前
实现最大异步并发执行队列
javascript
破无差7 小时前
《赛事报名系统小程序》
小程序·html·uniapp
Nan_Shu_6148 小时前
Web前端面试题(2)
前端
知识分享小能手8 小时前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue
2501_918126919 小时前
用html5写一个flappybird游戏
css·游戏·html5
蚂蚁RichLab前端团队9 小时前
🚀🚀🚀 RichLab - 花呗前端团队招贤纳士 - 【转岗/内推/社招】
前端·javascript·人工智能