Skip to content

Instantly share code, notes, and snippets.

@ramon-src
Last active May 30, 2020 18:51
Show Gist options
  • Save ramon-src/cdf4838bb59cdaec4194badf368fdb1b to your computer and use it in GitHub Desktop.
Save ramon-src/cdf4838bb59cdaec4194badf368fdb1b to your computer and use it in GitHub Desktop.
// 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