gpt4 book ai didi

angular - location.go() 没有以 Angular 2 重定向用户

转载 作者:太空狗 更新时间:2023-10-29 17:25:38 25 4
gpt4 key购买 nike

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

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。

最佳答案

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

引用那篇关于 Location 类的文章:“

Note: it's better to use Router service to trigger route changes. Use Location only if you need to interact with or create normalized URLs outside of routing.

关于angular - location.go() 没有以 Angular 2 重定向用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42858644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com