[英]React Native [TypeError: Cannot read property 'startsWith' of undefined]?
I'm getting this error when I put my Javascipt code to a Typescript project.将 Javascipt 代码放入 Typescript 项目时出现此错误。 In Javascript project its worked fine but when I put the code into a Typescript project I got the error.
在 Javascript 项目中,它运行良好,但是当我将代码放入 Typescript 项目时,我得到了错误。
Is there any mistake in coding?编码有错误吗?
Here's the code :这是代码:
const createPDF = async() => {
let Options = {
html: '<h1>test</h1>',
fileName: 'test',
directory: 'Download',
};
let file = await RNHTMLtoPDF.convert(Options)
Alert.alert('Alert','Successfully exported file:' + file.filePath,[
{text:'Cancel',style:'cancel'},
{text:'Open',onPress:()=>openFile(file.filepath)}
],{cancelable:true});
}
const openFile = (filepath: any) => {
const path = filepath;
FileViewer.open(path)
.then(()=>{
//success
console.log(`Successfully export PDF file`);
})
.catch(error => {
console.log(error);
});
}
LOG [TypeError: Cannot read property 'startsWith' of undefined]
finally I have solved the problem by adding string
type and :void
to filepath instead of any
( i dont know what this mean but it worked )最后我通过添加
string
类型和:void
到文件路径而不是any
解决了这个问题(我不知道这是什么意思,但它有效)
const openFile = (filepath: string):void => {
const path = filepath;
FileViewer.open(path, { showOpenWithDialog: true })
.then(()=>{
//success
console.log(`Successfully export PDF file`);
})
.catch(error => {
console.log(error);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.