darknoll
V2EX  ›  问与答

前端新手,请教个 vue 的路由跳转问题

  •  
  •   darknoll · Jan 13, 2020 · 2611 views
    This topic created in 2503 days ago, the information mentioned may be changed or developed.

    请教个问题,在一个页面的 created 钩子函数里面做了个 this.$route.push, 此时仍然会把生命周期执行完,有没有什么办法直接跳转到新的页面,不执行接下来的 mounted 啥的。

    8 replies  •  2020-01-13 17:47:16 +08:00
    airyland
    airyland
    Jan 13, 2020
    1
    router.beforeEach
    shintendo
    shintendo
    Jan 13, 2020
    2
    你需要路由守卫而不是生命周期钩子
    darknoll
    darknoll
    OP
    Jan 13, 2020
    3
    @shintendo 使用 BeforeEnter 还是会执行生命周期啊
    shadowyue
    shadowyue
    Jan 13, 2020
    4
    你为啥希望正常的 mounted 不触发呢?

    一般遇到比较奇怪的操作,先想想是不是有哪里不合理不合适呗
    shintendo
    shintendo
    Jan 13, 2020
    5
    @darknoll 怎么可能……
    duuu
    duuu
    Jan 13, 2020
    6
    你这个跳转逻辑应该写在前一个页面里判断跳转到 A 还是 B,而不是先跳转到 A 里面判断要不要留下来还是跳转到 B
    shellus
    shellus
    Jan 13, 2020
    8
    在 data 里面放个 continue: true
    在 created 里面 this.continue=false;
    然后在 mountedd 里面 if(this.continue === false){return;}
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1141 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 23:47 · PVG 07:47 · LAX 15:47 · JFK 18:47
    ♥ Do have faith in what you're doing.