可以在HttpClient的get方法中添加一个配置参数,通过设置responseType为"text",让响应结果作为字符串类型返回,然后使用eval方法进行处理。示例如下:
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) {}
// 发起get请求
this.http.get('http://example.com/script.js', { responseType: 'text' })
.subscribe((script: string) => {
// 使用eval方法对返回的脚本进行处理
eval(script);
});
上一篇:Angular2合并对象的部分