Vue js - 从对话框获取答案以确认使用 Vue Router + Vuetify 的导航

Vue js - Get Answer from Dialog to confirm navigation w/ Vue Router + Vuetify

如果我有一个带有 vuetify 对话框的 vue 模板(但实际上是任何对话框),我如何使用它来确认导航离开 vue-router 的 beforeRouteLeave 方法中的页面?

dialogTest.vue:

<template>
    <v-container>
        <v-layout>
            <v-dialog v-model="dialog" max-width="290" ref="popup">
                <v-card>
                    <v-card-title class="headline">Are you sure you wish to leave this page?</v-card-title>
                    <v-card-text>Better think long and hard.</v-card-text>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="primary darken-1" flat="flat" @click.native="dialog = false">Nah</v-btn>
                        <v-btn color="primary darken-1" flat="flat" @click.native="dialog = false">Yah</v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>
        </v-layout>
    </v-container>
</template>

<script src="./dialogTest.ts"></script>

dialogTest.ts:

import Vue from 'vue';
import { Component } from 'vue-property-decorator';

Component.registerHooks([
    'beforeRouteLeave'
]);

@Component
export default class DialogTestComponent extends Vue {

    dialog: boolean = false;

    beforeRouteLeave(to: Object, from: Object, next: Function) {
        console.log('beforeRouteLeave');

        //this works, but obviously doesn't use our dialog -> how do we get yah or nah response from dialog instead?
        const answer =  window.confirm('Do you really want to leave? you have unsaved changes!')
        if (answer) {
            next()
        } else {
            next(false)
        }
    }
}

我喜欢用承诺来做到这一点。为您的对话框提供一个 returns 承诺的 pop() 方法,然后在用户选择时用 true 或 false 解决承诺。或者从单元测试中调用 clickYah() 。像这样...

// in your dialog component....
data(){
    return {active : false, resolve: null};
}
methods : {
    pop(){
        this.active = true;
        return new Promise(function(resolve, reject){
            this.resolve = resolve;
        });
    },
    clickYah(){
        this.active = false;
        this.resolve(true);
    },
    clickNah(){
        this.active = false;
        this.resolve(false);
    }
}

// then to call it...
this.$refs.modalDialog.pop()
.then(confirmResult => next(confirmResult));

@bbsimonbb - 感谢您的快速回答。

这是我在 ts 的决赛:

在父组件(其中包含我们的 ConfirmLeaveDialog 组件,ref="confirmLeavePopup"):

async beforeRouteLeave(to: Object, from: Object, next: Function) {
    next(await (this.$refs.confirmLeavePopup as ConfirmLeaveDialog).pop()); 
}

在ConfirmLeaveDialog vue class组件中(我将组件的resolve func存储重命名为"answer"):

import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';

@Component
export default class ConfirmLeaveDialog extends Vue {

    @Prop({ default: 'Are you sure you wish to leave this page?' })
    question: any;

    active: boolean = false;
    answer: Function = () => { return false }; //. had to provide the type and initialize

    pop(): Promise<boolean> {
        this.active = true;
        return new Promise<boolean>((resolve: Function, reject: Function) => { //. note the arrow function here such that 'this' refers to the component, NOT the calling context
            this.answer = resolve;
        });
    };

    confirmLeave(): void {
        this.active = false;
        this.answer(true);
    };

    abortLeave(): void {
        this.active = false;
        this.answer(false);
    }
}

如果您不想使用 $refs 并且您的组件中有一个 v-dialog

模板:

<v-dialog v-model="openDialog">
   <v-btn @click="dialogResponse(true)">Yes</v-btn>
   <v-btn @click="dialogResponse(false)">No</v-btn>
</v-dialog>

脚本:

data() { return { openDialog: false, functionResolve: null } },

beforeRouteLeave(to, from, next) {
    this.openDialog = true
    this.createPromise().then(res => {
      next(res)
    })
},

methods: {
  createPromise() {
    return new Promise(resolve => {
      this.functionResolve = resolve;
    })
  },
  dialogResponse(response) {
    this.functionResolve(response)
  },
}