小编Den*_*ger的帖子

Angular 9 如何使用 TypeScript 将 HTML 文件作为字符串导入?

我正在使用 Angular 9.1.3 和 Typescript 3.8.3。

我想将 html 文件作为原始字符串导入。

对于这种情况:

import * as template from "./projects.page.html";
console.log("Template: ", template);
Run Code Online (Sandbox Code Playgroud)

我收到错误:

ERROR in src/app/features/projects/projects.page.ts:41:27 - error TS2307: Cannot find module './projects.page.html'.

41 import * as template from "./projects.page.html";
Run Code Online (Sandbox Code Playgroud)

如果我添加html.d.ts文件:

declare module "*.html" {
    const content: string;
    export default content;
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

ERROR in ./src/app/features/projects/projects.page.html 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process …
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular

2
推荐指数
1
解决办法
4620
查看次数

标签 统计

angular ×1

javascript ×1

typescript ×1