location.go()不会以角度2重定向用户

San*_*gde 6 typescript angular2-routing angular

我正在进行登录检查.一旦成功,我想将用户重定向到同一应用程序中的另一个页面.我有以下代码来做到这一点.

import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import { DataService } from '../services/data.service';
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
   providers: [Location, {provide: LocationStrategy, useClass: PathLocationStrategy}],
  styleUrls: ['./login.component.css'],

})
export class LoginComponent   {

static location:Location;

  constructor(private formBuilder: FormBuilder,
              private dataService: DataService,location: Location) {
                LoginComponent.location = location;

      this.loginForm = this.formBuilder.group({
      username: this.username,
      password: this.password
    });
  }

  loginForm : FormGroup;
  username = new FormControl('', Validators.required);
  password = new FormControl('', Validators.required);

Login(){

  this.dataService.doLogin(this.loginForm.value.username,this.loginForm.value.password).subscribe(function(res){
       console.log(LoginComponent.location)
       LoginComponent.location.go('/report');
     },function(err){
        console.log("Err",err)
      });;
}
Run Code Online (Sandbox Code Playgroud)

}

但问题是,浏览器中的URL更改为http:// localhost:3000/report.但是当页面没有加载时.当我点击重新加载时,会显示新页面.

这段代码有什么问题?location.go()不会加载URL.

Ima*_*ovs 7

如果使用角度路由器,则使用位于angular/router命名空间的Router类.看看这个 Angular Router Navigation示例.我认为这将有助于解决您的问题. Location类用于与URL交互,但不在应用程序路由中导航(就像在角度1中一样).

引用那篇关于位置类的文章:"

注意:最好使用路由器服务来触发路由更改.仅当您需要在路由之外进行交互或创建规范化URL时才使用位置.