【问题标题】:Prevent Default Routing - Angular 2防止默认路由 - Angular 2
【发布时间】:2016-03-04 10:46:11
【问题描述】:

如何防止默认 angular 2 中的路由?在 router subscribe 中,我只得到路径名。我没有参与其中。 Angular 2 中是否还有其他服务提供者来获取路由更改事件?

app.component.js

(function (app) {

app.AppComponent = ng.core
        .Component({
            selector: 'my-app',
            templateUrl: 'app/views/main.html',
            directives: [ng.router.ROUTER_DIRECTIVES],
            viewProviders: [ng.http.HTTP_PROVIDERS]
        })
        .Class({
            constructor: [ng.router.Router, ng.http.Http, function (router, http) {

                   this.router.subscribe(function (pathname) {
                       //console.log(pathname);
                   });

            }],
        });

ng.router
        .RouteConfig([
          { path: '/login', component: app.LoginComponent, name: 'Login', useAsDefault: true },
          { path: '/todos', component: app.TodosComponent, name: 'Todos' },
        ])(app.AppComponent);

})(window.app || (window.app = {}));

boot.js

(function (app) {

    document.addEventListener('DOMContentLoaded', function () {
        ng.platform.browser.bootstrap(app.AppComponent, [ng.router.ROUTER_PROVIDERS, ng.core.provide(ng.router.LocationStrategy, { useClass: ng.router.HashLocationStrategy })]);
    });

})(window.app || (window.app = {}));

【问题讨论】:

  • @ThierryTemplier 你能帮忙解决这个问题吗?
  • 不太清楚你想做什么。例如,您是否要阻止具有点击处理程序的链接的默认行为?
  • $scope.$on('$routeChangeStart', function(event, next, current){ }) 在角度 1 中,$routeChangeStart 给出了路线更改的事件,对吗?我在 Angular 2 中需要同样的东西。我使用路由器订阅来获取路由更改。但我只得到路径名。

标签: angular angular2-routing angular2-directives


【解决方案1】:

如果您想在纯 JS 中使用 CanActivate 装饰器,请执行以下操作:

var Child1Component = ng.core.Component({
  selector:'test',
  template: '<div>Test</div>'
}).Class({
  onSubmit: function(){
    console.log('onSubmit');
  }
});

ng.router.CanActivate((next, prev) => {
  // The Child1Component component is instantiated
  return true;

  // The Child1Component component isn't instantiated
  // return false;
})(Child1Component);

例如,在启动时,prev 参数为空,next 包含以下内容:

ComponentInstruction {
  urlPath: "child1",
  urlParams: Array[0],
  terminal: true,
  specificity: "2",
  params: Object…
}

【讨论】:

  • 它是 100% 工作的。我需要为每个子组件添加CanActivate 吗?我可以为 AppComponent 添加CanActivate,这样我就可以在每次路由更改时收听,而无需为每个子组件添加CanActivate
  • 太棒了! CanActivate 必须为每个组件定义,因为它将允许组件被实例化或不被实例化...
  • 好的。一切都完成了。非常感谢你的帮助。我从你身上学到了很多。谢谢
  • CanActivate 在 angular 2 rc 版本中不起作用。你能给出一个正确的代码吗?
【解决方案2】:

您可以使用@CanActivate() 来防止路由更改。

有关当前限制,另请参阅 Angular 2: Inject a dependency into @CanActivate?

【讨论】:

  • 你能给一个 CanActivate 的示例 Javascript 代码吗?
  • 不清楚你的问题是什么。见上面的 cmets。链接的问题有代码的答案。您也可以查看github.com/angular/angular/issues/4112#issuecomment-153811572
  • 我的问题很简单。登录后我需要防止后退按钮导航。在角度 1 中,$routeChangeStart 在路由更改时给出 event, next, prev。我在 Angular 2 中需要同样的东西。我该怎么做?
  • 您的问题不包含任何关于此的内容。我认为你应该编辑你的问题并添加这个。我不知道 Angular1。你真的想阻止后退按钮还是要操纵后退按钮通向的位置?你为什么需要这个? @CanActivate() 提供 prev 和 next,当您返回 false 时,导航被取消。
  • 现在我明白了。但我尝试使用代码ng.router.CanActivate(function (next, prev) { return true; });。它不工作。我无法清楚地理解 CanActivate 文档。你能告诉我在 Javascript 中使用 CanActivate 的代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
  • 2016-07-03
  • 2016-03-26
  • 2017-07-27
相关资源
最近更新 更多