vue3实现router路由

说明:
vue3实现router路由
效果图:
在这里插入图片描述

step1:项目结构

src/
├── views/
│   ├── Home.vue
│   └── User.vue
├── router/
│   └── index.js
├── App.vue
└── main.js

step2:左边路由列表C:\Users\wangrusheng\PycharmProjects\untitled3\src\App.vue

<!-- App.vue -->
<template>
  <div class="container">
    <!-- 左侧导航 -->
    <div class="sidebar">
      <h2>路由列表</h2>
      <nav>
        <router-link to="/home" active-class="active-link">首页</router-link>
        <router-link to="/user" active-class="active-link">用户中心</router-link>
      </nav>
    </div>

    <!-- 右侧内容 -->
    <div class="content">
      <router-view></router-view>
    </div>
  </div>
</template>

<style>
.container {
  display: flex;
  height: 100vh;
}

.sidebar {
  width: 200px;
  background-color: #f0f0f0;
  padding: 20px;
  border-right: 1px solid #ddd;
}

.sidebar nav {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sidebar a {
  padding: 10px;
  text-decoration: none;
  color: #333;
  border-radius: 4px;
  transition: all 0.3s;
}

.sidebar a:hover {
  background-color: #e0e0e0;
}

.active-link {
  background-color: #4CAF50;
  color: white !important;
}

.content {
  flex: 1;
  padding: 20px;
}
</style>

step3:配置路由 C:\Users\wangrusheng\PycharmProjects\untitled3\src\router\index.js

// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import User from '../views/User.vue'

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/home', name: 'Home', component: Home },
  { path: '/user', name: 'User', component: User }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

step4:主界面引入配置js C:\Users\wangrusheng\PycharmProjects\untitled3\src\main.js

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


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


step5:home C:\Users\wangrusheng\PycharmProjects\untitled3\src\views\Home.vue

<!-- src/views/Home.vue -->
<template>
  <div class="content">
    <h1>Home Page</h1>
    <p>Hello World from Home!</p>
  </div>
</template>

<script>
export default {
  name: 'HomeView'
}
</script>

step6:user C:\Users\wangrusheng\PycharmProjects\untitled3\src\views\User.vue

<!-- src/views/User.vue -->
<template>
  <div class="content">
    <h1>User Page</h1>
    <p>Hello World from User!</p>
  </div>
</template>

<script>
export default {
  name: 'UserView'
}
</script>

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值