[英]Unable to access function definition defined in a .ts file from another .ts file
I would appreciate your 2 cents to resolve the issue of calling a function defined in another file. 我很感激您的2美分来解决调用另一个文件中定义的函数的问题。 I read the docs for Typescript which suggested to either setup an interface or a module and then reference it but I received the same error as below. 我阅读了Typescript的文档,建议设置一个接口或模块,然后引用它但我收到的错误与下面相同。
Error 错误
ReferenceError: viewReport is not defined
File Structure 文件结构
sample
- sample.component.ts
- sample.component.html
- sample.component.css
pdf
viewReport.ts
sample.component.ts sample.component.ts
import './pdf/viewReport';
declare var pdfMake: any;
declare var viewReport: any;
export class SampleComponent {
addr = {
"streetaddress": '',
"cityName": '',
"stateCode": '',
"zipCode": ''
};
constructor() {}
data = {
header: {
"address": {
"street": this.addr.streetAddress,
"city": this.addr.cityName,
"state": this.addr.stateCode,
"zip": this.addr.zipCode
}
}
}
downloadPdf(){
let pdf = pdfMake;
pdf.createPdf(viewReport(this.data)).download('test.pdf');
}
}
viewReport.ts viewReport.ts
function viewReport( data ) {
let docDefinition = {
content: [{
text: 'Address: ' + data.header.address.street
}]
}
console.log( 'data:: ', data );
return docDefinition;
}
I was able to resolve the issue by making the following changes to the code: 我能够通过对代码进行以下更改来解决此问题:
viewReport.ts viewReport.ts
add keyword ' default ' when exporting 导出时添加关键字' default '
export default function viewReport( data ) {
sample.component.ts sample.component.ts
replace 更换
import './pdf/viewReport';
declare var viewReport: any;
with 同
import viewReport from './pdf/viewReport';
References 参考
@n00dl3
@vesse
https://www.typescriptlang.org/docs/handbook/modules.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.