Vue动手实践p110和p107小试牛刀

2023-11-06 11:04

本文主要是介绍Vue动手实践p110和p107小试牛刀,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、小试牛刀

真的很不好意思诸位,最近事情有点多,更新进度缓慢了,这次就简单的再复习一下vue组件的内容,大家可以自行研究一下,我就不深入解析了。

<body>
<div id="app"><button @click="Cmop">切换组件</button><p></p><component :is="current" :name="name[current]" :color="color[current]" @change="change"><template slot="content">{{name[current]}}</template></component>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.7.0/vue.js"></script>
<script>Vue.component('my-component-one',{template:`<div><div style="line-height: 2.6;" :style="{background: color}"><slot name="content"></slot><button @click="$emit('change',name)">回传事件</button></div></div>`,props:{name:String,color:String}});Vue.component('my-component-two',{template:`<div><div style="line-height: 2.4;" :style="{background: color}"><slot name="content"></slot><button @click="$emit('change',name)">回传事件</button></div></div>`,props:{name:String,color:String}});new Vue({el:'#app',data: {current: 'my-component-one',name: {'my-component-one': '我是组件一','my-component-two': '我是组件二'},color: {'my-component-one': 'yellow','my-component-two': 'red'},},methods: {change(value) {alert(value)},Cmop() {if (this.current === 'my-component-one') {this.current = 'my-component-two'} else {this.current = 'my-component-one'}}}})
</script>
</body>

效果


在这里插入图片描述

二、动手实践

<body>
<div id="app"><!-- 组件使用者只需传递users数据即可 --><my-stripe-list :items="users" odd-bgcolor="#D3DCE6" even-bgcolor="#E5E9F2" @change="change"><!-- props对象接收来自子组件slot的$index参数 --><template slot="cont" slot-scope="props"><span>{{users[props.$index].id}}</span><span>{{users[props.$index].name}}</span><span>{{users[props.$index].age}}</span><!-- 这里可以自定[编辑][删除]按钮的链接和样式 --><a :href="'#edit/id='+users[props.$index].id">编辑</a><a :href="'#del/id='+users[props.$index].id">删除</a></template></my-stripe-list>
</div>
<script>Vue.component('my-stripe-list', {/*slot的$index可以传递到父组件中*/template: `<div><div v-for="(item, index) in items" style="line-height:2.2;" :style="index % 2 === 0 ? 'background:'+oddBgcolor : 'background:'+evenBgcolor"><slot name="cont" :$index="index"></slot><button @click="$emit('change', item)">弹出名字和年龄</button></div></div>`,props: {items: Array,oddBgcolor: String,evenBgcolor: String}});new Vue({el: '#app',data: {users: [{id: 1, name: '张三', age: 20},{id: 2, name: '李四', age: 22},{id: 3, name: '王五', age: 27},{id: 4, name: '张龙', age: 27},{id: 5, name: '赵虎', age: 27}]},methods: {change(value) {alert(`姓名:${value.name}, 年龄:${value.age}`)}}});
</script>
</body>

效果

在这里插入图片描述

这篇关于Vue动手实践p110和p107小试牛刀的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/356237

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe