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

vue 路由

文章目录

  • 作用
  • 如何设置路由
  • 注意点
  • 路由器工作模式
  • to的两种写法(跳转去哪里的链接的写法)
  • 命名路由(后续可以用名字进行跳转)
  • 嵌套路由(多个层级的路由)

作用

一句话说完,就是用来,跳转页面的

如何设置路由

效果:
在这里插入图片描述

整体框架的图
在这里插入图片描述
components 代码
About.vue 简单的显示主键

<template><div class="about"><h2>大家好</h2></div>
</template><script setup lang="ts" name="About"></script><style scoped>
.about {display: flex;justify-content: center;align-items: center;height: 100%;color: rgb(85, 84, 84);font-size: 18px;
}
</style>

Home.vue 也就是简单的弄了一张图片而已

<template><div class="home"><img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" alt=""></div>
</template><script setup lang="ts" name="Home"></script><style scoped>.home {display: flex;justify-content: center;align-items: center;height: 100%;}
</style>

New.vue 新闻组件

<template><div class="news"><ul><li><a href="#">新闻001</a></li><li><a href="#">新闻002</a></li><li><a href="#">新闻003</a></li><li><a href="#">新闻004</a></li></ul></div>
</template><script setup lang="ts" name="News"></script><style scoped>
/* 新闻 */
.news {padding: 0 20px;display: flex;justify-content: space-between;height: 100%;
}
.news ul {margin-top: 30px;list-style: none;padding-left: 10px;
}
.news li>a {font-size: 18px;line-height: 40px;text-decoration: none;color: #64967E;text-shadow: 0 0 1px rgb(0, 84, 0);
}
.news-content {width: 70%;height: 90%;border: 1px solid;margin-top: 20px;border-radius: 10px;
}
</style>

然后是主页组件:App.vue

<template><div class="app"><h2 class="title">Vue路由测试</h2><!-- 导航区 --><div class="navigate"><RouterLink to="/home" active-class="xiaozhupeiqi">首页</RouterLink><RouterLink to="/news" active-class="xiaozhupeiqi">新闻</RouterLink><RouterLink to="/about" active-class="xiaozhupeiqi">关于</RouterLink></div><!-- 展示区 --><div class="main-content"><RouterView></RouterView></div></div>
</template><script lang="ts" setup name="App">import {RouterView,RouterLink} from 'vue-router'</script><style>/* App */.title {text-align: center;word-spacing: 5px;margin: 30px 0;height: 70px;line-height: 70px;background-image: linear-gradient(45deg, gray, white);border-radius: 10px;box-shadow: 0 0 2px;font-size: 30px;}.navigate {display: flex;justify-content: space-around;margin: 0 100px;}.navigate a {display: block;text-align: center;width: 90px;height: 40px;line-height: 40px;border-radius: 10px;background-color: gray;text-decoration: none;color: white;font-size: 18px;letter-spacing: 5px;}.navigate a.xiaozhupeiqi {background-color: #64967E;color: #ffc268;font-weight: 900;text-shadow: 0 0 1px black;font-family: 微软雅黑;}.main-content {margin: 0 auto;margin-top: 30px;border-radius: 10px;width: 90%;height: 400px;border: 1px solid;}
</style>

然后是main.ts

// 引入createApp用于创建应用
import {createApp} from 'vue'
// 引入App根组件
import App from './App.vue'
// 引入路由器
import router from './router'// 创建一个应用
const app = createApp(App)
// 使用路由器
app.use(router)
// 挂载整个应用到app容器中
app.mount('#app')

这一节最重要的,路由组件 index.ts

// 创建一个路由器,并暴露出去// 第一步:引入createRouter
import {createRouter,createWebHistory} from 'vue-router'
// 引入一个一个可能要呈现组件
import Home from '@/components/Home.vue'
import News from '@/components/News.vue'
import About from '@/components/About.vue'// 第二步:创建路由器
const router = createRouter({history:createWebHistory(), //路由器的工作模式(稍后讲解)routes:[ //一个一个的路由规则{path:'/home',component:Home},{path:'/news',component:News},{path:'/about',component:About},]
})// 暴露出去router
export default router

注意点

  1. 路由组件通常存放在pagesviews文件夹,一般组件通常存放在components文件夹。

  2. 通过点击导航,视觉效果上“消失” 了的路由组件,默认是被卸载掉的,需要的时候再去挂载

路由器工作模式

history模式

优点:URL更加美观,不带有#,更接近传统的网站URL

缺点:后期项目上线,需要服务端配合处理路径问题,否则刷新会有404错误。

const router = createRouter({history:createWebHistory(), //history模式/******/
})

hash模式

优点:兼容性更好,因为不需要服务器端处理路径。

缺点:URL带有#不太美观,且在SEO优化方面相对较差。

const router = createRouter({history:createWebHashHistory(), //hash模式/******/
})

to的两种写法(跳转去哪里的链接的写法)

<!-- 第一种:to的字符串写法 -->
<router-link active-class="active" to="/home">主页</router-link><!-- 第二种:to的对象写法 -->
<router-link active-class="active" :to="{path:'/home'}">Home</router-link>

命名路由(后续可以用名字进行跳转)

作用:可以简化路由跳转及传参)。

给路由规则命名:

routes:[{name:'zhuye',path:'/home',component:Home},{name:'xinwen',path:'/news',component:News,},{name:'guanyu',path:'/about',component:About}
]

跳转路由:

<!--简化前:需要写完整的路径(to的字符串写法) -->
<router-link to="/news/detail">跳转</router-link><!--简化后:直接通过名字跳转(to的对象写法配合name属性) -->
<router-link :to="{name:'guanyu'}">跳转</router-link>

嵌套路由(多个层级的路由)

  1. 编写News的子路由:Detail.vue

  2. 配置路由规则,使用children配置项:

const router = createRouter({history:createWebHistory(),routes:[{name:'zhuye',path:'/home',component:Home},{name:'xinwen',path:'/news',component:News,children:[{name:'xiang',path:'detail',component:Detail}]},{name:'guanyu',path:'/about',component:About}]
})
export default router
  1. 跳转路由(记得要加完整路径):
<router-link to="/news/detail">xxxx</router-link>
<!---->
<router-link :to="{path:'/news/detail'}">xxxx</router-link>
  1. 记得去Home组件中预留一个<router-view>
<template><div class="news"><nav class="news-list"><RouterLink v-for="news in newsList" :key="news.id" :to="{path:'/news/detail'}">{{news.name}}</RouterLink></nav><div class="news-detail"><RouterView/></div></div>
</template>

相关文章:

  • JavaSE学习笔记第二弹——对象和多态(下)
  • 等保测评视角下的哈尔滨智慧城市安全框架构建
  • 2019年美赛题目Problem A: Game of Ecology
  • 硅纪元AI应用推荐 | 百度橙篇成新宠,能写万字长文
  • C++ 判断语句的深入解析
  • 【北京迅为】《i.MX8MM嵌入式Linux开发指南》-第一篇 嵌入式Linux入门篇-第十八章 Linux编写第一个自己的命令
  • PageDTO<T>,PageQuery,BeanUtils,CollUtils的封装
  • openfoam生成的非均匀固体Solid数据分析、VTK数据格式分析、以及paraview官方用户指导文档和使用方法
  • 昇思15天
  • sdwan是硬件还是网络协议?
  • html+css+js仿黑客帝国代码雨
  • 应对高并发请求:服务器性能压力的解决方案
  • TLS与SSL的区别
  • 手机下载APP (uniapp/vue)
  • 矩阵分解及其在机器学习中的应用
  • 【5+】跨webview多页面 触发事件(二)
  • 【剑指offer】让抽象问题具体化
  • 【刷算法】从上往下打印二叉树
  • 10个确保微服务与容器安全的最佳实践
  • httpie使用详解
  • JavaScript 基础知识 - 入门篇(一)
  • JavaScript设计模式与开发实践系列之策略模式
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • MySQL的数据类型
  • mysql外键的使用
  • nodejs调试方法
  • SAP云平台里Global Account和Sub Account的关系
  • swift基础之_对象 实例方法 对象方法。
  • Vue--数据传输
  • web标准化(下)
  • 给新手的新浪微博 SDK 集成教程【一】
  • 关于Java中分层中遇到的一些问题
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 如何设计一个比特币钱包服务
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • ionic入门之数据绑定显示-1
  • #NOIP 2014# day.1 T2 联合权值
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (6)设计一个TimeMap
  • (javaweb)Http协议
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (vue)页面文件上传获取:action地址
  • (不用互三)AI绘画工具应该如何选择
  • (二)原生js案例之数码时钟计时
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (强烈推荐)移动端音视频从零到上手(下)
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (原創) X61用戶,小心你的上蓋!! (NB) (ThinkPad) (X61)