【VUE】vue-router

1. vue-router组件

1.1 路由的嵌套

index.js

import {createRouter, createWebHistory} from 'vue-router'const router = createRouter({history: createWebHistory(import.meta.env.BASE_URL),routes: [{path: '/login',name: 'login',component: () => import('../views/LoginView.vue')},{path: '/info',name: 'info',component: () => import('../views/InfoView.vue')},{path: '/admin',name: 'admin',component: () => import('../views/AdminView.vue'),children: [{path: "",redirect: {name: "mine"}},{path: "mine",name: "mine",component: () => import('../views/MineView.vue')},{path: "order",name: "order",component: () => import('../views/OrderView.vue')}]}]
})
export default router

App.vue

<template><div class="page-header"><div class="container"><RouterLink to="/login">登录</RouterLink> |<RouterLink to="/info">信息</RouterLink> |<RouterLink to="/admin">后台</RouterLink> |<RouterLink to="/admin/mine">我的</RouterLink></div></div><div class="container"><RouterView/></div>
</template><script setup>
</script><style scoped>
.page-header {height: 48px;background-color: cornflowerblue;line-height: 48px;
}.container {width: 980px;margin: 0 auto;
</style>

AdminView.vue

<template><h1>Admin</h1><router-link to="/admin/mine">我的</router-link> |<router-link to="/admin/order">订单</router-link><RouterView/>
</template><script setup>
</script><style scoped>
</style>

1.2 编程式导航

不使用<router-link>标签实现路由的跳转。
使用js代码实现路由跳转,例如:
InfoView.vue

<template><h1>Info</h1>
</template><script setup>
import {useRouter} from "vue-router"const router = useRouter()
// router.push({path:"/admin/order"})
// router.push({name:"login"})
router.push({name: "login", params: {nid: 100}, query: {page: 100}})
</script><style scoped></style>

浏览器的后退前进缓存中,有以下区别:
push [A,B,C,D,新页面]
replace [A,B,C,新页面]

案例1:博客+公司官网+xx在线平台

  • 未登录,可以查看某些页面

  • 用户登录,去登录

  • 去查看某些页面

    现象:登录页是在有导航条的前提

App.vue

<template><div class="page-header"><div class="container"><RouterLink to="/info">信息</RouterLink>|<RouterLink to="/admin">后台</RouterLink>|<RouterLink to="/admin/mine">我的</RouterLink><div style="float:right;"><RouterLink to="/login">登录</RouterLink></div></div></div><div class="container"><RouterView/></div>
</template><script setup></script><style scoped>
body {margin: 0;
}.page-header {height: 48px;background-color: cornflowerblue;line-height: 48px;
}.page-header a {display: inline-block;padding: 0 10px;
}.container {width: 980px;margin: 0 auto;
}</style>

LoginView.vue

<template><div class="box"><p>用户名:<input type="text" placeholder="用户名" v-model="msg.username"></p><p>密码:<input type="text" placeholder="密码" v-model="msg.password"></p><p><button @click="doLogin">登录</button></p></div>
</template><script setup>
import {ref} from "vue";
import {useRouter} from "vue-router";const msg = ref({username: "",password: ""
})
const router = useRouter()const doLogin = function () {// 1、获取数据console.log(msg.value)// 2、发送网络请求// 3、跳转到首页router.push({name:"info"})
}</script><style scoped>
.box {width: 300px;margin: 100px auto;
}
</style>

案例2:后台管理

现象:登录页面,无导航条的前提。未登录时,不允许查看系统中有哪些菜单。
index.js

import {createRouter, createWebHistory} from 'vue-router'const router = createRouter({history: createWebHistory(import.meta.env.BASE_URL),routes: [{path: '/login',name: 'login',component: () => import('../views/LoginView.vue')},{path: '/admin',name: 'admin',component: () => import('../views/AdminView.vue'),children: [{path: "",redirect: {name: "mine"}},{path: "mine",name: "mine",component: () => import('../views/MineView.vue')},{path: "order",name: "order",component: () => import('../views/OrderView.vue')}]}]
})export default router

App.vue

<template><div class="container"><RouterView/></div>
</template><script setup>
</script><style scoped>
body {margin: 0;
}
.container {width: 980px;margin: 0 auto;
}
</style>

LoginView.vue

<template><div class="box"><p>用户名:<input type="text" placeholder="用户名" v-model="msg.username"></p><p>密码:<input type="text" placeholder="密码" v-model="msg.password"></p><p><button @click="doLogin">登录</button></p></div>
</template><script setup>
import {ref} from "vue";
import {useRouter} from "vue-router";const msg = ref({username: "",password: ""
})
const router = useRouter()const doLogin = function () {// 1、获取数据console.log(msg.value)// 2、发送网络请求// 3、跳转到首页router.push({name:"mine"})
}
</script><style scoped>
.box {width: 300px;margin: 100px auto;
}
</style>

AdminView.vue

<template><div class="page-header"><div class="container"><RouterLink to="/admin/mine">我的</RouterLink>|<RouterLink to="/admin/order">订单</RouterLink></div></div><div class="container"><RouterView/></div>
</template><script setup>
</script><style scoped>
body {margin: 0;
}
.page-header {height: 48px;background-color: cornflowerblue;line-height: 48px;
}
.page-header a {display: inline-block;padding: 0 10px;
}
.container {width: 980px;margin: 0 auto;
}
</style>

1.3 导航守卫

请求之前,路由之前,进行拦截。
守护着每一个路由。

浏览器的本地存储

  • cookie:有效期+保存
  • localStorage:永久保存
  • sessionStorage:浏览器关闭后自动消失

index.js

import {createRouter, createWebHistory} from 'vue-router'const router = createRouter({history: createWebHistory(import.meta.env.BASE_URL),routes: [{path: '/login',name: 'login',component: () => import('../views/LoginView.vue')},{path: '/admin',name: 'admin',component: () => import('../views/AdminView.vue'),children: [{path: "",redirect: {name: "mine"}},{path: "mine",name: "mine",component: () => import('../views/MineView.vue')},{path: "order",name: "order",component: () => import('../views/OrderView.vue')}]}]
})
router.beforeEach(function (to,from,next) {// 1.访问登录页面,不需要登录就可以直接去查看if (to.name === "login") {next()return}// 2.检查用户登录状态,登录成功,继续往后走next();未登录,跳转至登录页面let username = localStorage.getItem("name")if (!username){next({name:"login"})return;}// 3.登录成功且获取到用户信息,继续向后访问next()
})
export default router

LoginView.vue

<template><div class="box"><p>用户名:<input type="text" placeholder="用户名" v-model="msg.username"></p><p>密码:<input type="text" placeholder="密码" v-model="msg.password"></p><p><button @click="doLogin">登录</button></p></div>
</template><script setup>
import {ref} from "vue";
import {useRouter} from "vue-router";const msg = ref({username: "",password: ""
})
const router = useRouter()const doLogin = function () {// 1、获取数据console.log(msg.value)// 2、发送网络请求// 3、本地存储用户信息(登录凭证)localStorage.setItem("name", msg.value.username)// 3、跳转到首页router.push({name:"mine"})
}</script><style scoped>
.box {width: 300px;margin: 100px auto;
}
</style>

AdminView.vue

<template><div class="page-header"><div class="container"><RouterLink to="/admin/mine">我的</RouterLink>|<RouterLink to="/admin/order">订单</RouterLink><div style="float:right;"><a @click="doLogout">退出</a></div></div></div><div class="container"><RouterView/></div>
</template><script setup>
import {useRouter} from "vue-router";const router = useRouter()
function doLogout() {localStorage.clear()router.push({name:"login"})
}
</script><style scoped>
body {margin: 0;
}.page-header {height: 48px;background-color: cornflowerblue;line-height: 48px;
}.page-header a {display: inline-block;padding: 0 10px;cursor: pointer;
}.container {width: 980px;margin: 0 auto;
}</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1544881.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

2-102基于matlab的蒙特卡洛仿真

基于matlab的蒙特卡洛仿真&#xff0c;对64QAM和BPSK进行蒙特卡洛仿真&#xff0c;并绘出误码率曲线。程序已调通&#xff0c;可直接运行。 下载源程序请点链接&#xff1a; 2-102基于matlab的蒙特卡洛仿真

【FPGA必知必会】(二)7系列的配置(一)

配置概述 7系列FPGA是通过将bitstream下载到内存中来实现配置的。 既可以通过外部非易失性存储器加载&#xff0c;也可以通过微处理器、DSP处理器、微控制器、PC或者板级测试仪进行加载。 有两种通用的配置路径&#xff0c;一种是串行数据路径&#xff0c;用于减少对器件引脚…

数据丢失不再怕!四款神器助你找回一切

哈喽&#xff0c;大家好&#xff01;今天咱们来聊聊数据恢复工具&#xff1b;在数字化的时代&#xff0c;数据丢失可是个让人头疼的问题&#xff1b;不过别担心&#xff0c;有了这些数据恢复工具&#xff0c;再也不用担心数据不见&#xff1b;下面我给大家推荐五款非常好用的数…

【systemctl start jenkins】启动报错问题解决

问题说明&#xff0c;最终是在jenkins.service中配置JAVA_HOME解决的&#xff0c;但是我的服务器环境中确定已经配置好了Java环境变量&#xff0c;并且java -version也能正常打印信息&#xff0c;不清楚为什么jenkins.service无法读取配置 1.环境配置说明 服务器&#xff1a;…

如何确定SAP 某些凭证或者单号的号码编码范围的 OBJECT 是什么?

在SAP的运维或者项目实施中&#xff0c;有时会如何确定SAP 某些凭证或者单号的号码 OBJECT 是什么&#xff1f; 一般一下常用的可以通过事务代码 例如&#xff1a; XDN1 Create Number Ranges for Customer Accounts&#xff0c;定义客户编码FBN1查看维护会计凭证号范围 我…

破解 oklink 网站加密数据(升级版)

大家好!我是炒青椒不放辣,关注我,收看每期的编程干货。 逆向是爬虫工程师进阶必备技能,当我们遇到一个问题时可能会有多种解决途径,而如何做出最高效的抉择又需要经验的积累。本期文章将以实战的方式,带你详细地分析并破解 oklink 网站加密数据 特别声明:本篇文章仅供学…

屏幕演示工具 | 水豚鼠标助手 v1.0.7

水豚鼠标助手是一款功能强大的屏幕演示工具&#xff0c;专为Windows 10及以上系统设计。这款软件提供了多种实用功能&#xff0c;旨在增强用户的屏幕演示体验&#xff0c;特别适合教师、讲师和需要进行屏幕演示的用户。鼠标换肤&#xff1a;软件提供多种鼠标光标样式&#xff0…

深兰科技陈海波应邀出席2024长三角论坛暨虹桥人才创新发展大会

近日&#xff0c;以“人才引领 联动共融——国际化创新与长三角协同”为主题的“2024长三角人才发展论坛暨虹桥人才创新发展大会”在上海国际会议中心隆重举行。上海市委常委、组织部部长、市委人才办主任张为应邀出席并做大会致辞。 深兰科技创始人、董事长陈海波作为特邀企业…

跑lvs出现soft connect怎么处理?

首先&#xff0c;我们先了解一下什么是soft connect。简而言之&#xff0c;就是工具会将所有连接在psub上的信号认作soft connect&#xff08;也就是short&#xff09;。如图1所示&#xff0c;VSS和AVSS都接到了p上&#xff0c;它们通过psub便有了soft connect。 如果有soft co…

SQLServer运维实用的几个脚本

目录 1、查询出最近所有耗时最大的SQL语句 2、查询数据库每个数据表存储占用 3、当前正在执行的最耗时的前10个SQL语句 4、SQLServer查看锁表和解锁 5、快速清理数据库日志文件 1、查询出最近所有耗时最大的SQL语句 返回的是未关联任何特定对象的最耗费资源的查询信息,包…

剖解相交链表

相交链表 思路&#xff1a;我们计算A和B链表的长度&#xff0c;求出他们的差值&#xff08;len&#xff09;&#xff0c;让链表长的先多走len步&#xff0c;最后在A,B链表一起向后走&#xff0c;即可相逢于相交节点 实现代码如下&#xff1a; public class Solution {public …

string 的介绍及使用

一.string类介绍 C语言中&#xff0c;字符串是以’\0’结尾的一些字符的集合&#xff0c;为了操作方便&#xff0c;C标准库中提供了一些str系列的库函数&#xff0c;但是这些库函数与字符串是分离开的&#xff0c;不太符合OOP的思想&#xff0c;而且底层空间需要用户自己管理&a…

服务设计原则介绍

在Java或任何软件开发中&#xff0c;设计服务时遵循一些核心原则是非常重要的&#xff0c;这些原则不仅有助于构建高质量、可维护的软件系统&#xff0c;还能提高系统的可扩展性和可重用性。以下是一些关键的服务设计原则&#xff1a; 单一职责原则&#xff08;SingleResponsib…

个人量化成功之路-----获取实时OHLC的数据

昨天有一个客户说自己之前交易主要看OHLC线&#xff0c;想问量化软件如何实现获取实时一分钟OHLC的数据并生产图像。 有朋友可能不熟悉OHLC这个名字哈&#xff0c;其实跟K线/蜡烛图的数据是一样的&#xff0c;和蜡烛图的区别只是表现形式的不一致。 O为open、开盘价&#xff…

第6章 常用UI组件库

一.Element Plus组件库 1. 安装Element Plus 什么是Element Plus&#xff1f; Element Plus是基于Vue 3开发的优秀的PC端开源UI组件库&#xff0c;它是Element的升级版&#xff0c;对于习惯使用Element的人员来说&#xff0c;在学习Element Plus时&#xff0c;不用花费太多的…

如何使用ssm实现基于Java的超市管理系统

TOC ssm681基于Java的超市管理系统jsp 绪论 1.1 研究背景 当前社会各行业领域竞争压力非常大&#xff0c;随着当前时代的信息化&#xff0c;科学化发展&#xff0c;让社会各行业领域都争相使用新的信息技术&#xff0c;对行业内的各种相关数据进行科学化&#xff0c;规范化…

BUUCTF [SCTF2019]电单车详解两种方法(python实现绝对原创)

使用audacity打开&#xff0c;发现是一段PT2242 信号 PT2242信号 有长有短&#xff0c;短的为0&#xff0c;长的为1化出来 这应该是截获电动车钥匙发射出的锁车信号 0 01110100101010100110 0010 0前四位为同步码0 。。。中间这20位为01110100101010100110为地址码0010为功…

macOS设置 Redis自启动

macOS自定义开机启动程序 1、打开 自动操作app里面的应用程序 过程资料 1、https://juejin.cn/post/7123098435254747149 2、https://blog.twofei.com/889/ 2、编写脚本&#xff0c;可以点击右上角运行测试&#xff0c;保存为 app https://juejin.cn/post/7123098435254747149…

全能的Office插件——不坑盒子 2024.0923发布,云同步配置、合并单元格复制、PPT样机展示……

昨天凌晨&#xff0c;不坑盒子上线了2024.0923版本&#xff0c;这次更新的功能比较多&#xff0c;亮点较多&#xff0c;有必要发文推荐给大家&#xff01; 向新人介绍 不坑盒子是一款全能的Office插件&#xff0c;支持微软Office和WPS Office的办公三件套&#xff08;Word、E…

众数信科 AI智能体政务服务解决方案——AI法律助手

政务服务解决方案 AI法律助手 一款基于AI大模型的智能鼠标 搭裁“寻知AI法律助手” 众数信科AI智能体 产品亮点 能够结合全国各地案例数据 为用户提供法律咨询、文书生成、案例研判 类案推荐、法规检索等法律服务 同时结合Al office插件 具备AI智能办公、PPT生成等功能 …