javascript - Angular - 在当前 ts 文件中包含额外的 ts 文件

标签 javascript angular typescript include export

我是 Angular 的新手,正在查看文档,但我想我也应该在这里问我的问题。

我正在导出一个越来越大的类,因此我想将其分成两个文件,并将 d1、d2、d3 等存储在一个单独的文件中,然后将它们导入到 d1 所在的位置。

  export class HomeComponent implements OnInit {

  name: string;
  AppModule;
  d1: string = " lengthy string of text";
  d2: string = " another lengthy string of text";
  d3: string = " another lengthy string of text";
  // goes on for many lines

  constructor() { }

  ngOnInit() {
  this.name = "Home";
  } ...

实现这一目标的最佳方法是什么?

最佳答案

对于原始问题,最简单且与 Angular 无关的方法是只创建一个像 string.ts 这样的文件:

export const d1 : string = "Hello World, this is a long string.";

并通过导入字符串

 import {d1} from './string';

示例:https://stackblitz.com/edit/angular-ayepho

关于javascript - Angular - 在当前 ts 文件中包含额外的 ts 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48429627/

相关文章:

angular - NullInjectorError : StaticInjectorError(DynamicTestModule)[ErrorHandlerInterceptor -> Router]:

angular - 在 typescript 中传播语法

javascript - 我应该为项目使用angular-cli吗

javascript - 如何获取时间在 Mongodb 中检索数据

javascript - 如何在不更改日期值的情况下更改时区

Angular 4 : Bootstrap's collapse does not work using data-target attribute

typescript - 用于检查未定义和 NULL 类属性的 TSLint 规则

javascript - 我应该如何强制 TypeScript 理解这个数组中不会有任何空值?

javascript - 使用 Jest 和 @std/esm 模拟 Node 模块

javascript - 使用 javascript 选择单选按钮。