search

Home  >  Q&A  >  body text

javascript - In vue.js, when opening a modal box component on the page, how to press the return key to make the modal box disappear instead of returning to the previous page?

As the title says, in vue.js, when opening a modal box component on the page, how can you press the return key (Android return key or browser return key) to make the modal box disappear instead of returning to the previous page?
ps. Can you try adding history records to window.history when the modal box is opened? If possible, how to implement

伊谢尔伦伊谢尔伦2777 days ago595

reply all(3)I'll reply

  • 阿神

    阿神2017-05-19 10:15:36

    H5 page cannot be operated directly

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-19 10:15:36

    If there is a way to monitor when the user presses the return key, then disable the default event of the return key when the modal box is displayed and close the modal box instead.

    If you want to use history recording, you can try changing the hash value

    reply
    0
  • 为情所困

    为情所困2017-05-19 10:15:36

    If you use vue-router, you can place another modal routing view below the main routing view, and then configure the modal routing in the routing configuration file as a sub-route of the main routing view, for example like this :

    foo.vue
    <template>
      <router-view></router-view>
    </template>
    
    modal.vue
    <template>
      <router-view></router-view>
    </template>
    
    route.js(vue-router2)
    module.exports = {
      routes: [{
        path: '/foo',
        name: 'foo',
        component(resolve) {
          require(['foo.vue'], resolve);
        },
        children: [{
          name: 'modal',
          path: 'modal',
          component(resolve) {
            require(['modal.vue'], resolve);
          }
        }]
      }]
    }

    In this case, when a modal component is needed, just router.push the routing path of the modal component

    reply
    0
  • Cancelreply