简体   繁体   English

Angular4 / 5依赖项注入文档不起作用

[英]Angular4/5 dependency injection docs not working

https://angular.io/guide/architecture#services https://angular.io/guide/architecture#services

I'm following the docs on angular.io to inject dependencies like services, etc. I did everything they said and when I try to run it, the console keeps telling me: 我正在关注angular.io上的文档以注入依赖项,例如服务等。我做了他们所说的一切,当我尝试运行它时,控制台不断告诉我:

Uncaught ReferenceError: LedgerService is not defined

I am doing nothing crazy except creating a simple component with a service where both constructors have console.log commands (constructors in both the component and service). 除了使用带有两个构造函数都具有console.log命令(组件和服务中的构造函数)的服务创建一个简单组件之外,我没有做任何疯狂的事情。 I've done everything Angular says to do in their 2 paragraphs that details this feature of Angular. 我已经完成了Angular在其2段中所说要做的所有事情,其中​​详细介绍了Angular的这一功能。

The component itself is being injected into the main app module (with the service being injected into the component) and both the component and service were created with the Angular CLI. 组件本身被注入到主应用程序模块中(服务被注入到组件中),并且组件和服务都是使用Angular CLI创建的。 So there isn't much I've even done at all minus trying to inject the service. 因此,我什至没有做太多事情去尝试注入服务。 So I'm not sure where it is going wrong but it is definitely not working and just shows a blank page (when it previously had basic content by default). 因此,我不确定哪里出了问题,但绝对不能正常工作,只显示一个空白页(默认情况下它以前具有基本内容)。

I created both units, tried to specify providers in both the app.module and the component.ts file and neither works and yields the same error--when Angular claims either could work. 我创建了这两个单元,试图在app.module和component.ts文件中都指定提供程序,但它们都不起作用并且产生相同的错误-当Angular声明可以起作用时。 I've also specified it as a private service within the constructor of the component.ts file. 我也已在component.ts文件的构造函数中将其指定为私有服务。

Everything I've seen relating to this is always for Angular 1 or 2. Neither of which are even remotely similar to Angular 4/5. 我所见到的与此相关的所有内容始终都是针对Angular 1或2。它们都与Angular 4/5几乎没有相似之处。

If you really want to see this code, fine but it's literally just framework and nothing else: 如果您真的想看这段代码,可以,但是实际上只是框架而已:

bookkeeper.component.ts : bookkeeper.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-bookkeeper',
  templateUrl: './bookkeeper.component.html',
  styleUrls: ['./bookkeeper.component.css'],
  providers: [LedgerServiceService]
})
export class BookkeeperComponent implements OnInit {

    constructor(private service: LedgerServiceService) { }

    ngOnInit() {
        console.log("Ledger component works!");
    }

}

app.module.ts : app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { InterfaceComponent } from './interface/interface.component';
import { BookkeeperComponent } from './bookkeeper/bookkeeper.component';


@NgModule({
  declarations: [
    AppComponent,
    InterfaceComponent,
    BookkeeperComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [
    LedgerServiceService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

ledger-service.service.ts : ledger-service.service.ts

import { Injectable } from '@angular/core';

@Injectable()
export class LedgerServiceService {

    constructor() { 
        console.log("wtf");
    }

}

LedgerService is actually called LedgerServiceService because I initially created LedgerService manually and then tried to use the AngularCLI to generate a service and named it LedgerService and it created a service called LedgerServiceService. LedgerService实际上被称为LedgerServiceService,因为我最初是手动创建LedgerService,然后尝试使用AngularCLI生成服务并将其命名为LedgerService,然后创建了一个名为LedgerServiceService的服务。 Naming is not what is wrong. 命名没有错。 I only initially called it simply LedgerService because I figured it would be confusing. 我最初只是将其简单地称为LedgerService,因为我认为它会令人困惑。

Your examples are missing the import. 您的示例缺少导入。

Anywhere we use a custom type, we also need to import that type. 在使用自定义类型的任何地方,我们还需要导入该类型。

For that reason, in both the module and component you will need to add: 因此,在模块和组件中都需要添加:

import { LedgerServiceService } from './your-path-here' 

You can see this in the examples they give on https://angular.io/guide/dependency-injection 您可以在他们在https://angular.io/guide/dependency-injection上给出的示例中看到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM