我可以使用[routerLink]完美浏览View。当我尝试使用 this.router.navigate([' / Todos'])或 this.router.navigateByUrl(' / todos& #39;),最初路由器正在正确更改为 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 = {}));
答案 0 :(得分:0)
根据您的最新评论,我认为您的问题与您提交表单的方式有关:
<form #simpleForm="ngForm" novalidate>
<div>
<input type="text" placeholder="Name"
[(ngModel)]="user.name" ngControl="name"
#name="ngForm" required />
</div>
<button type="submit" (click)="onSubmit(simpleForm, user)">Login</button>
</form>
您应该直接利用表单上的submit
事件,如下所述:
<form #simpleForm="ngForm" (submit)="onSubmit(simpleForm, user)">
<div>
<input type="text" placeholder="Name"
[(ngModel)]="user.name" ngControl="name"
#name="ngForm" required />
</div>
<button type="submit">Login</button>
</form>
这是相应的plunkr:https://plnkr.co/edit/w61Ecbmuj7EfDnsYEHOS?p=preview。