V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
darknoll
V2EX  ›  问与答

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

  •  
  •   darknoll · 2020-01-13 16:50:23 +08:00 · 1659 次点击
    这是一个创建于 1536 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    一般遇到比较奇怪的操作,先想想是不是有哪里不合理不合适呗
    shintendo
        5
    shintendo  
       2020-01-13 17:08:45 +08:00
    @darknoll 怎么可能……
    duuu
        6
    duuu  
       2020-01-13 17:09:23 +08:00
    你这个跳转逻辑应该写在前一个页面里判断跳转到 A 还是 B,而不是先跳转到 A 里面判断要不要留下来还是跳转到 B
    shellus
        8
    shellus  
       2020-01-13 17:47:16 +08:00
    在 data 里面放个 continue: true
    在 created 里面 this.continue=false;
    然后在 mountedd 里面 if(this.continue === false){return;}
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5372 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:07 · PVG 15:07 · LAX 00:07 · JFK 03:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.