【发布时间】:2017-08-09 01:51:32
【问题描述】:
我正在进行登录检查。一旦成功,我想将用户重定向到同一应用程序中的另一个页面。我有以下代码可以做到这一点。
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() 不会加载 URL。
【问题讨论】:
-
我认为你应该使用
router进行重定向,你使用routing吗? -
是的。路由器为我工作。我已经推荐了stackoverflow.com/questions/33571605/…
标签: angular typescript angular2-routing