admin管理员组文章数量:1289528
I'm trying to implement Vue Router's in-ponent navigation guards using the vue-property-decorator package. Underlying, vue-property-decorator relies on vue-class-ponent package. It offers a registerHooks
method, allowing you to declare what methods should be treated as lifecycle hooks (instead of instance methods).
The following code works, resulting in an alert when entering the route:
import { Component, Vue } from 'vue-property-decorator';
import { router } from './Router';
@Component({
template: '<div></div>'
})
export class Foo extends Vue {
beforeRouteEnter(to, from, next){
alert('Entering Foo');
next();
}
}
router.addRoutes([
{ path: '/foo', ponent: Foo }
]);
The Router.ts file contains the hook registration as per the docs and is exactly:
import VueRouter from 'vue-router';
import { Component } from 'vue-property-decorator'
Component.registerHooks([
'beforeRouteEnter',
'beforeRouteUpdate',
'beforeRouteLeave'
]);
export const router = new VueRouter({
mode: 'abstract'
});
The router file is imported in an App.ts file, before the ponents are imported, hence the hooks are registered before the ponents are.
However, I seem to be unable to pass in a callback to the next
method. Given the updated Foo
ponent from above:
import { Component, Vue } from 'vue-property-decorator';
import { router } from './Router';
@Component({
template: '<div></div>'
})
export class Foo extends Vue {
beforeRouteEnter(to, from, next){
next(vm => {
alert('Entering Foo');
});
}
}
The alert is never fired. Am I missing something, or it this a bug in the vue-class-ponent package? I should add that I'm also unable to pass the callback to the next function in the per route and global route navigation guards.
Many thanks in advance for any help, greatly appreciated!
I'm trying to implement Vue Router's in-ponent navigation guards using the vue-property-decorator package. Underlying, vue-property-decorator relies on vue-class-ponent package. It offers a registerHooks
method, allowing you to declare what methods should be treated as lifecycle hooks (instead of instance methods).
The following code works, resulting in an alert when entering the route:
import { Component, Vue } from 'vue-property-decorator';
import { router } from './Router';
@Component({
template: '<div></div>'
})
export class Foo extends Vue {
beforeRouteEnter(to, from, next){
alert('Entering Foo');
next();
}
}
router.addRoutes([
{ path: '/foo', ponent: Foo }
]);
The Router.ts file contains the hook registration as per the docs and is exactly:
import VueRouter from 'vue-router';
import { Component } from 'vue-property-decorator'
Component.registerHooks([
'beforeRouteEnter',
'beforeRouteUpdate',
'beforeRouteLeave'
]);
export const router = new VueRouter({
mode: 'abstract'
});
The router file is imported in an App.ts file, before the ponents are imported, hence the hooks are registered before the ponents are.
However, I seem to be unable to pass in a callback to the next
method. Given the updated Foo
ponent from above:
import { Component, Vue } from 'vue-property-decorator';
import { router } from './Router';
@Component({
template: '<div></div>'
})
export class Foo extends Vue {
beforeRouteEnter(to, from, next){
next(vm => {
alert('Entering Foo');
});
}
}
The alert is never fired. Am I missing something, or it this a bug in the vue-class-ponent package? I should add that I'm also unable to pass the callback to the next function in the per route and global route navigation guards.
Many thanks in advance for any help, greatly appreciated!
Share Improve this question edited Sep 3, 2018 at 12:09 thomaux asked Sep 3, 2018 at 11:05 thomauxthomaux 19.7k10 gold badges81 silver badges105 bronze badges 1- 4 I encountered similar problem, but thanks to your post , I see that I have to register explicitly this router hooks, so my problem has been solved. Hope you figure yours out~ – Allan Ruin Commented Sep 29, 2018 at 7:44
1 Answer
Reset to default 10Please move your beforeRouteEnter hook to @Component
decorator
@Component({
template: '<div></div>',
beforeRouteEnter(to, from, next){
next(vm => {
alert('Entering Foo');
});
}
})
版权声明:本文标题:javascript - Vue Router with vue-class-component: next function does not accept callback option - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741465017a2380261.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论