(免責宣告:我是 React 和 Ionic 的新手)我希望將電容器的檔案系統 API 實作到 React 組件中,以便讀取包含簡單字串的 JSON 檔案。我的 JSON 檔案與 FileSystem.tsx 檔案位于同一檔案夾中。
import { Filesystem, Directory, Encoding } from '@capacitor/filesystem';
import { IonHeader, IonTitle, IonToolbar } from '@ionic/react';
import React from 'react';
import { IonPage,IonMenuButton,IonButton } from '@ionic/react';
class ReadFileContainer extends React.Component {
constructor(props: any) {
super(props)
this.state = {
}
}
readsecretFile = async () => {
const contents = await Filesystem.readFile({
path: "file:///Users/tbagggg/Downloads/FinalProject/src/pages/FileSystem/data.json",
});
console.log('contents:', contents);
};
render() {
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>
This is a test page
</IonTitle>
</IonToolbar>
</IonHeader>
<IonButton onClick={() => {this.readsecretFile()}}>
Read File
</IonButton>
</IonPage>
)
}
}
export default ReadFileContainer;
但是,當我呼叫 readsecretFile() 方法時,我在 google 的控制臺中遇到了這個錯誤。
. 奇怪的是,我可以 CMND 單擊 VS Code 中的路徑,它會將我定向到檔案“data.json”。有沒有其他人遇到過這個問題?
uj5u.com熱心網友回復:
Web 平臺上的檔案系統插件無法訪問計算機檔案系統,它使用 IndexedDB 模擬將檔案存盤在資料庫中的檔案系統。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/532963.html
上一篇:z-index屬性不適用于<ion-segment-button></ion-segment-button>內的元素
