要在Angular 7中在HTTP请求完全执行后点击路由,可以使用RxJS的操作符来实现。
首先,您需要在组件的构造函数中导入Router
和HttpClient
:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
然后,您可以在组件类中创建一个方法来执行HTTP请求并在请求完成后进行路由导航。在该方法中,您可以使用subscribe
来订阅HTTP请求的结果,并在请求成功后执行路由导航。
export class YourComponent implements OnInit {
constructor(private router: Router, private http: HttpClient) { }
ngOnInit() {
// 执行HTTP请求
this.http.get('https://api.example.com/data').subscribe(
response => {
// 请求成功后执行路由导航
this.router.navigate(['/your-route']);
},
error => {
// 处理请求错误
}
);
}
}
在上面的示例中,我们使用了HttpClient
发送了一个GET请求到https://api.example.com/data
。当请求成功后,subscribe
函数中的第一个回调函数将被调用,并执行this.router.navigate(['/your-route'])
来导航到指定的路由。
请确保将'/your-route'
替换为您要导航到的实际路由路径。
请注意,如果您在组件的构造函数中调用HTTP请求,而不是在ngOnInit
中,那么您可能需要在构造函数中使用setTimeout
来确保HTTP请求完成后再进行路由导航。这是因为HTTP请求是异步执行的,可能不会立即完成。