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

354
Visualizações
How to pass Boolean props to another component in vuejs

I have two components: component A and Component B. In the component A, I have a props called hasBorder with Boolean type and when i call component A in the component B, I passed value to a props component of component A, I have an error.

html component A:

<div> <img src="path" :style="hasBorder ? 'border:5px;': ''"/></div>

js component A:

Vue.component('compA', {
    props: {
       hasBorder:{
         type:Boolean,
         default:false
       }
    }
});

html component B:

<div> My image : <compA has-border="myStyle"></compA></div>

js component B:

Vue.component('compB', {
    data: {
       return {
           myStyle : { type:Boolean, default :true}
        } 
    }
});

I have this error "Invalid prop:type check failed for prop hasBorder. Expected Boolean, got String with value "myStyle".

How can i fix this error please

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

0

You expect bool value but you are trying to send an object.

Try this

Vue.component('compB', {
    data: {
       return {
           myStyle : true
        } 
    }
});

And use v-bind

<div> My image : <compA v-bind:has-border="myStyle"></compA></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to bind your props with v-bind: or : in order to pass data. Data property needs to be a function that returns object. Please take a look at snippet:

const app = Vue.createApp({
  data() {
    return {
      myStyle: true,
      myPath: 'https://picsum.photos/100'
    };
  },
})
app.component('compA', {
  template: `
    <div><img :src="path" :style="hasBorder ? 'border: 5px solid turquoise;': ''"/></div>
  `,
  props: {
    hasBorder:{
      type:Boolean,
      default:false
    },
    path: {
      type: String,
      default: ''
    }
  }
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <div> My image : <comp-a :has-border="myStyle" :path="myPath"></comp-a></div>
</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