如下代码
想实现一个计数器, 计数器是没问题了,就是想使用场景里一般个数都会传回来, 我这个个数如何传入到组件里, 传入到组件里如何使用 v-model
或者 :value
绑定; 求帮忙~~~
<template>
<div class="ai-counter"
:class="{ disdecrement: decrementDisable, disincrement: incrementDisable}"
>
<button
class="btn btn-counter btn-decrement"
@click="decrement"
>-</button>
<input type="text" class="form-control" v-model="currentValue">
<button
class="btn btn-counter btn-increment"
@click="increment"
>+</button>
</div>
</template>
<script>
export default {
props:{
min: {
type: Number,
default: 1
},
max: {
type: Number,
default: 999
},
currentValue: {
type: Number,
default: 1
}
},
methods: {
increment(){
this.currentValue ++
if( this.currentValue >= this.max ){
this.incrementDisable = true
this.currentValue = this.max
}else{
this.incrementDisable = false
this.decrementDisable = false
}
},
decrement() {
this.currentValue --
if( this.currentValue <= this.min ){
this.decrementDisable = true
this.currentValue = this.min
}else{
this.decrementDisable = false
this.incrementDisable = false
}
}
},
data(){
return{
decrementDisable: true,
incrementDisable: false
}
},
computed(){
}
}
</script>
朋友你应该是父组件上v-model ,在子组件里要$emit提交到父组件 this.$emit('input', this.val)
就是说你少一个子组件提交到父组件
<my-counter v-model="num"></my-counter>
Vue.component("my-counter", {