Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
[Vue warn]: Invalid prop: type check failed for prop "value". Expected Array, got Number with value 1

I have an input with the type number, I want to make it so that in the input they cannot print a number greater than ten, everything worked fine for me until I replaced the value with an array (before value: 1 after value: [1, 1])

After I changed to an array, I try to manually get the first number of the array as the value for my input, but I get an error and do not understand how to solve it

App.vue

  <div>
    <customInput v-model="value[0]" :max-value="10" />
  </div>


<script>
import customInput from "./components/HelloWorld";

export default {
  name: "App",
  data() {
    return {
      value: [1, 1],
    };
  },
  components: {
    customInput,
  },
};
</script>

HelloWorld.vue

  <div>
    <input :value="value[0]" type="number" @input="onInput" max="10" />
  </div>


<script>
export default {
  props: {
    value: Array,
    maxValue: Number,
  },
  methods: {
    onInput(event) {
      const newValue = parseInt(event.target.value);
      const clampedValue = Math.min(newValue, this.maxValue);
      this.$emit("input", clampedValue);
      this.$forceUpdate();
    },
  },

};
</script>

Again, everything worked for me until I replaced the 'value' with an array, you can also look at my code in codesandbox

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

When you are using

<div>
     <customInput v-model="value[0]" :max-value="10" />
</div

In App.vue you are passing a single value instead of an Array to HelloWorld.vue

So, either you change the type of value in the component from Array to Number, or change to:

<div>
     <customInput v-model="value" :max-value="10" />
</div

Also, in HelloWorld.vue the input type is "number" so even if you pass the array to the component you will have the warning.

So, do you need an array passed to the children component?

P.S. In the codesandbox you have this line missing the base: const newValue = parseInt(event.target.value); it should be: const newValue = parseInt(event.target.value, 10);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can send first element from array to children component, but you receive it in props as Number:

Vue.component('custom-input', {
  template: `
    <div>
      <input :value="value" type="number" @input="onInput" :max="maxValue" />
    </div>
  `,
  props: {
    value: Number,
    maxValue: Number,
  },
  methods: {
    onInput(event) {
      const newValue = parseInt(event.target.value);
      const clampedValue = Math.min(newValue, this.maxValue);
      this.$emit("input", clampedValue);
      this.$forceUpdate();
    },
  },
})

new Vue({
  el: '#demo',
  data() {
    return {
      value: [1, 1],
    }
  },
})

Vue.config.productionTip = false
Vue.config.devtools = false
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
    <custom-input v-model="value[0]" :max-value="10" />
  </div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda