簡體   English   中英

導入所有組件Angular2

[英]Import all components Angular2

我有很多組件,導入部分看起來很臃腫:

import {comp1} from './components/comp1/comp1';
import {comp2} from './components/comp2/comp2';
....

有沒有辦法寫通用導入? 就像是

import * from './components/';

您可以使用帶* import並使用as關鍵字來創建要在組件中使用的變量。 例如:

import * as jwt from 'angular2-jwt/angular2-jwt';

然后在您的組件中將其引用如下:

// ...
  console.log(jwt.AuthConfig);

這是index.ts文件的目的。

我通常在該文件夾中寫一個index.ts文件夾,並將您想要的所有內容放入索引文件中。

例如:

path/component/index.ts

export * from './child component 1/index';
export * from './child component 2/index';
export * from './child component 3/index';
export * from './child component 4/index';
export * from './child component 5/index';
...

some/component/*.component.ts

從'path / component / index'導入*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM