Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.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

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
Javascript 角度提供者的奇怪问题_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 角度提供者的奇怪问题

Javascript 角度提供者的奇怪问题,javascript,angular,typescript,Javascript,Angular,Typescript,我刚开始学习Angular2,我有我的app.module.ts ... ], providers: [ {provide:'content',useClass:ContentService} ], .... 在这里,我为我的服务类定义了文字 我试图像这样在我的组件类中注入“内容” export class DashboardComponent implements OnInit { constructor( @Inject('content') privat

我刚开始学习Angular2,我有我的app.module.ts

    ...
    ],
providers: [
    {provide:'content',useClass:ContentService}

],
....
在这里,我为我的服务类定义了文字

我试图像这样在我的组件类中注入“内容”

    export class DashboardComponent implements OnInit {
constructor( @Inject('content') private contentService ) { }
我收到一个错误,说“没有ContentService的提供程序!”

但当我将app.module.ts修改为

    ...
    ],
providers: [
    {provide:'content',useClass:ContentService},ContentService

],
....

它工作得很好。就我个人而言,我无法理解这个问题

你是在嘲笑你的服务吗?如果不是,为什么
{provide:'content',使用class:ContentService}
,而不仅仅是
ContentService
?正如您所指出的,您的第二种语法是有效的,因为只提供ContentService是一种方法。这个表达式没有使用
{provide:'content',useClass:ContentService}
我想。另外,如果您的服务已经有了decorator
@Injectable()
,那么您不需要将它注入组件的构造函数中,因为我是Angular 2的新手,所以仍然有一些东西我不完全理解。我试图做的是使用内容作为ContentService的占位符。我认为这可能有助于避免将服务类手动导入到我的组件中。我将研究你关于@injectable注释的建议。好的,你应该看看服务教程:你是在试图模仿你的服务吗?如果不是,为什么
{provide:'content',使用class:ContentService}
,而不仅仅是
ContentService
?正如您所指出的,您的第二种语法是有效的,因为只提供ContentService是一种方法。这个表达式没有使用
{provide:'content',useClass:ContentService}
我想。另外,如果您的服务已经有了decorator
@Injectable()
,那么您不需要将它注入组件的构造函数中,因为我是Angular 2的新手,所以仍然有一些东西我不完全理解。我试图做的是使用内容作为ContentService的占位符。我认为这可能有助于避免将服务类手动导入到我的组件中。我将研究您关于@injectable注释的建议。好的,您应该看看服务教程: