如何在几次导航后关闭 nativescript 模式?

How to close a nativescript modal after a few navigations?

一个模式被打开,它有多个页面。对于导航,我使用的是框架导航。每个页面上都有一个关闭按钮,单击它会关闭模式。

我现在正在做的是将 this.$modal 作为 属性 传递给每个页面,这会创建一个长长的 属性 传递链,而在每个页面上我只是做 this.modal.close() 其中 this.modal 是引用第一页 this.$modal 的组件的 属性。

我想知道是否有更好的方法,比如访问最顶部的打开模式并关闭它。

我正在使用 nativescript-vue 和内置的 nativescript modals

请注意,我在应用程序的其他部分有多个模态框。只有这个有导航功能。

一个小的改进可能是将模态保存到 Vuex 存储中,随时访问它而不是链接道具。

  1. 通过插件分离模态组件。
const modalDialog = {
  install (Vue, options = {}) {
     // ...
  }
}

Vue.use(modalDialog)
  1. 为插件指定 Vue 原型。
const modalDialog = {
  install (Vue, options = {}) {
     Vue.prototype.$modal = {
       show () {
         // ...
       },
       hide () {
         // ..
       }
     }
  }
}

Vue.use(modalDialog)
  1. this.$modal 可从所有组件访问。
this.$modal.show() // or hide()