darknoll
V2EX  ›  问与答

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

  •  
  •   darknoll · Jan 13, 2020 · 2274 views
    This topic created in 2336 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
        1
    airyland  
       Jan 13, 2020
    router.beforeEach
    shintendo
        2
    shintendo  
       Jan 13, 2020
    你需要路由守卫而不是生命周期钩子
    darknoll
        3
    darknoll  
    OP
       Jan 13, 2020
    @shintendo 使用 BeforeEnter 还是会执行生命周期啊
    shadowyue
        4
    shadowyue  
       Jan 13, 2020
    你为啥希望正常的 mounted 不触发呢?

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