C
C
Carrot1342020-08-13 23:57:15
Vue.js
Carrot134, 2020-08-13 23:57:15

Change value in input on click?

Store:

export default {
    actions:{
        addNumber(ctx){
            const number=[32,35]
            ctx.commit('allNumber',number)
        }
    },
    mutations:{
        allNumber(state,number){
            state.numbers = number
        }
    },
    state:{
        numbers:[],
        change:[]

    },
    getters:{
        number(state){
            return state.numbers
        },
     
    }
}


App:
<div>
    <input type="text" class="first" value="num" >
    <input type="text" class="second">=
    <input type="text" class="third"><br>
      <div  v-for="num in number">
        <button>{{num}}</button>
     </div>
</div>

import {mapGetters,mapActions} from 'vuex'

export default {
  name: 'App',
  computed:mapGetters(['number']),

  methods: {
    ...mapActions(["addNumber"]),
  },
  async mounted() {
    this.addNumber()
  },
}

How can I make it so that when the button with the value 32 is pressed, its "value" value in the input field also changes to 32, and then when the button "35" is clicked, the value becomes "3235" (like a calculator)?
Addition: So that, for example, all values ​​​​are entered into the change array when the button is clicked through action and from there they are thrown into the value field of the input

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-08-14
@Carrot134

mutations: {
  add: ({ change }, val) => change.push(val),
},

<input :value="$store.state.change.join('')">
<div v-for="n in $store.state.numbers">
  <button @click="$store.commit('add', n)">{{ n }}</button>
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question