2016-10-04 34 views
3

我需要在我的angular2应用程序中设置应用程序常量,以便可以在整个应用程序中使用这些应用程序,在角度1中,我使用angular.constant和angular.value进行了操作。我在这里寻找最好的方法。angular2中的应用常量?

+0

的可能的复制[定义在角2全局常量(HTTP ://stackoverflow.com/questions/34986922/define-global-constants-in-angular-2) – estus

+0

请参阅http://stackoverflow.com/a/38718878/2460760 – Nodarii

回答

5

文件constants.ts

export const XXX = 'yyy'; 

otherfile.ts

import {XXX} from 'constants.ts' 
2

如果你想,把它们封装在一个变量:

export const BaseConstants = Object.freeze({ 
    BASE_API_URL: 'http://example.com/' 
});