简体   繁体   English

提供Angular 7服务:'root'

[英]Angular 7 Service providedIn: 'root'

I'm very new to angular development so please forgive me if this is a very basic question 我对角度开发很新,所以如果这是一个非常基本的问题,请原谅我

But I have a cart service which I have at the moment simply has a simple console log function 但我有一个购物车服务,我目前只有一个简单的控制台日志功能

 import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class CartService { constructor( ) {} public addItem() { console.log('Hello'); } } 

and basically I cannot figure out how to use this service within a NG Module that I have installed , I have successfully used it in other components via the constructor but the ngmodule doesn't have this? 基本上我无法弄清楚如何在我安装的NG模块中使用此服务,我已成功通过构造函数在其他组件中使用它,但ngmodule没有这个?

I get the fact that it's a singleton at the app-module level by using the providedIn: 'root' tag added in angular 6 通过使用在角度6中添加的providedIn:'root'标签,我得到了它在app-module级别的单例

but just can't figure out how to call cartService.addItem()? 但只是无法弄清楚如何调用cartService.addItem()?

Thanks if anyone can help! 谢谢,如果有人可以帮助!

You can use Dependency Injection like this to call the service 您可以像这样使用依赖注入来调用服务

export class AppComponent {
  constructor(private cartService: CartService) {

  }

  doSomething() {
     this.cartService.addItem();
  }
}

Below is the sample code of how to use service in component: 以下是如何在组件中使用服务的示例代码:

Component.ts Component.ts

import { Component, OnInit} from '@angular/core';
import { yourSeerviceName } from "PATH_TO_SERVICE";
@Component({
    selector: 'app-dummy',
    styleUrls: ['dummy.component.sass'],
    templateUrl: 'dummy.component.html'
})


export class yourComponent implements OnInit {
    // you can provide any name for variable it's upto you
    constructor(private dummyService:yourSeerviceName) {

    }

    //access the method in you'r service by performing below action.
    this.dummyService.yourMethod();
}

If you created a new module, you need to introduce the service to your module, by going to the .module.ts file and adding your service to the providers array. 如果您创建了一个新模块,则需要将该服务引入模块,方法是转到.module.ts文件并将服务添加到providers数组中。 It will be something like: 它将是这样的:

providers: [
    CartService
  ],

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

相关问题 在“root”中提供的 angular 服务未定义 - providedin 'root' angular service is undefined Angular 提供的服务:'root' 为空 object - Angular service with providedIn: 'root' is an empty object 角度服务装饰器提供了对延迟加载的根效果 - Angular service decorator providedIn root effect on lazy loading Angular service providedIn:从库导入的根会引发 NullInjectorError - Angular service providedIn: root imported from library throws NullInjectorError Angular 7/8:Treeshakable 提供程序(提供在:'root') - Angular 7/8: Treeshakable providers (providedIn: 'root') Angular 提供了根和根模式 - Angular providedIn root and forRoot pattern providedIn 属性在 Angular 服务不工作 - providedIn Property In Angular Service Is not working Angular 服务在 VS forRoot 中提供 - Angular service providedIn VS forRoot 如何使用 Angular 6/7 将服务注入另一个服务? (providedIn: 'root') 服务总是未定义的 - How to inject service into an another service with Angular 6/7 ? (providedIn: 'root') Service is always undefined 尽管使用了 @Injectable({providedIn: 'root' }) 装饰器,Angular 服务在注入到组件中时不会充当单例 - Angular Service not acting as singleton when injected in components despite using @Injectable({providedIn: 'root' }) decorator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM