【发布时间】:2016-02-25 11:52:09
【问题描述】:
我可以使用 [routerLink] 在 View 中完美导航。当我尝试使用 this.router.navigate(['/Todos']) 或 this.router.navigateByUrl('/todos') 浏览组件时,最初路由器正确更改为 index.html#/todos,然后路由器自动更改为 index.html?#/login。我不知道为什么会这样。谁能帮我?提前致谢。
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) {
}],
});
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 = {}));
login.js
(function (app) {
app.LoginComponent = ng.core
.Component({
selector: 'login',
templateUrl: 'app/views/login.html',
})
.Class({
constructor: [ng.router.Router, function (router) {
this.router = router;
}],
onSubmit: function (form, user) {
this.router.navigate(['/Todos']);
//this.router.navigateByUrl('/todos');
},
});
})(window.app || (window.app = {}));
【问题讨论】:
-
你试过
this.router.navigate(['Todos'])没有/吗? -
是的,我试过了。这也会产生同样的问题。
-
另外,我通过注入 ng.router.Location 尝试了 this.location.go('/todos')。在这种情况下,URL 已正确更改,但未加载组件。我仍然停留在登录页面。
-
this.router.parent.navigate(['Todos'])可能是另一种选择。我自己还不知道路由:-/ -
如果你删除/评论
/login路由会发生什么?
标签: angular angular2-routing angular2-directives