gpt4 book ai didi

typescript - 如何在 Type Script 中导入 CSV 文件和文本文件

转载 作者:行者123 更新时间:2023-12-05 07:29:54 29 4
gpt4 key购买 nike


我只需要帮助我是 angular-6 的新手。我的问题是如何在 typescript 中导入 csv 或文本文件,如下例所示,我导入文件的 json
1) 例子:-我将 json 文件存储在 app 文件夹下的文件夹中,例如文件名登录.json

{
"email": "nabeel.amd9@gmail.com",
"password": "Balc"
}

并像 json.d.ts

一样声明模块

This will allow you to load .json files from disk

declare module "*.json" {
const value: any;
export default value;
}

并将此文件 json 文件导入为

//# json import
import jsonData from '../common/json/jsondata.json';
export const JsonResouce = <IJsondata> jsonData;

export interface IJsondata {
email: string;
password: string;
}

上面的示例对我有用,而我使用相同的过程来解析 csv 和文本文件,但它是 sow 错误

ERROR in ./src/app/common/text/info.txt Module parse failed:Unexpected token (1:9) You may need an appropriate loader to handlethis file type. | Pakistan zindabad
ERROR in ./src/app/common/csv/latlong.csv Module parse failed: Unexpected token(2:4) You may need an appropriate loader to handle this file type. |name, lat, long | Pir Mahal, Toba Tek Singh, Punjab, Pakistan
30.767956 72.437813

我的 csv 和文本文件代码是:-

// # csv file
import csvData from './latlong.csv';
//# text import
import textData from '../common/text/info.txt';
//# json import
import jsonData from '../common/json/jsondata.json';


export const TextResource = textData; // not-work
export const JsonResource = <IJsondata> jsonData; // work
export const CsvResource = <ILatLong[]> csvData; // not-work

export interface IJsondata {
email: string;
password: string;
}

export interface ILatLong {
name: string;
lat: any;
long: any;
}

declare module "*.txt" {
const content: string;
export default content;
}

declare module '*.csv' {
const value: any;
export default value;
}

是否可以通过这种方式导入 csv|txt 文件?

最佳答案

对于某些文件,您可能需要在 bundler (Webpack) 中配置一个特殊的加载器。JSON 文件可能开箱即用。检查此 CSV 加载器:https://www.npmjs.com/package/csv-loader

如果声明文件无法正确附加类型,您可以尝试 require:

const textData = require('../common/text/jsondata.json') as IJsondata;

关于typescript - 如何在 Type Script 中导入 CSV 文件和文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52532039/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com