Comments (4)
Vue.component('currency-input', {
template: '\
<span>\
$\
<input\
ref="input"\
v-bind:value="value"\
v-on:input="updateValue($event.target.value)"\
>\
</span>\
',
props: ['value'],
methods: {
// 不是直接更新值,而是使用此方法来对输入值进行格式化和位数限制
updateValue: function (value) {
var formattedValue = value
// 删除两侧的空格符
.trim()
// 保留 2 小数位
.slice(
0,
value.indexOf('.') === -1
? value.length
: value.indexOf('.') + 3
)
// 如果值不统一,手动覆盖以保持一致
if (formattedValue !== value) {
this.$refs.input.value = formattedValue
}
// 通过 input 事件发出数值
this.$emit('input', Number(formattedValue))
}
}
})
官网例子中有这个例子
不是很明白这个if里面的事情,因为我理解后面$emit更新后,input的value会跟着变,是不是多此一举了?
// 如果值不统一,手动覆盖以保持一致
if (formattedValue !== value) {
this.$refs.input.value = formattedValue
}
// 通过 input 事件发出数值
this.$emit('input', Number(formattedValue))
from cc.
大哥,这个难了点,
据说v-model是语法糖,,
from cc.
自定义 checkbox 按钮中使用 v-model(我也写了一个新的)
// App.vue
<template>
<div class="wrap">
<v-input id="s1" v-model="selected"/>
<v-input id="s2" v-model="selected"/>
<v-input id="s3" v-model="selected"/>
<v-input id="s4" v-model="selected"/>
{{selected}}
</div>
</template>
<script>
import Input from './Input.vue';
export default {
components: {Input},
data:()=>({
selected: []
})
}
</script>
// Input.vue 组件文件
<template id='input'>
<label :for="id">
<input type="checkbox" :checked="checkVal()" :id='id' @input="updateValue">
<slot />
</label>
</template>
<script>
export default {
name:'v-input',
model: {
prop: 'selected',
event: 'input'
},
props: {
id: {
type: String,
required: true
// default: '需要一个独一无二的id'+ ~~(Math.random()*1000) + 999
},
selected: {},
},
methods: {
checkVal(){
return this.selected.includes(this.id)
},
updateValue(e) {
let newVal = [...this.selected]
if (e.target.checked) {
newVal.push(this.id)
} else {
newVal.splice(newVal.indexOf(this.id), 1)
}
this.$emit('input', newVal)
}
}
};
</script>
from cc.
Vue.component('currency-input', { template: '\ <span>\ $\ <input\ ref="input"\ v-bind:value="value"\ v-on:input="updateValue($event.target.value)"\ >\ </span>\ ', props: ['value'], methods: { // 不是直接更新值,而是使用此方法来对输入值进行格式化和位数限制 updateValue: function (value) { var formattedValue = value // 删除两侧的空格符 .trim() // 保留 2 小数位 .slice( 0, value.indexOf('.') === -1 ? value.length : value.indexOf('.') + 3 ) // 如果值不统一,手动覆盖以保持一致 if (formattedValue !== value) { this.$refs.input.value = formattedValue } // 通过 input 事件发出数值 this.$emit('input', Number(formattedValue)) } } })官网例子中有这个例子
不是很明白这个if里面的事情,因为我理解后面$emit更新后,input的value会跟着变,是不是多此一举了?
// 如果值不统一,手动覆盖以保持一致 if (formattedValue !== value) { this.$refs.input.value = formattedValue } // 通过 input 事件发出数值 this.$emit('input', Number(formattedValue))
并没有多次一举啊。 将if那段代码注释掉,往input中输入内容是没有限制的,并不是只到了小数点后两位就停止了。同时input标签的value值也不全等于formattedValue,而是等于输入框中输入的值。这点我也好奇为啥会这样...
from cc.
Related Issues (20)
- 48.帮学弟做题--WebSocket和Vue的五子棋 HOT 29
- 50.Material Design 风格的 Vue.js UI 组件库 beta 版本发布
- 49.理解 Vue.js 的计算属性 HOT 1
- 51.Vue.js 组件库 Rubik UI 开发心得总结 HOT 6
- 52.2016-这一年的总结和随想 HOT 3
- 53.图解 Flexbox HOT 6
- 54.Vue.js 项目使用 Karma 做自动化UI测试小结 HOT 9
- 55.[函数式编程] 不用循环的 JavaScript HOT 6
- 56.图解 Flexbox 2 - 深入理解 HOT 10
- flex HOT 1
- 57.在 Vue 中使用全局共享的方式管理状态
- 58.ES2015 Proxy 实用代码示例&详解
- 59.适用于 vue.js 和原生 js 的渐进式图片加载 HOT 25
- 60.[半夜改bug] mongoose 的 createConnection 和 connect HOT 2
- 61.async/await 小技巧 HOT 4
- 62.Vue.js Little Tips HOT 6
- 63.CSS 伪类选择器
- 64.深度使用 JSON.stringify() HOT 14
- ccc HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cc.