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

编程入门 行业动态 更新时间:2024-10-28 14:30:19

Vue动手实践p110和p107小试<a href=https://www.elefans.com/category/jswz/34/1764263.html style=牛刀"/>

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=".7.0/vue.js"></script>
<script>Vueponent('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}});Vueponent('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>Vueponent('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小试牛刀

本文发布于:2023-11-16 10:15:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:牛刀   Vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!