0

我可以使用 [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 = {}));
4

1 回答 1

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 。

于 2016-03-04T08:07:53.680 回答