我想在Angular 7中使用原始加载程序,将文本文件导入TypeScript源代码。我已经找到了大量有关该主题的信息,并花了几个小时试图使其正常工作,但我无法使其正常工作。
我首先创建一个新项目
ng new example --defaults --minimal
Run Code Online (Sandbox Code Playgroud)
我在/src/app/example.txt其中创建一个文本文件,并显示消息“ Hello World”
然后,我修改app.component.ts文件以导入此文本文件。
import {Component} from '@angular/core';
import str from 'example.txt';
alert(str);
@Component({
selector: 'app-root',
template: ``,
styles: []
})
export class AppComponent {
}
Run Code Online (Sandbox Code Playgroud)
我Cannot load module "example.txt"在WebStorm中看到错误,并且在运行时ng build收到以下错误。
src / app / app.component.ts(2,17)中的错误:错误TS2307:找不到模块“ example.txt”
因此,我用Google搜索了如何解决此问题,并找到了答案。
然后,我添加一个/src/typings.d.ts包含以下内容的文件,这可以修复WebStorm中的错误。
declare module "*.txt" {
const value: string;
export default value;
}
Run Code Online (Sandbox Code Playgroud)
我ng build再次运行,错误消息更改为无法找到该模块。
找不到模块:错误:无法在“ C:\ work \ temp \ example …