Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] beforeRouteEnter's next(vm => { }) callback not always works #779

Closed
fy0 opened this issue Oct 14, 2016 · 18 comments
Closed

[2.0] beforeRouteEnter's next(vm => { }) callback not always works #779

fy0 opened this issue Oct 14, 2016 · 18 comments

Comments

@fy0
Copy link

fy0 commented Oct 14, 2016

This bug this about different path with the same component.

index.vue

<template>
  <div>
    <router-link :to="{ path: '/' }">root</router-link>
    <router-link :to="{ path: '/a' }">a</router-link>
    <router-link :to="{ path: '/b' }">b</router-link>
  </div>
</template>
  beforeRouteEnter: (to, from, next) => {
    console.log(111)
    next(vm => {
      console.log(222,  vm) // <- this not always be printed
    });
  }

main.js

import Index from './index.vue'

Vue.use(VueRouter)

var routes = [
    { path: '/', component: Index },
    { path: '/a', component: Index },
    { path: '/b', component: Index },
]

var router = new VueRouter({
    routes: routes
})

Then access / and just switch between a and b, "222" never be printed.
I already tested it on a new empty project generated by vue-cli. I'll upload that if you need.

@fnlctrl
Copy link
Member

fnlctrl commented Oct 14, 2016

Tested on jsfiddle, https://jsfiddle.net/fnlCtrl/a2v5rrcp/
Seems to be a bug.

@petergaal91
Copy link

Same here.

const router = new VueRouter({
    mode: 'history',
    routes: [
        {path: '*', beforeEnter (to, from, next) {
            alert('beforeEnter'); // Appears

            next((vm) => {
                alert("next"); // Doesnt appears
            });
        }}
    ]
});

@posva
Copy link
Member

posva commented Oct 24, 2016

@fy0 Why would you use the same component for different routes?

@fy0
Copy link
Author

fy0 commented Oct 26, 2016

@posva Just like the todo-list demo, different items and url, same page.

@posva
Copy link
Member

posva commented Oct 26, 2016

BTW, using a key attribute with the route path is a workaround for your problem (:key="$route.path" on the router-view element) https://jsfiddle.net/pyqchnrd/

@fy0
Copy link
Author

fy0 commented Oct 27, 2016

@posva Cool. But It doesn't matter the way it be implemented, bug is bug.

@posva
Copy link
Member

posva commented Oct 27, 2016

@fy0 that's why I didn't close it 😉

@fy0
Copy link
Author

fy0 commented Oct 27, 2016

@posva Yeah, thx for contributors' great works.

@crabmusket
Copy link

crabmusket commented Jan 17, 2017

I'm still seeing this issue using Vue 2.1.0 and Vue Router 2.1.1, but when switching between different params under the same route. I'm investigating whether it's something to do with my setup.

EDIT: I see now that param changes don't cause that function to fire anyway. Okay, I'll set up a watcher.

@dehypnosis
Copy link

Seems to still have a problem, anyone with me?

@SnooHD
Copy link

SnooHD commented Sep 9, 2017

I am also having this issue. adding a key to the router-view doesn't seem to work for me.

@fy0
Copy link
Author

fy0 commented Sep 10, 2017

@dehypnosis @SnooHD

It's a long time ago, now use beforeRouteUpdate to load new data.

And don't forget to call next(), otherwise afterEach won't be emitted.

@donghaohao
Copy link

Add a key seems work for me

chiangkeith added a commit to chiangkeith/readr-site that referenced this issue Apr 25, 2018
chiangkeith added a commit to readr-media/readr-site that referenced this issue Apr 25, 2018
chiangkeith added a commit to chiangkeith/readr-site-mobile that referenced this issue Apr 25, 2018
chiangkeith added a commit to readr-media/readr-site-mobile that referenced this issue Apr 25, 2018
@rdelgatte
Copy link

rdelgatte commented Apr 27, 2018

Same for me with <router-view :key="$route.path"/>

@ndethore
Copy link

Anyone else still experiencing this issue on vue-router@^3.0.1?

@ValterJunior
Copy link

Yes, I still get the same problem with vue-router@^3.0.1. Any ideas rather than forcing a key in the router-view ?

@joemsak
Copy link

joemsak commented Aug 17, 2018

yes, suggestions in comments don't work for me

@posva
Copy link
Member

posva commented Aug 17, 2018

There's an open issue for this at #2255

@vuejs vuejs locked and limited conversation to collaborators Aug 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests