Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 4 typescript修饰符在此处无效_Angular_Typescript - Fatal编程技术网

Angular 4 typescript修饰符在此处无效

Angular 4 typescript修饰符在此处无效,angular,typescript,Angular,Typescript,下面的代码 let isBrowserFactory2=function(@Inject(PLATFORM_ID) platformId: string){ return isPlatformBrowser(platformId);} 获取以下错误: Decorators are not valid here 这个呢 let isBrowserFactory=(@Inject(PLATFORM_ID) platformId: string):boolean=> isPlatformBr

下面的代码

let isBrowserFactory2=function(@Inject(PLATFORM_ID) platformId: string){ return isPlatformBrowser(platformId);}
获取以下错误:

Decorators are not valid here
这个呢

let isBrowserFactory=(@Inject(PLATFORM_ID) platformId: string):boolean=> isPlatformBrowser(platformId)
获取预期的
表达式
错误

  • 为什么我不能对函数使用typescript的参数装饰器
  • 如何使用
    @Inject()
    将内容注入工厂方法

在构造函数中使用
@Inject

isBrowserFactory: boolean;

constructor(@Inject(PLATFORM_ID) platformId string) { 
  this.isBrowserFactory = this.isPlatformBrowser(platformId);
}

这样您就可以声明
platformId
注入
platformId
。然后,您可以调用我相信返回布尔值的方法
isPlatformBrowser

基本正确,但支持
class
方法,而不仅仅是构造函数。不支持对象文字简明方法thouigh@AluanHaddad我以为你必须在类声明之前或者在
构造函数中使用decorators
@AluanHaddad哦!谢谢DGlad来帮忙。为了清晰起见,我稍微更新了我的评论。如果你编辑你的答案,我愿意投赞成票。@AluanHaddad谢谢!你确实帮了忙。我认为这种方式可能更清晰(更新的答案)。我明白OP想要做什么,我相信这样他就能做到。谢谢你的帮助!:D