Vuejs:在路线更改之前显示确认对话框
Vuejs: Show confirmation dialog before route change
在我的 vueJS 项目中,我想在当前路由更改之前显示确认对话框。
是的,它应该重定向到下一个所需的路线,否则将保持在同一路线上。
知道如何实现吗?
提前致谢。
VueJS 有 In Component Navigation Guards 像 beforeRouteUpdate
和 beforeRouteLeave
beforeRouteEnter (to, from, next) {
// called before the route that renders this component is confirmed.
// does NOT have access to `this` component instance,
// because it has not been created yet when this guard is called!
},
...
beforeRouteLeave (to, from, next) {
// called when the route that renders this component is about to
// be navigated away from.
// has access to `this` component instance.
}
您可以使用 In-Component Guards beforeRouteLeave
。参见 https://router.vuejs.org/en/advanced/navigation-guards.html。
演示:https://codesandbox.io/s/jzr5nojn39(尝试在主页、第 1 页和第 2 页之间导航)
示例代码(使用 vuejs-dialog 作为确认对话框):
beforeRouteLeave (to, from, next) {
this.$dialog.confirm('Do you want to proceed?')
.then(function () {
next();
})
.catch(function () {
next(false);
});
}
如果要继续,请使用 next()
。
如果要取消重定向,请使用next(false)
。
以下代码适合我
<v-btn @click="deleteDialog = true">Delete</v-btn>
<v-dialog v-model="deleteDialog" max-width="500px">
<v-card>
<v-card-title style="font-size:20px" >Are you sure you want to archive?</v-card-title>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="red" style="font-size:15px" flat @click.native="deleteDialog = false">No</v-btn>
<v-btn color="green" style="font-size:15px" flat @click.native="deleteItem">Yes</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
The accepted answer shows how to do it by using vuejs-dialog. But if you don't want to use this library check below:
假设您有一个包含 3 个选项的对话框:
关闭对话框 => 调用 closeDialog()
并停留在同一页面
保存更改 => 调用 saveChanges()
保存更改并离开
放弃更改 => 调用 discardChanges()
导航离开而不保存更改
data: () => ({
to: null,
showDialog: false
}),
beforeRouteLeave(to, from, next) {
if (this.to) {
next();
} else {
this.to = to;
this.showDialog = true;
}
},
methods: {
closeDialog() {
this.showDialog = false;
this.to = null;
},
saveChanges() {
// add code to save changes here
this.showDialog = false;
this.$router.push(this.to);
},
discardChanges() {
this.showDialog = false;
this.$router.push(this.to);
}
}
See it in action in codesandbox
要点 这里的关键要点是 beforeRouteLeave 导航守卫,如果 to
属性 中的数据为空。唯一不能为 null 的情况是当用户单击对话框中的保存或丢弃按钮时。
在我的 vueJS 项目中,我想在当前路由更改之前显示确认对话框。
是的,它应该重定向到下一个所需的路线,否则将保持在同一路线上。
知道如何实现吗?
提前致谢。
VueJS 有 In Component Navigation Guards 像 beforeRouteUpdate
和 beforeRouteLeave
beforeRouteEnter (to, from, next) {
// called before the route that renders this component is confirmed.
// does NOT have access to `this` component instance,
// because it has not been created yet when this guard is called!
},
...
beforeRouteLeave (to, from, next) {
// called when the route that renders this component is about to
// be navigated away from.
// has access to `this` component instance.
}
您可以使用 In-Component Guards beforeRouteLeave
。参见 https://router.vuejs.org/en/advanced/navigation-guards.html。
演示:https://codesandbox.io/s/jzr5nojn39(尝试在主页、第 1 页和第 2 页之间导航)
示例代码(使用 vuejs-dialog 作为确认对话框):
beforeRouteLeave (to, from, next) {
this.$dialog.confirm('Do you want to proceed?')
.then(function () {
next();
})
.catch(function () {
next(false);
});
}
如果要继续,请使用 next()
。
如果要取消重定向,请使用next(false)
。
以下代码适合我
<v-btn @click="deleteDialog = true">Delete</v-btn>
<v-dialog v-model="deleteDialog" max-width="500px">
<v-card>
<v-card-title style="font-size:20px" >Are you sure you want to archive?</v-card-title>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="red" style="font-size:15px" flat @click.native="deleteDialog = false">No</v-btn>
<v-btn color="green" style="font-size:15px" flat @click.native="deleteItem">Yes</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
The accepted answer shows how to do it by using vuejs-dialog. But if you don't want to use this library check below:
假设您有一个包含 3 个选项的对话框:
关闭对话框 => 调用 closeDialog()
并停留在同一页面
保存更改 => 调用 saveChanges()
保存更改并离开
放弃更改 => 调用 discardChanges()
导航离开而不保存更改
data: () => ({
to: null,
showDialog: false
}),
beforeRouteLeave(to, from, next) {
if (this.to) {
next();
} else {
this.to = to;
this.showDialog = true;
}
},
methods: {
closeDialog() {
this.showDialog = false;
this.to = null;
},
saveChanges() {
// add code to save changes here
this.showDialog = false;
this.$router.push(this.to);
},
discardChanges() {
this.showDialog = false;
this.$router.push(this.to);
}
}
See it in action in codesandbox
要点 这里的关键要点是 beforeRouteLeave 导航守卫,如果 to
属性 中的数据为空。唯一不能为 null 的情况是当用户单击对话框中的保存或丢弃按钮时。