我可以使用 [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 = {}));
引导.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 = {}));
登录.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 = {}));