Angular 防止默认布线-角度2

Angular 防止默认布线-角度2,angular,angular2-routing,angular2-directives,Angular,Angular2 Routing,Angular2 Directives,如何防止angular 2中的默认布线?在路由器订阅中,我只得到路径名。我不想卷入其中。angular 2中是否有其他服务提供商获取路线更改事件 app.component.js (function (app) { app.AppComponent = ng.core .Component({ selector: 'my-app', templateUrl: 'app/views/main.html', d

如何防止angular 2中的默认布线?在路由器订阅中,我只得到路径名。我不想卷入其中。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 = {}));
(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 = {}));
boot.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 = {}));
(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 = {}));
您可以使用来防止路线更改

有关当前限制,请参见。

您可以使用来防止路线更改


另请参阅以了解当前的限制。

如果要在纯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);
ComponentInstruction {
  urlPath: "child1",
  urlParams: Array[0],
  terminal: true,
  specificity: "2",
  params: Object…
}

如果要在普通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);
ComponentInstruction {
  urlPath: "child1",
  urlParams: Array[0],
  terminal: true,
  specificity: "2",
  params: Object…
}


@ThierryTemplier你能帮我解决这个问题吗?你想做什么还不是很清楚。是否要阻止具有单击处理程序的链接的默认行为?例如,
$scope.$on(“$routeChangeStart',function(event,next,current){})
在angular 1中,$routeChangeStart赋予路由更改事件权限?和我在angular 2中需要的一样。我已使用路由器订阅获取路由更改。但我只能得到路径名。@ThierryTemplier您能帮我解决这个问题吗?您想做什么还不是很清楚。是否要阻止具有单击处理程序的链接的默认行为?例如,
$scope.$on(“$routeChangeStart',function(event,next,current){})
在angular 1中,$routeChangeStart赋予路由更改事件权限?和我在angular 2中需要的一样。我已使用路由器订阅获取路由更改。但我只知道路径名。你能给出一个CanActivate的Javascript代码示例吗?你的问题不清楚。见上面的评论。链接的问题有带代码的答案。你也可以看看我的问题很简单。我需要防止登录后返回按钮导航。在angular 1中,$routeChangeStart在路由更改时提供事件next和prev。和我在angular 2中需要的一样。我该怎么做?你的问题不包含任何关于这个的内容。我认为你应该编辑你的问题并添加这个。我不知道安格拉尔。你真的想阻止后退按钮还是想操纵后退按钮的方向?你为什么需要这个
@CanActivate()
提供了prev和next,当您返回
false
时,导航将被取消。现在我明白了。但我尝试了代码
ng.router.CanActivate(函数(next,prev){returntrue;})。它不起作用。我不能清楚地理解CANActive文档。你能告诉我在Javascript中使用CanActivate的代码吗?你能给出一个CanActivate的Javascript代码示例吗?你的问题不清楚。见上面的评论。链接的问题有带代码的答案。你也可以看看我的问题很简单。我需要防止登录后返回按钮导航。在angular 1中,$routeChangeStart在路由更改时提供事件next和prev。和我在angular 2中需要的一样。我该怎么做?你的问题不包含任何关于这个的内容。我认为你应该编辑你的问题并添加这个。我不知道安格拉尔。你真的想阻止后退按钮还是想操纵后退按钮的方向?你为什么需要这个
@CanActivate()
提供了prev和next,当您返回
false
时,导航将被取消。现在我明白了。但我尝试了代码
ng.router.CanActivate(函数(next,prev){returntrue;})。它不起作用。我不能清楚地理解CANActive文档。你能告诉我在Javascript中使用CanActivate的代码吗?它100%正常工作。是否需要为每个子组件添加
CanActivate
?我可以为AppComponent添加
CanActivate
,这样我就可以在每次路由更改时监听,而无需为每个子组件添加
CanActivate
?太好了<必须为每个组件定义code>CanActivate
,因为它将允许实例化或不实例化组件…确定。一切都完成了。非常感谢你的帮助。我从你那里学到了很多。谢谢
CanActivate
在angular 2 rc版本中不工作。你能给出一个正确的代码吗?它100%工作。是否需要为每个子组件添加
CanActivate
?我可以为AppComponent添加
CanActivate
,这样我就可以在每次路由更改时监听,而无需为每个子组件添加
CanActivate
?太好了<必须为每个组件定义code>CanActivate
,因为它将允许实例化或不实例化组件…确定。一切都完成了。非常感谢你的帮助。我从你那里学到了很多。谢谢
CanActivate
在angular 2 rc版本中不工作。你能给我一个正确的代码吗?