我有一个 API,它返回一个 excel 文档作为响应。请求将是一个简单的 json。
我搜索了谷歌并找到了一些代码库来下载该文件,并且我已经在我的应用程序中使用了它,但是我遇到了一些错误并且无法找到解决方案。下面是我的代码库。
组件.ts
import rxjs/Rx;
details = {id: 75,name: "some name"}
this.nameService.getData(this.details).subscribe(response => {
this.downloadFile(response);
})
downloadFile(data: Response) {
const blob = new Blob([data], { type: '.xlsx' });
const url= window.URL.createObjectURL(blob);
window.open(url);
}
Run Code Online (Sandbox Code Playgroud)
名称Service.ts
getData(postData) {
return this.httpService.post('https://localhost:8080/getFile/', postData);
}
Run Code Online (Sandbox Code Playgroud)
httpService.ts
constructor(private http: HttpClient)
post(apiEndpoint: string, request: any) :Observable<any> {
return this.http.post<any>(apiEndpoint,request);
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码库,我遇到了两个错误并且文件没有下载。
在创建 Blob(const blob = new Blob([data], { type: '.xlsx' });) 时,“类型响应不能分配给类型 Blobpart”
(downloadFile(data: any))
则会出现上述任何错误(1),但我收到的httperror
响应为“语法错误:json 中位置 0 处的意外标记 …我使用“ create-react-app ”创建了一个示例反应应用程序。我需要使用X-Frame-options
和设置请求标头Content-Security-Policy
。我该怎么做呢?我只是尝试index.html
使用以下代码更新我的代码,但我不确定这是否正确。有人能帮忙吗?
<meta http-equiv="Content-Security-Policy" content="frame-ancestors 'none'">
<meta http-equiv="X-Frame-Options" content="SAMEORIGIN">
Run Code Online (Sandbox Code Playgroud) 我的角度应用程序中有以下 http 拦截器,我想使用 Jasmine 对其进行单元测试。我用谷歌搜索了其中一些并尝试过,但它没有按预期工作。请找到下面的HttpInterceptorService.ts文件代码
export class HttpInterceptorService Implements HttpInterceptor {
counter = 0;
constructor(private loaderService: LoaderService) { }
intercept(req: HttpRequest<any>, next: HttpHandler) {
if (req.url !== '/getUsers') {
this.counter ++;
}
this.loaderService.setStatus(true);
return next.handle(req).pipe(
finalize(() => {
if (req.url !== 'getUsers') {
this.counter --;
}
if (this.counter === 0) {
this.loaderService.setStatus(false);
}
};
);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我目前尝试过的 HttpInterceptor.service.spec.ts 文件代码。我不确定如何测试其中的特定方法。
describe('HttpInterceptorService', () => {
let httpService: HttpService;
let httpMock: HttpTestingController;
let interceptor: HttpInterceptorService;
beforeEach(()=> {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [ …
Run Code Online (Sandbox Code Playgroud) 我有一个使用json数据生成的动态表单,我需要在提交时传递表单输入值.我打算将这些值作为formdata发送.我已经创建了提交函数,但我不知道如何在formdata中附加值,需要使用Axios传递post方法.我有新的反应任何人都可以告诉我如何做到这一点.以下是我的代码.
var DATA = {
"indexList": [{
"Label": "Name",
"Type": "text",
"Regex": "",
"Default_Val": "",
"Values": {
"Key": "",
"Value": ""
},
"Validtion Msg": "",
"Script": "",
"Mandatory": "Y",
"maxLength":"16",
"minLength":"7",
"format":"Alphanumeric",
"cssClassName": "form-control",
"Placeholder": ""
},
{
"Label": "Select Language",
"Type": "dropdown",
"Regex": "",
"Default_Val": "English",
"Values": [{
"Key": "option1",
"Value": "English"
},{
"Key": "option2",
"Value": "Spanish"
}],
"Validtion Msg": "",
"Script": "",
"Mandatory": "Y",
"maxLength":"",
"minLength":"",
"format":"",
"cssClassName": "form-control",
"Placeholder": ""
},
{
"Label": "Type",
"Type": "radio",
"Regex": …
Run Code Online (Sandbox Code Playgroud) 我有一个react
组件,我正在尝试div
使用margin-left
属性对齐以下内容。我收到一个console
错误:
意外的标记
指向margin left
属性中的连字符。谁能帮助解决这个问题?
<div id="loadingDiv" style = {{display:'block'}}>
<img src={Loading} style = {{width:150,height:150,margin-left:370}} />
</div>
Run Code Online (Sandbox Code Playgroud) 我在组件中有一个函数,它将调用带有 3 个参数的服务方法,并返回一个承诺。我想在角度中使用 karma jasmine 对函数进行单元测试。我在这里犯了什么错误吗?
组件代码
getHeaderData() {
return this.service.getList({
id: this.id,
name: this.name,
pageName: constants.PAGE_NAME
});
}
Run Code Online (Sandbox Code Playgroud)
服务代码
getList(param): Promise<any> {
const params = new HttpParams()
.set('cId',param.id)
.set('cName',param.name)
.set('cPageName',param.pageName);
return new Promise((resolve, reject) => {
try {
this.httpService.get('getHeader', {params}).subscribe(data => {
resolve(data);
});
}catch (err){
reject(err);
}
});
}
Run Code Online (Sandbox Code Playgroud)
规格文件代码
it('test getHeaderData()', async () => {
const serviceSpy: Service = TestBed.get(Service);
SpyOn(serviceSpy, 'getList').and.ReturnValue(Promise.resolve(constants.response));
expect(serviceSpy.getList).toHaveBeenCalled();
expect(component.getHeaderData()).toBe(constants.response);
});
Run Code Online (Sandbox Code Playgroud)
服务的实际返回值是一个对象数组,与我在常量文件中创建的对象数组相同作为响应,并且使用上面的代码我得到以下错误。我不确定这是正确的方法。
类型参数缺少 ' { then 中的以下属性: ExpectedRecursive<{< TResult1 等......
我的角度服务“config.service.ts”中有以下功能。我已经为此编写了一些单元测试用例,但无法覆盖订阅方法的错误部分。
getConfiguration(params){
return new Promise((resolve, reject) => {
try {
this.httpService.post('getConfig', params).subscribe{
data => {
resolve(data);
},
error => {
reject(error);
}
};
} catch(error) {
reject(error);
}
});
}
Run Code Online (Sandbox Code Playgroud)
以下是“config.service.spec.ts”代码。请让我知道如何覆盖相同的订阅错误部分。
it('coverage for getConfiguration()', () => {
const service: ConfigService = Testbed.get(ConfigService);
const param = {id: 10};
const response = {success: true};
const httpService: HttpService = Testbed.get(HttpService);
spyOn(httpService, 'post').and.returnValue(of(response));
service.getConfiguration(param).then(function() {});
expect(service.getConfiguration).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)
这里可观察误差部分没有被覆盖,无法达到覆盖范围。如果这里有什么不对的地方请指正。