我想在App Settings我的应用程序中添加一个部分,其中包含一些consts和预定义的值.
我已经阅读了这个使用的答案OpaqueToken但它在Angular中已被弃用.此文章解释了差异,但它没有提供一个完整的例子,我的尝试均告失败.
这是我尝试过的(我不知道这是不是正确的方法):
//ServiceAppSettings.ts
import {InjectionToken, OpaqueToken} from "@angular/core";
const CONFIG = {
apiUrl: 'http://my.api.com',
theme: 'suicid-squad',
title: 'My awesome app'
};
const FEATURE_ENABLED = true;
const API_URL = new InjectionToken<string>('apiUrl');
Run Code Online (Sandbox Code Playgroud)
这是我想要使用这些consts的组件:
//MainPage.ts
import {...} from '@angular/core'
import {ServiceTest} from "./ServiceTest"
@Component({
selector: 'my-app',
template: `
<span>Hi</span>
` , providers: [
{
provide: ServiceTest,
useFactory: ( apiUrl) => {
// create data service
},
deps: [
new Inject(API_URL)
]
} …Run Code Online (Sandbox Code Playgroud) 我遇到了'不透明的标记'作为在Angular 2中实现全局常量的解决方案,例如:在Angular 2中定义全局常量
尽管阅读了文档,我似乎无法理解这一点.
使用OpaqueToken比使用字符串作为标记更可取,因为多个提供程序使用与两个不同标记相同的字符串可能导致冲突.
什么?什么是开始的Angular2令牌?所有我得到的谷歌都是JSON网络代币(他们在auth等中的角色等)的答案,据我所知,但显然没有任何关联.
什么是不透明的令牌?它是干什么用的?
PS更多关于不透明标记的文档用于提供常量.然而,他们并没有帮助我.
在我的服务中,我正在使用一个http帖子.我想将URL设置为常量.
return this.http.get(this.config.API_URL+'users', options).map(res=>res.json());
Run Code Online (Sandbox Code Playgroud)
我试过一项服务:
import {Injectable} from '@angular/core';
@Injectable()
export class AppService {
API_URL :String;
constructor() {
this.API_URL = 'some url';
}
}
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以在Angular4中创建一个常量值吗?
在Angular(类型脚本)中有许多配置文件.保存全球环境的哪一个是正确的?
例如,当我从本地调用API然后我rootUrl就是,localhost:42000但是当我开启生产时它应该是http:www.someting.com.
我想将它保存rootUrl在一些全球的地方,所以如果我打开生产,那么我只需要改变它rootUrl.
请建议我应该在哪里保存这些全局设置,与Asp.Net中的web.config相同.
如何在接口中将常量放在打字稿中.就像在java中一样:
interface OlympicMedal {
static final String GOLD = "Gold";
static final String SILVER = "Silver";
static final String BRONZE = "Bronze";
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个ionic2项目,我需要创建一个新的自定义JSON配置文件.我发现了一些教程来创建一个并通过http.get访问它,但我认为通过get请求调用它是很奇怪的.我希望它在根文件夹(所有配置JSON都是)中,我直接打开/读取文件.
我不知道是否可能,甚至推荐?这就是为什么我在这里张贴有一些意见和解决方案:)
谢谢
我有两个服务。login.service.ts和environment-specific.service.ts。
在login.service.ts我需要初始化一个 Json 对象,environment-specific.service.ts从中传递几个链接。
用例是如何在没有 的情况下使其在 servcie 中工作ngOnInit(){},因为 ngOnInit()根据 Angular2 Lifecycle Hook 不能在 servcie 中使用。
在这里你可以找到我必须在里面初始化的部分login.service.ts- 当然没有ngOnInit()方法:
...
import { EnvSpecific } from '../models/env-specific';
import { ActivatedRoute } from "@angular/router";
@Injectable()
export class LoginService {
link1: string;
link2: string;
link3: string;
constructor(private http: Http, private fbuilder:FormBuilder, private route: ActivatedRoute) { }
ngOnInit(){
this.route.data
.subscribe((data: { envSpecific: EnvSpecific }) => {
this.link1 = data.envSpecific.link1;
this.link2 = …Run Code Online (Sandbox Code Playgroud) ================================================== ==============
编辑:解决方案升级到2.0 Final后 - 在RC5升级后将服务器参数传递给ngModule
================================================== ================
有没有将服务器参数传递给Angular 2应用程序的方法?
即我想使用MVC对象"HttpContext.User.Identity.Name",并在我的角度2应用程序中的任何地方注入它.
在角度1中,这可以使用ng".constant"并将.Net对象序列化为index.cshtml中的JSON.
看起来有一种方法可以传递params,但这不适用于.Net代码. 在Angular 2中定义全局常量
//HTML - Bootstrapping
<script>
System.import('app/main').then(null, console.error.bind(console));
//I WOULD LIKE TO PASS SOME PARAMS TO APP/MAIN HERE
</script>
Run Code Online (Sandbox Code Playgroud)
最终解决方案:(非常感谢蒂埃里)
index.cshtml:
<script>
System.import('app/main').then(
module =>
module.main(
{
name: '@User.Identity.Name',
isAuthenticated: User.Identity.IsAuthenticated.ToString().ToLowerInvariant(),
}
),
console.error.bind(console)
);
</script>
Run Code Online (Sandbox Code Playgroud)
main.ts:
...
import {provide} from '@angular/core';
...
export function main(params) {
bootstrap(AppComponent,
[
provide('Name', { useValue: params.name }),
provide('IsAuthenticated', { useValue: params.isAuthenticated }),
ROUTER_PROVIDERS,
HTTP_PROVIDERS,
LoggerService,
AuthenticationService
]);
}
Run Code Online (Sandbox Code Playgroud)
用法:
import …Run Code Online (Sandbox Code Playgroud) 我需要在我的angular2应用程序中设置应用程序常量,以便在整个应用程序中可以使用angularIconantant和angular.value.我在这里寻找最好的方式.
我想为我的 API url 定义全局常量变量,我想在很多地方访问它。
它将定义什么类型的文件,以及如何访问它们。