【发布时间】:2018-08-09 06:05:19
【问题描述】:
如果我有一个带有 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)
}
}
}
【问题讨论】:
标签: vue.js vuejs2 vue-component vue-router vuetify.js