【发布时间】: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