Vue23全面知识总结七(2)

news/2024/5/3 1:50:32/文章来源:https://blog.csdn.net/m0_52781902/article/details/126903994

感兴趣的朋友可以去我的语雀平台进行查看更多的知识。
https://www.yuque.com/ambition-bcpii/muziteng

7.8 路由的props配置

props作用:让路由组件更方便的收到参数

{name:'detail',path:'detail/:id',component:Detail,//第一种写法:props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件// props:{a:900}//第二种写法:props值为布尔值,为true时,则把路由收到的所有params参数通过props传给Detail组件// props:true//第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件props($route){return {id: $route.query.id,title: $route.query.title}}
}

实例:

src/router/index.js

// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router';
// 引入组件
import Home from "@/pages/Home";
import About from "@/pages/About";
import News from "@/pages/News";
import Message from "@/pages/Message";
import Detail from "@/pages/Detail";// 创建并暴露一个路由器
export default new VueRouter({routes: [{path: '/home',component: Home,children: [{path: "news",component: News,},{path: "message",component: Message,children: [{name: "detail", // name配置项为路由命名path: "detail/:id/:title",  // 使用占位符声明接受params参数component: Detail,// props的第一种写法,值为对象,该对象中的key-value都会以props的形式传给Detail组件// props: {a: 1, b: 'hello'}// props的第二种写法,值为布尔值,若布尔值为真,就会把该路由组件收到的params参数,以props的形式传给Detail组件// props: true,// props的第三种写法,值为函数props($route) { // 这里可以使用解析赋值return {id: $route.params.id,title: $route.params.title,}}}]}]},{path: '/about',component: About,}]
})

src/pages/Message.vue

<template><div><ul><li v-for="m in messageList" :key="m.id"><!--跳转并携带params参数,to的对象写法(推荐)--><router-link:to="{name: 'detail', // 使用name进行跳转params: {id: m.id,title: m.title,}}">{{ m.title }}</router-link></li></ul><hr/><router-view></router-view></div>
</template><script>
export default {name: "Message",data() {return {messageList: [{id: '001', title: '消息001'},{id: '002', title: '消息002'},{id: '003', title: '消息003'},]}}
}
</script>

src/pages/Detail.vue

<template><ul><li>消息编号:{{ id }}</li><li>消息标题:{{ title }}</li></ul>
</template><script>
export default {name: "Detail",props: ['id', 'title']
}
</script>

7.9 路由跳转的replace方法

  1. 作用:控制路由跳转时操作浏览器历史记录的模式
  2. 浏览器的历史记录有两种写入方式:pushreplace
    push是追加历史记录
    replace是替换当前记录,路由跳转时候默认为push方式
  3. 开启replace模式
    <router-link :replace="true" ...>News</router-link>
    简写<router-link replace ...>News</router-link>
  4. 总结:浏览记录本质是一个栈,默认push,点开新页面就会在栈顶追加一个地址,后退,栈顶指针向下移动,改为replace就是不追加,而将栈顶地址替换

src/App.vue

<template><div><Banner/><div class="row"><div class="col-xs-2 col-xs-offset-2"><div class="list-group"><!-- Vue中借助router-link标签实现路由的切换 --><router-link replace class="list-group-item" active-class="active" to="/about">About</router-link><router-link replace class="list-group-item" active-class="active" to="/home">Home</router-link></div></div><div class="col-xs-6"><div class="panel"><div class="panel-body"><!-- 指定组件的呈现位置 --><router-view></router-view></div></div></div></div></div>
</template><script>
import Banner from "@/components/Banner";export default {name: 'App',components: {Banner},
}
</script>

image-20220917104544690

7.10 编程式路由导航(不用<router-link>)

作用:不借助<router-link>实现路由跳转,让路由跳转更加灵活

this.$router.push({})内传的对象与<router-link>中的to相同

this.$router.replace({})

this.$router.forward() 前进

this.$router.back() 后退

this.$router.go(n) 可前进也可后退,n为正数前进n,为负数后退

this.$router.push({name:'detail',params:{id:xxx,title:xxx}
})this.$router.replace({name:'detail',params:{id:xxx,title:xxx}
})

实例:

src/components/Banner.vue

<template><div class="row"><div class="col-xs-offset-2 col-xs-8"><div class="page-header"><h2>Vue Router Demo</h2><button @click="back">后退</button><button @click="forward">前进</button></div></div></div>
</template><script>
export default {name: "Banner",methods: {back() {this.$router.back()},forward() {this.$router.forward()}}
}
</script>

src/pages/Message.vue

<template><div><ul><li v-for="m in messageList" :key="m.id"><!--跳转并携带params参数,to的对象写法(推荐)--><router-link:to="{name: 'detail', // 使用name进行跳转params: {id: m.id,title: m.title,}}">{{ m.title }}</router-link><button @click="pushShow(m)">push查看</button><button @click="replaceShow(m)">replace查看</button></li></ul><hr/><router-view></router-view></div>
</template><script>
export default {name: "Message",data() {return {messageList: [{id: '001', title: '消息001'},{id: '002', title: '消息002'},{id: '003', title: '消息003'},]}},methods: {pushShow(m) {this.$router.push({name: 'detail', // 使用name进行跳转params: {id: m.id,title: m.title,}})},replaceShow(m) {this.$router.replace({name: 'detail', // 使用name进行跳转params: {id: m.id,title: m.title,}})}}
}
</script>

image-20220917110245115

7.11 缓存路由组件

作用:让不展示的路由组件保持挂载,不被销毁

<keep-alive include="News"><router-view></router-view></keep-alive>

<keep-alive :include="['News', 'Message']"><router-view></router-view></keep-alive>

// 缓存一个路由组件
<keep-alive include="News"> // include中写想要缓存的组件名,不写表示全部缓存<router-view></router-view>
</keep-alive>// 缓存多个路由组件
<keep-alive :include="['News','Message']"> <router-view></router-view>
</keep-alive>

实例:

src/pages/News.vue

<template><ul><li>news001 <input type="text"></li><li>news002 <input type="text"></li><li>news003 <input type="text"></li></ul>
</template><script>
export default {name: "News"
}
</script>

src/pages/Home.vue

<template><div><h2>我是Home的内容</h2><ul class="nav nav-tabs"><li><router-link class="list-group-item" active-class="active" to="/home/news">News</router-link></li><li><router-link class="list-group-item" active-class="active" to="/home/message">Message</router-link></li></ul><keep-alive include="News"><router-view></router-view></keep-alive></div>
</template><script>
export default {name: "Home",
}
</script>

image-20220917111025426

7.12 activated deactivated

activateddeactivated是路由组件所独有的两个钩子,用于捕获路由组件的激活状态

具体使用

  1. activated路由组件被激活时触发
  2. deactivated路由组件失活时触发

src/pages/News.vue

<template><ul><li :style="{opacity}">欢迎学习Vue</li><li>news001 <input type="text"></li><li>news002 <input type="text"></li><li>news003 <input type="text"></li></ul>
</template><script>
export default {name: "News",data() {return {opacity: 1,}},activated() {console.log("News组件被激活了")this.timer = setInterval(() => {this.opacity -= 0.01;if (this.opacity <= 0) this.opacity = 1}, 16);},deactivated() {console.log("News组件失活了")clearInterval(this.timer)}
}
</script>

image-20220917112832536

7.13 路由守卫

作用:对路由进行权限控制

分类:全局守卫、独享守卫、组件内守卫

7.13.1 全局路由守卫

meta 路由元信息

// 全局前置守卫:初始化时、每次路由切换前执行
router.beforeEach((to,from,next) => {console.log('beforeEach',to,from)if(to.meta.isAuth){ // 判断当前路由是否需要进行权限控制if(localStorage.getItem('school') === 'teng'){ // 权限控制的具体规则next()	// 放行}else{alert('暂无权限查看')}}else{next()	// 放行}
})// 全局后置守卫:初始化时、每次路由切换后执行
router.afterEach((to,from) => {console.log('afterEach',to,from)if(to.meta.title){ document.title = to.meta.title //修改网页的title}else{document.title = 'vue_test'}
})

实例:

src/router/index.js

// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router';
// 引入组件
import Home from "@/pages/Home";
import About from "@/pages/About";
import News from "@/pages/News";
import Message from "@/pages/Message";
import Detail from "@/pages/Detail";// 创建并暴露一个路由器
const router = new VueRouter({routes: [{name: 'zhuye',path: '/home',meta: {title: '主页'},component: Home,children: [{name: 'xinwen',path: "news",component: News,meta: {isAuth: true, title: '新闻'}},{name: 'xiaoxi',path: "message",component: Message,meta: {isAuth: true, title: '消息'},children: [{name: "detail", // name配置项为路由命名path: "detail/:id/:title",  // 使用占位符声明接受params参数component: Detail,meta: {isAuth: true, title: '详情'},// props的第三种写法,值为函数props($route) { // 这里可以使用解析赋值return {id: $route.params.id,title: $route.params.title,}}}]}]},{name: 'guanyu',path: '/about',component: About,meta: {title: '关于'}}]
})// 全局前置路由守卫---初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {console.log("前置路由守卫", to, from)// 判断是否需要鉴权if (to.meta.isAuth) {if (localStorage.getItem('school') === 'teng') {next()} else {alert("学校名不对,无权限查看")}} else {next()}
})// 全局后置路由守卫---初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to, from) => {console.log("后置路由守卫", to, from)document.title = to.meta.title || 'Vue学习'
})export default router;

7.13.2 独享路由守卫

beforeEnter(to,from,next){console.log('beforeEnter',to,from)if(localStorage.getItem('school') === 'atguigu'){next()}else{alert('暂无权限查看')}
}

实例:

src/router/index.js

// 该文件专门用于创建整个应用的路由器
import VueRouter from 'vue-router';
// 引入组件
import Home from "@/pages/Home";
import About from "@/pages/About";
import News from "@/pages/News";
import Message from "@/pages/Message";
import Detail from "@/pages/Detail";// 创建并暴露一个路由器
const router = new VueRouter({routes: [{name: 'zhuye',path: '/home',meta: {title: '主页'},component: Home,children: [{name: 'xinwen',path: "news",component: News,meta: {isAuth: true, title: '新闻'},// 独享守卫,特点路由切换之后被调用beforeEnter: (to, from, next) => {if (to.meta.isAuth) {if (localStorage.getItem('school') === 'teng') {next()} else {alert("学校名不对,无权限查看")}} else {next()}}},{name: 'xiaoxi',path: "message",component: Message,meta: {isAuth: true, title: '消息'},children: [{name: "detail", // name配置项为路由命名path: "detail/:id/:title",  // 使用占位符声明接受params参数component: Detail,meta: {isAuth: true, title: '详情'},// props的第三种写法,值为函数props($route) { // 这里可以使用解析赋值return {id: $route.params.id,title: $route.params.title,}}}]}]},{name: 'guanyu',path: '/about',component: About,meta: {title: '关于'}}]
})// 全局后置路由守卫---初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to, from) => {console.log("后置路由守卫", to, from)document.title = to.meta.title || 'Vue学习'
})export default router;

7.13.3 组件内路由守卫

//进入守卫:通过路由规则,进入该组件时被调用
beforeRouteEnter (to, from, next) {... next()},//离开守卫:通过路由规则,离开该组件时被调用
beforeRouteLeave (to, from, next) {... next()},

实例:

src/pages/About.vue

<template><h2>我是About的内容</h2>
</template><script>
export default {name: "About",// 通过路由规则,进入该组件时被调用beforeRouterEnter(to, from, next) {console.log('About-beforeRouterEnter')if (to.meta.isAuth) {if (localStorage.getItem('school') === 'teng') {next()} else {alert("学校名不对,无权限查看")}} else {next()}},// 通过路由规则,离开该组件时被调用beforeRouteLeave(to, from, next) {console.log('About-beforeRouteLeave')next()}
}
</script>

7.14 路由器的两种工作模式

  1. 对于一个url来说,什么是hash值?

    #及其后面的内容就是hash

  2. hash值不会包含在HTTP请求中,即:hash值不会带给服务器

  3. hash模式

    1. 地址中永远带着#号,不美观
    2. 若以后将地址通过第三方手机app分享,若app校验严格,则地址会被标记为不合法
    3. 兼容性较好
  4. history模式

    1. 地址干净,美观
    2. 兼容性和hash模式相比略差
    3. 应用部署上线时需要后端人员支持,解决刷新页面服务端404的问题
const router =  new VueRouter({mode:'history',routes:[...]
})export default router

8. Vue UI组件库

8.1 常用的UI组件库

8.1.1 移动端常用UI组件库

  • Vant

  • Cube UI

  • Mint UI

  • https://nutui.jd.com/#/

8.1.2 PC端常用UI组件库

  • Element UI

  • IView UI

8.2 element-ui基本使用

  1. 安装element-uinpm i element-ui -S
  2. src/main.js
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';							// 引入ElementUI组件库
import 'element-ui/lib/theme-chalk/index.css';	// 引入ElementUI全部样式Vue.config.productionTip = falseVue.use(ElementUI)	// 使用ElementUInew Vue({el:"#app",render: h => h(App),
})
  1. src/App.vue
<template><div><br><el-row><el-button icon="el-icon-search" circle></el-button><el-button type="primary" icon="el-icon-edit" circle></el-button><el-button type="success" icon="el-icon-check" circle></el-button><el-button type="info" icon="el-icon-message" circle></el-button><el-button type="warning" icon="el-icon-star-off" circle></el-button><el-button type="danger" icon="el-icon-delete" circle></el-button></el-row></div>
</template><script>export default {name:'App',}
</script>

image-20220917123505326

8.3 element-ui按需引入

  1. 安装babel-plugin-component npm i babel-plugin-component -D
  2. 修改babel-config-js
module.exports = {presets: ['@vue/cli-plugin-babel/preset',["@babel/preset-env", { "modules": false }]],plugins: [["component",{        "libraryName": "element-ui","styleLibraryName": "theme-chalk"}]]
}
  1. src/main.js
import Vue from 'vue'
import App from './App.vue'
import { Button,Row } from 'element-ui'	// 按需引入Vue.config.productionTip = falseVue.component(Button.name, Button);
Vue.component(Row.name, Row);
/* 或写为* Vue.use(Button)* Vue.use(Row)*/new Vue({el:"#app",render: h => h(App),
})

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

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java内存模型:创建对象在堆区如何分配内存

一、Heap堆区 Heap堆是JVM所管理的内存中最大的一块区域&#xff0c;被所有线程共享的一块内存区域。堆区中存放对象实例和数组&#xff0c;“几乎”所有的对象实例以及数组都在这里分配内存。 新生代、老年代 二、创建对象的内存分配 初始创建对象会在新生代的Eden区生成&…

行为型设计模式之策略模式

行为型设计模式之策略模式策略模式应用场景优缺点主要角色策略模式的基本使用创建抽象策略角色创建具体策略角色创建上下文角色客户端执行策略模式实现支付方式的选择创建抽象策略角色创建具体策略角色创建上下文角色客户端执行策略模式 策略模式&#xff08;Strategy Pattern)…

线程安全集合:CopyOnWriteArrayList源码分析

目录 一、基本思想 二、源码分析 add()方法 set()方法 remove()方法 get()方法 三、小结 一、基本思想 首先CopyOnWrite 简称 COW &#xff0c;是一种用于对集合并发访问的优化策略。基本思想是&#xff1a;当我们往一个集合容器中写入元素时&#xff08;比如添加…

C++左值右值、左值引用右值引用、移动语义move

目录 1.什么是左值、右值 2.什么是左值引用&、右值引用&& 2.1左值引用& 2.2右值引用&& 2.3对左右值引用本质的讨论 2.3.1右值引用有办法指向左值吗&#xff1f; 2.3.2左值引用、右值引用本身是左值还是右值&#xff1f; 2.4 右值引用使用场景…

51单片机学习:静态数码管实验

实验名称&#xff1a;静态数码管实验 接线说明&#xff1a; 实验现象&#xff1a;下载程序后“数码管模块”最左边数码管显示数字0 注意事项&#xff1a; ***************************…

神经体液调节网络,神经网络能干嘛

神经网络的发展趋势如何&#xff1f; 神经网络的云集成模式还不是很成熟&#xff0c;应该有发展潜力&#xff0c;但神经网络有自己的硬伤&#xff0c;不知道能够达到怎样的效果&#xff0c;所以决策支持系统中并不是很热门&#xff0c;但是神经网络无视过程的优点也是无可替代…

CSDN编程竞赛-第六期(上)

CSDN编程竞赛报名地址&#xff1a;https://edu.csdn.net/contest/detail/16 努力是为了让自己不平庸&#xff1a; 前言/背景 四道题都是相关数组的&#xff0c;思路很好想&#xff0c;但是需要熟练使用&#xff0c;不能有小错误。 参赛流程 活动时间&#xff1a;9月8日-21日&a…

Python机器视觉--OpenCV进阶(核心)--图像直方图与掩膜直方图与直方图均衡化

1.图像直方图 1.1 图像直方图的基本概念 在统计学中&#xff0c;直方图是一种对数据分布情况的图形表示&#xff0c;是一种二维统计图表. 图像直方图是用一表示数字图像中亮度分布的直方图&#xff0c;标绘了图像中每个亮度值的像素数。可以借助观察该直方图了解需要如何调整…

记录一次关于Rank()排序函数问题

先来看应用场景吧 就是页面上有个top按钮 根据不同的top 进行筛选 比如我选择top5 那么在下方当前大区的销售额降序筛选出来最高的前五个销售员or客户这种场景 &#x1f496; 问题 问题1&#xff1a;为什么我的这个rank排序函数 这个华南大区 不是从1开始的呢 其他大区都是正…

java毕业设计选题系统ssm实现的商城系统(电商购物项目)

&#x1f345;文末获取联系&#x1f345; 一、项目介绍 《ssm实现的商城系统》该项目采用技术&#xff1a;springspringMVCmybaitsEasyUIjQueryAjax等相关技术&#xff0c;项目含有源码、文档、配套开发软件、软件安装教程、项目发布教程等 1.1 课题背景、目的及意义 当今社…

java 同学聚会AA制共享账单系统springboot 小程序022

本系统在一般同学会小程序的基础上增加了首页推送最新信息的功能方便用户快速浏览&#xff0c;是一个高效的、动态的、交互友好的同学会小程序。 用户在首页上会看到各类模块的推送内容&#xff0c;可以以最直接的方式获取信息&#xff0c;注册登陆后&#xff0c;可以对应经费信…

Unity基础笔记(5)—— Unity渲染基础与动画系统

Unity渲染基础与动画系统 Unity渲染基础 一、摄像机 1. 摄像机概念和现实中的摄像机很接近,Unity 中 Camera 组件负责将游戏画面拍摄然后投放到画面上 Camera 拍摄到的画面决定了 Game 面板的画面 创建场景的时候,Unity 会默认创建一个摄像机,所以我们点击 Game 面板才有画面…

【算法刷题】链表篇-链表的回文结构

文章目录题目要求方法1&#xff1a;思路代码方法2代码题目要求 链接&#xff1a;链表的回文结构_牛客题霸_牛客网 (nowcoder.com) 1 -> 2 -> 3 -> 2 -> 1 1 -> 2 -> 2 -> 1 上面两个是回文结构 方法1&#xff1a;思路 1.遍历链表&#xff0c;把结点对应的…

网络安全基础——对称加密算法和非对称加密算法(+CA数字证书)

目录 一、数据传输时的安全特性 二、对称加密算法&#xff1a; 三、非对称加密算法 四、对称加密和非对称加密 — 融合算法&#xff1a; 五、CA数字证书&#xff1a; 一、数据传输时的安全特性 ———————————————————————————————————…

分布式进化算法

1 多解优化问题 多解优化问题是指一类具有多个最优解的复杂优化问题。多峰优化问题和多目标优化问题都是两类典型的多解优化问题&#xff0c;它们之前的统一关系&#xff0c;即都具有多个最优解。多峰优化问题要求算法找到多个具有相同适应度值得最优解&#xff0c;多目标优化问…

SpringBoot的核心原理(扒笔记记录)

这一课的主要重点&#xff1a; 自动装配以及starterJDBC数据库连接池ORM、JPA、MyBatis、Hibernate这样相关的一些技术 从Spring到SpringBoot 我们在工作中都可能用过了SpringBoot&#xff0c;特别是最近几点&#xff0c;Java开发者大军里的一员&#xff0c;我们一般可能上手就…

卷积神经网络相比循环神经网络具有哪些特征

CNN卷积神经网络结构有哪些特点&#xff1f; 局部连接&#xff0c;权值共享&#xff0c;池化操作&#xff0c;多层次结构。 1、局部连接使网络可以提取数据的局部特征&#xff1b;2、权值共享大大降低了网络的训练难度&#xff0c;一个Filter只提取一个特征&#xff0c;在整个…

Docker容器互联

前言&#xff1a; 虽然每个docker容器之间都能通过ip来进行互联&#xff0c;但当容器重新启动&#xff0c;ip就会被重新分配给重新启动的容器&#xff0c;这时同个容器由于重启导致ip不一样了&#xff0c;这时就会导致开发和运维的困难程度大大增加&#xff0c;这时候就要考虑…

springboot+学生信息管理 毕业设计-附源码191219

学生信息管理的设计与实现 摘 要 科技进步的飞速发展引起人们日常生活的巨大变化&#xff0c;电子信息技术的飞速发展使得电子信息技术的各个领域的应用水平得到普及和应用。信息时代的到来已成为不可阻挡的时尚潮流&#xff0c;人类发展的历史正进入一个新时代。在现实运用中&…

Maven下的依赖管理

依赖管理一. 使用坐标引入jar包二. 快捷方式导入jar包的坐标三. 自动导入设置四. 依赖范围一. 使用坐标引入jar包 使用坐标引入jar包的步骤&#xff1a; 在项目的 pom.xml 中编写 标签在 标签中 使用 引入坐标定义坐标的 groupId&#xff0c;artifactId&#xff0c;version 点…