当前位置: 首页 > news >正文

日常练习一部分

这一次练习不使用后端,只进行对使用vue3的前端练习,包括两种不同的跳转方式:
我的部分代码大致如下:
DashboardView.vue:

<template><div><h1>仪表盘视图</h1><p>这是您的个人仪表盘,显示各种统计信息。</p><div class="stats-container"><div class="stat-card"><h3>访问量</h3><p class="stat-value">1,254</p></div><div class="stat-card"><h3>用户数</h3><p class="stat-value">548</p></div><div class="stat-card"><h3>完成率</h3><p class="stat-value">89%</p></div></div><button class="btn back-btn" @click="goHome">返回首页</button></div>
</template><script>
export default {methods: {goHome() {this.$router.push({ name: 'home' });}}
}
</script><style scoped>
.stats-container {display: flex;justify-content: center;gap: 20px;margin: 40px 0;
}.stat-card {background: linear-gradient(135deg, #f0f7ff, #e6f2ff);border-radius: 12px;padding: 20px;text-align: center;min-width: 150px;box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}.stat-value {font-size: 2.2rem;font-weight: 700;color: #2c3e50;margin: 10px 0;
}.back-btn {margin-top: 30px;
}
</style>

HomeView.vue``

<template><div><div class="nav-container"><router-link :to="{ name: 'home' }" class="nav-link" active-class="active">首页</router-link><router-link to="/dashboard" class="nav-link" active-class="active">仪表盘</router-link><router-link to="/profile" class="nav-link" active-class="active">个人中心</router-link></div><h1>欢迎来到首页</h1><p>这是一个使用Vue构建的多页面应用示例。点击下方按钮切换到不同视图。</p><div class="button-container"><button class="btn" @click="switchView('dashboard')">查看仪表盘</button><button class="btn" @click="switchView('profile')">查看个人中心</button><button class="btn about-btn" @click="goToAbout">前往关于页</button></div></div>
</template><script>
export default {name: 'HomeView',methods: {switchView(viewName) {this.$router.push({ name: viewName })},goToAbout() {this.addLoadingToButton('.about-btn');setTimeout(() => {window.location.href = 'about.html';}, 500);},addLoadingToButton(selector) {const btn = document.querySelector(selector);if (btn) btn.classList.add('loading');}}
}
</script>

ProfileView.vue

<template><div><h1>个人中心</h1><p>这是您的个人信息页面。</p><div class="profile-card"><div class="avatar"><div class="avatar-initial">U</div></div><div class="profile-info"><h3>用户名</h3><p>user@example.com</p><p>会员状态: <span class="premium">高级会员</span></p></div></div><button class="btn back-btn" @click="goHome">返回首页</button></div>
</template><script>
export default {methods: {goHome() {this.$router.push({ name: 'home' });}}
}
</script><style scoped>
.profile-card {display: flex;align-items: center;gap: 25px;background: white;border-radius: 15px;padding: 25px;margin: 30px auto;max-width: 500px;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
}.avatar {width: 80px;height: 80px;border-radius: 50%;background: linear-gradient(135deg, #6a11cb, #2575fc);display: flex;align-items: center;justify-content: center;
}.avatar-initial {font-size: 2.5rem;font-weight: bold;color: white;
}.profile-info h3 {font-size: 1.8rem;margin-bottom: 10px;
}.premium {color: #ff7e5f;font-weight: 700;
}.back-btn {margin-top: 30px;
}
</style>

router.js:

import { createRouter, createWebHashHistory } from 'vue-router'
// 导入视图组件
const HomeView = () => import('./views/HomeView.vue')
const DashboardView = () => import('./views/DashboardView.vue')
const ProfileView = () => import('./views/ProfileView.vue')const routes = [{path: '/',name: 'home',component: HomeView,meta: { title: '首页' }},{path: '/dashboard',name: 'dashboard',component: DashboardView,meta: { title: '仪表盘' }},{path: '/profile',name: 'profile',component: ProfileView,meta: { title: '个人中心' }}
]const router = createRouter({history: createWebHashHistory(),routes
})router.beforeEach((to) => {document.title = to.meta.title || '默认标题';
});export default router

这一个部分就能实现单页面跳转,如下图:
image

http://www.rkmt.cn/news/4729.html

相关文章:

  • SpringCloud全解:核心组件与实战案例 - 教程
  • 学起plus刷课
  • 202212_风二西_冰蝎流量分析
  • 记账:报表
  • 记账:快速上手
  • Madness - TryHackMe
  • 机器人逆运动学进阶:李代数、矩阵指数与旋转流形计算
  • CSP2025 游记
  • Luogu P14031 【MX-X20-T5】「FAOI-R7」连接时光 II
  • 完整教程:JMeter基本介绍
  • []
  • Source Insight 4.0安装和使用教程
  • ORA-00800
  • 第一周个人作业——我
  • Acrobat Pro DC 2025破解版安装下载教程,附永久免费免中文破解版(稳定版安装包)
  • 华擎、微星、华硕BIOS阵脚线序及杜邦现自制刷机线
  • AT_abc422_f [ABC422F] Eat and Ride 题解
  • 模拟赛 R14
  • Java并发编程(2)
  • 深入解析:精确位置定位,AR交互助力高效作业流程​
  • windows与linux环境下网络编程
  • 在飞牛系统中通过docker形式部署Nginx proxy manager
  • “四人过河”经典问题
  • 完整教程:C#语言入门详解(18)传值、输出、引用、数组、具名、可选参数、扩展方法
  • DevOps On Kubernetes
  • Dify实战训练营(基础班)(全免费值得收藏)
  • PostgreSQL 上的向量搜索实践
  • (读书笔记)平衡掌控者
  • 带头结点的单链表删除指定位置结点
  • 《文字、语言与数字的奇妙联结》读后感,大公司内部编码规范,本学期编码遵守规范