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

169
Visualizações
vue back button should close dialog if open

I have a vue.js project. The vue-component can create a dialog. If the user presses the back button I want to close this dialog (if it is open) instead of leaving the vue-component. How can I achieve this?

I tried this:

beforeRouteLeave(to, from, next) {
    let self = this;
    //dialog is open
    if(self.isShowingDialog){
        //closes the dialog
        self.isShowingDialog = false;
    }
    else {
        //navigates back to previous component
        next();
    }
},

It works, but not if I open the the component for the first time and the history is empty. In this case the back button exit my app instead of closing the dialog.

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

0

It works now with the hashtag/anchor as suggested by @Kapcash. If I press now the back button the dialog is hidden and my main component doesn't close.

MyComponent.vue:

<StockChooser v-if="showStockChooser" v-model="showStockChooser">

import StockChooser from "./components/StockChooser";
Vue.component('StockChooser', StockChooser)

data() {
  return {
    showStockChooser: false,
  }
},
methods: {
 openDialog: {
   this.showStockChooser = true;
   this.$router.push('#dialogStockChooser')
 }
}

StockChooser.vue:

<v-dialog v-model="show" v-if="show" persistent fullscreen>
  <v-row>
    <v-btn color="red" @click="cancel()" style="width:100%">Cancel</v-btn>
  </v-row>
</v-dialog>

data() {
  return {
    show: false,
  }
},
watch: {
    "$route.hash": {
        handler: function(dialog_id) {
            if(dialog_id === '#dialogStockChooser'){
                this.show = true;
            }
            else {
                this.show = false;
            }
        }, immediate: true
    },
},
methods: {
   cancel(){
     this.$router.push(-1)
   },
}
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