Last active
May 30, 2020 18:51
-
-
Save ramon-src/cdf4838bb59cdaec4194badf368fdb1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app | |
<template> | |
<carros v-if="opcionais.length" :opcionais="opcionais" | |
</template> | |
computed: { | |
opcionais() { | |
return this.$store.getters.opcionais; | |
}, | |
}, | |
// componente de carros | |
props: { | |
opcionais: {type: Array, required: true} | |
} | |
created() { | |
this.setId() | |
} | |
methods: { | |
setId() { | |
//o problema maior que enquanto não for setado um valor no teu array de opcionais | |
// ele retorna undefined na expressão this.opcionais[this.opcionais.length - 1] | |
// logo this.opcionais[this.opcionais.length - 1].id podia quebrar dizendo que | |
// não consegue pegar a propriedade id de undefined | |
// da pra tratar dessa maneira a baixo | |
const encontrouOpcional = this.opcionais[this.opcionais.length - 1] | |
if (encontrouOpcional && encontrouOpcional.id) { | |
this.id = encontrouOpcional.id | |
} | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment