typescript 在打字稿中定义常量(离子应用程序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37866806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
defining constants in typescript (ionic application)
提问by runtimeZero
In my Ionic application I am defining constants as
在我的 Ionic 应用程序中,我将常量定义为
//constants.ts
export var CONSTANTS = {
API_ENDPOINT: 'http://localhost:3000/'
};
and importing it as
并将其导入为
import {CONSTANTS} from '../../services/constants'; //the path is correct
However I get the error CONSTANTS not defined in the file where I am importing.. what am i missing here ?
但是,我收到错误 CONSTANTS 未在我导入的文件中定义..我在这里遗漏了什么?
回答by Gus Arik
Here is how you should do it:
以下是您应该如何操作:
// constants.ts
export const API_ENDPOINT= 'http://localhost:3000/';
And importing it as:
并将其导入为:
import * as Constants from '../../services/constants';
And you can access it like this:
您可以像这样访问它:
Constants.API_ENDPOINT;
回答by Hardik Vaghani
For Ionic
对于离子
app.value('config', {
"constant1": "value1",
"constant2": "value2"
});
and access it with
并访问它
config.constant1
Do not forget to inject dependency
config
.
不要忘记注入依赖
config
。
For Nativescript
对于Nativescript
Define
定义
var configObject = {
testData: false,
apiUrl: "https://www.domain.com/api/v1/"
};
Use
利用
var config = require('../../utils/config');
and get value
并获得价值
config.apiUrl
Regards
问候
回答by basarat
However I get the error CONSTANTS not defined in the file where I am importing
但是,我收到错误 CONSTANTS 未在我导入的文件中定义
It works fine. Double check:
它工作正常。再确认一次:
- tsconfig.json : have
module
setting set. console.log(CONSTANTS)
in both files to see whats happening
- tsconfig.json :
module
设置了设置。 console.log(CONSTANTS)
在两个文件中查看发生了什么
回答by Subhasish Nath
In my App I created my constant file like bellow,in my main directory of my app inside filename - "envrionment.ts"
在我的应用程序中,我创建了如下所示的常量文件,在我的应用程序主目录中的文件名 - “envrionment.ts”
export const environment = {
site_url : 'http://localhost/wp',
quotes_url : '/wp-json/wp/v2/quotes',
jwt_url: '/wp-json/jwt-auth/v1/token'
}
Then I imported from inside my provider like bellow:
然后我从我的提供者内部导入,如下所示:
import {environment} from '../../envrionment';
Hope it helps, Thanks :)
希望有帮助,谢谢:)