Vue核心技术之组件封装和路由
Python 列表推导式实战示例列表推导式是Python中一种简洁高效的创建列表的方式可以替代传统的for循环使代码更加简洁易读。1. 基本用法# 传统for循环方式 squares [] for i in range(10): squares.append(i * i) 列表推导式方式更简洁 squares [i * i for i in range(10)] print(squares) # 输出: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]2. 带条件的列表推导式# 只生成偶数的平方 even_squares [i * i for i in range(10) if i % 2 0] print(even_squares) # 输出: [0, 4, 16, 36, 64] 多个条件 filtered_numbers [x for x in range(20) if x 5 and x % 3 0] print(filtered_numbers) # 输出: [6, 9, 12, 15, 18] 条件表达式类似三元运算符 numbers [1, 2, 3, 4, 5] result [偶数 if x % 2 0 else 奇数 for x in numbers] print(result) # 输出: [奇数, 偶数, 奇数, 偶数, 奇数]3. 与普通循环对比示例# 场景从字符串列表中提取长度大于3的字符串并转为大写 words [apple, cat, banana, dog, elephant, fox] 传统for循环方式 result1 [] for word in words: if len(word) 3: result1.append(word.upper()) 列表推导式方式 result2 [word.upper() for word in words if len(word) 3] print(传统循环结果:, result1) # 输出: [APPLE, BANANA, ELEPHANT] print(列表推导式结果:, result2) # 输出: [APPLE, BANANA, ELEPHANT] 性能对比列表推导式通常更快 import time 测试传统循环 start1 time.time() for _ in range(100000): result [] for word in words: if len(word) 3: result.append(word.upper()) end1 time.time() 测试列表推导式 start2 time.time() for _ in range(100000): result [word.upper() for word in words if len(word) 3] end2 time.time() print(f传统循环耗时: {end1 - start1:.4f}秒) print(f列表推导式耗时: {end2 - start2:.4f}秒) print(f列表推导式比传统循环快: {(end1 - start1) - (end2 - start2):.4f}秒)4. 嵌套列表推导式# 创建二维矩阵 matrix [[i * j for j in range(1, 4)] for i in range(1, 4)] print(matrix) # 输出: [[1, 2, 3], [2, 4, 6], [3, 6, 9]] 展平二维列表 matrix [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flattened [num for row in matrix for num in row] print(flattened) # 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9]总结列表推导式让代码更简洁、更Pythonic但在复杂逻辑时仍需考虑可读性。对于简单的转换和过滤操作列表推导式是首选。1.my-tag标签组件封装作用双击显示输入框失去焦点后隐藏输入框修改内容并回车保存template div classmy-tap input v-ifisEdit classinput typetext placeholder输入标签 / div v-else classtext dblclickhandleClick新兰/div /div /template script export default { data() { return { isEdit: false, } }, methods: { handleClick() { //双击后切换到显示状态 this.isEdit true //等dom更新完获取焦点 this.$nextTick(() { this.$refs.inp.focus() }) } } } /script2.my-table 表格组件封装1.数据不能写死动态传递表格渲染的数据2.结构不能写死多处结构自定义1表头支持自定义2主体支持自定义script import MyTable from ./App.vue export default { components: { MyTable }, data() { return { tempText: cr, tempText1: 温简言, goods: [ { id: 101, picture: https://c-ssl.dtstatic.com/uploads/blog/202407/30/jJS5ZpX0Se9G2v6.thumb.1000_0.jpeg }, { id: 101, picture: https://c-ssl.dtstatic.com/uploads/blog/202407/30/jJS5ZpX0Se9G2v6.thumb.1000_0.jpeg }, {id:101, picture:https://c-ssl.dtstatic.com/uploads/blog/202407/30/jJS5ZpX0Se9G2v6.thumb.1000_0.jpeg} ] } }, } /script3.单页面应用单页面开发效率高性能好的原因是页面按需更新要按需更新首先要明确访问路径和组件的对应关系 ----路由4.插件VueRouter2个核心步骤1创建需要的组件配置路由规则2配置导航配置路由出口div classfooter_wrap a href#/find发现/a a href#/my我的/a a href#/friend朋友/a /div div classtop router-view/router-view /div组件分类1.页面组件和复用组件都是.vue类型文档2.页面组件-views文件夹配合路由页面展示复用组件-components.文件夹 封装复用5.路由的封装抽离import App from ./App.vue import MyTable from ./MyTable.vue Vue.use(vueRouter) //插件初始化 const router new vueRouter({ //route 一条路由器规则 { path:路径 component:组件 } routes: [ { path: ./App.vue, component: App }, { path:/MyTable,component:MyTable }, ] })6.声明式导航1.vue-router提供一个全局组件router-link(取代a标签)1能跳转配置to 属性指定路径。2能高亮默认就会提供高亮类名可直接设置高亮样式template div classtable-case MyTable :datagoods/MyTable div classfooter_wrap router-link to/find发现/router-link router-link to/my我的/router-link router-link to/part朋友/router-link /div div classtop router-view/router-view /div /div /template2.两个类名① router-link-active模糊匹配用的多to/my可以匹配/my/my/a/my/6② router-link-exact-active精确匹配to/my仅可以匹配/my3.跳转传参①查询参数传参 to/path?参数名值”route.query.参数名②动态路由传参//创建一个路由对象 const router new VueRouter({ router: [ { path: /searth/:words, component: Search } ] })7.Vue路由重定向匹配path后强制跳转path路径import NotFind from /views/NotFind const router new VueRouter({ routes:[ { path:/redirect:/home}, { path:/home,component:Home }path:/search/words?,component:Search}, { path:*,component:NotFind } ] })