功能描述:
通过点击按钮,可以增减购物数量
组件名称是 CouterBtn
最终效果如下
我们使用 vue-cli搭建基本的开发环境,这也是最快的进行 .vue组件开发的方式
对于入口组件 App.vue (可以暂时忽略其他细节,我们的重点是如何写组件)
App.vue
<template> <div id="app"> <h4>这是一个利用 v-model实现的自定义的表单组件</h4> <h6>CouterBtn组件的值 <i> {{ btnValue }} </i></h6> 5. <counter-btn v-model="btnValue"></counter-btn> <form class="" action="/add" method="post"> <!-- 真实情况请将 type改为hidden --> <label for="count">值绑定到 input 隐藏域里</label> <input type="text" name="count" :value="btnValue.res" id="count"> 10. </form> </div> </template> <script> import CounterBtn from './components/CouterBtn.vue' 15. export default { data() { return { btnValue: {} } 20. }, components: { CounterBtn } } 25. </script> <style lang="stylus"> h6 i border 1px dotted form 30. margin-top 20px padding 20px border 1px dotted #ccc label vertical-align: middle 35. </style>
下面我来对 App.vue中的一些代码进行一些说明,根据代码行数来说明
4 : 我们使用 {{ btnValue }}来获取自定义组件 counter-btn的值, 这里仅仅是展示效果用
5: 我们使用了counter-btn ,自定义的组件
9: 我们将自定义组件的值,绑定到我们的表单组件 input中去, 在真实的情况下, 此 input的类型可能为 hidden类型
21: 由于我们需要在App.vue组件中使用我们自定义的组件 counter-btn,所以需要使用 components注册组件, 这里还使用了 ES6的对象解构
26: 我们使用CSS预处理器为stylus, 前提是你的 node_modules中要安装此npm包和loader,vue-cli已经帮我们处理好了stylus的编译; 根据个人情况选择
我们自己设计的组件通过 v-model,把组件内部的值传给了它所绑定的值
下面我们来看看我们的组件的实现
CounterBtn.vue
<template> <div class="coutter-wrapper"> <button type="button" @click="plus">+</button> <button type="button">{{ result }}</button> 5. <button type="button" @click="minus">-</button> </div> </template> <script> export default { 10. methods: { minus() { this.result--; this.$emit('input', {res: this.result, other: '--'}) }, 15. plus() { this.result++; this.$emit('input', {res: this.result, other: '++'}) } }, 20. data() { return { result: 0, } } 25. } </script> <style lang="stylus" scoped> button border 0 30. outline 0 border-radius 3px button:nth-child(2) width 200px </style>
我们可以看到组件的实现非常简单, 3个button搞定; 这里最关键的代码是
this.$emit('input', {res: this.result, other: '++'})
通过 触发 input事件和自定的数据来实现把数据暴露给 v-model绑定的属性值
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
- 丁当《日与夜,跟自己说晚安》[320K/MP3][98.51MB]
- 何炅.2005-漫游【中唱】【WAV+CUE】
- 何嘉莉.1998-MYDREAMS(EP)【飞图】【WAV+CUE】
- 群星.2019-天坑鷹猎电视剧原声带(EP)【智慧大狗】【FLAC分轨】
- 【雨果唱片】《春江花月夜》2020【WAV+CUE】
- 【正原唱片】女子十二乐坊《发烧国乐精粹-十二乐坊》WAV+CUE
- [黄翠珊]SusanWong-TheBestofSusanWong[WAV+CUE]
- 杜德伟.1995-BEST.LOVE(英)【滚石】【WAV+CUE】
- 杜德伟.1996-BEST.LOVE.2(英)【滚石】【WAV+CUE】
- 陈柏宇.2008-Change【SONY】【FLAC分轨】
- 丁当《日与夜,跟自己说晚安》[FLAC/分轨][285.42MB]
- 早安《Bubble Vision》[320KMP3][36.83MB]
- 叶振棠《叶振棠歌曲合集》[合集][MP3][145.9MB]
- [黄翠珊]SusanWong-ClosetoMe[SACD]dsf
- 中唱唱片群星《好歌珍藏-民歌精粹》2CD[WAV.]