没有使用@Injectable时,我的注入服务(LoggingService)如何工作?

时间:2019-07-15 02:31:20

标签: javascript angular

我试图将服务(LoggingService)注入到另一个服务(AccountService)中,所以accountsService肯定需要@Injectable才能探查它.....问题是我只是将其添加到构造函数中而不添加@Injectable,可以正常工作.......如果不使用@Injectable,我的应用如何运行?

import { LoggingService } from "./logging.service";

export class AccountsService {
    constructor(private loggingService: LoggingService) {}

    accounts = [{
            name: 'Master Account',
            status: 'active'
        },
        {
            name: 'Testaccount',
            status: 'inactive'
        },
        {
            name: 'Hidden Account',
            status: 'unknown'
        }
    ];

    addAccount(newAccount: {name: string, status: string}) {
        this.accounts.push(newAccount);
        this.loggingService.loggingStatusChange(newAccount.status);
    }

    updateStatus(id: number, status: string) {
        this.accounts[id].status = status;
        this.loggingService.loggingStatusChange(status);
    }
}

1 个答案:

答案 0 :(得分:0)

只要您不对应用程序可见的任何组件进行注入或使用 AccountsService 很好(我的意思是说可见的是@NgModule()类装饰器中是否“导入/声明/提供了”)。

如果您已经在某个地方使用过AccountsService 并且您的应用没有对您大吼大叫 error 。然后,它可能会在您应用程序中的@NgModule({})中提供。否则它将无法正常工作。