在Angular 8中,如何从浏览器控制台访问注入的服务? [英] In Angular 8, how do I access an injected service from the browser console?

查看:90
本文介绍了在Angular 8中,如何从浏览器控制台访问注入的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular8。我想从我的浏览器控制台(Chrome开发工具)中访问注入的服务。我可以像这样从浏览器控制台访问注入器

I'm using Angular 8. I'd like to access an injected service from my browser console (Chrome dev tools). I can access the injector from my browser console like so

ng.probe(document.querySelector('app-root')).injector

我想在开发工具控制台中访问注入的服务,但是当我尝试这样做时

I would like to access an injected service in the dev tools console but when I try this

ng.probe($0).injector.get("AbcService")

我得到以下错误。我已验证我的服务名称正确。我还需要做些什么来从控制台访问我的服务?

I get the below error. I have verified the name of my service is correct. What else do I need to do to access my service from the console?

core.js:8991 Uncaught Error: StaticInjectorError(AppModule)[ActivationService]: 
  StaticInjectorError(Platform: core)[AbcService]: 
    NullInjectorError: No provider for AbcService!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:8895)
    at resolveToken (core.js:9140)
    at tryResolveToken (core.js:9084)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:8981)
    at resolveToken (core.js:9140)
    at tryResolveToken (core.js:9084)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:8981)
    at resolveNgModuleDep (core.js:21208)
    at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:21897)
    at Object.resolveDep (core.js:22268)

我要访问的服务是这样导入到我的组件中的:

The service I'm trying to access is imported into my component like so

import { AbcService } from '@myapp/services';


推荐答案

注入的服务将作为属性提供。 componentInstance 。如果将AbcService注入 AppComponent

The injected service will be available as a property on the componentInstance. If AbcService is injected into AppComponent

  export class AppComponent {  
    constructor(private abcService: AbcService) {}
  }

然后您可以在控制台中通过以下方式获取它:

then you can get it in the console with:

ng.probe(document.querySelector('app-root')).componentInstance.abcService

这篇关于在Angular 8中,如何从浏览器控制台访问注入的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆