在Angular中导入数据 [英] Import Data in Angular

查看:55
本文介绍了在Angular中导入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Angular项目中使用csv文件,如下所示:

I tried using csv files in my Angular projects like this:

import * as data1 from "./data.csv";
import * as data2 from "./data2.csv";

它们与我试图与它们一起使用的.ts位于同一文件夹中.我收到以下错误:

They are located in the same folder as the .ts i am trying to use them with. I got following error:

找不到模块"data.csv"

Cannot find module 'data.csv'

我还尝试将它们移动到资产文件夹:

What I also tried was moving them to the assets folder:

import * as data1 from '/assets/data.csv';

但是错误仍然出现.我在做什么错了?

But the error still shows up. What am I doing wrong?

推荐答案

使用HttpClient读取csv文件内容,例如:

Read the csv file content using HttpClient, for example:

constructor (private http: HttpClient) {}

readCsvData () {
   this.http.get('path.to.csv')
   .subscribe(
        data => console.log(data),
        err => console.log(err)
    );
}

这篇关于在Angular中导入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆