1
我正在進行登錄檢查。一旦成功,我想將用戶重定向到同一應用程序中的另一個頁面。我有下面的代碼來做到這一點。location.go()沒有重定向角2中的用戶
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)
});;
}
}
但問題是,在URL瀏覽器改爲http://localhost:3000/report。但是當頁面沒有加載時。當我點擊重新加載時,新頁面正在顯示。
這段代碼有什麼問題? location.go()
將不會加載該網址。
我認爲你應該使用'router'作爲重定向,你是否使用'routing'? –
是的,路由器爲我工作,我引用了http://stackoverflow.com/questions/33571605/angular-2-how -to-導航到另一個路由 - 使用 - 這個路由器,家長導航 –