G
G
Ghoulll2020-01-29 15:57:21
Angular
Ghoulll, 2020-01-29 15:57:21

How to access chrome in Angular?

How in angular to access chrome.runtime... in a component?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-01-29
@Ghoulll

don't listen to bad advice :)
Install types for chrome https://www.npmjs.com/package/@types/chrome
and then create a token for the injector somewhere.

export const CHROME = new InjectionToken('CHROME');

in the providers of the app module for this token, pass the value
providers: [
  ...
  {
    provide: CHROME,
    useValue: window.chrome,
  }
  ...
]

In the constructor of your component or service
constructor(
  @Inject(CHROME) private chrome: chrome,
)

This is done in order to be able to change this dependency when necessary. For example, slip mocks for testing.
But in general, you can just call chrome or window.chrome if you don’t worry about substitutions and generally tracking dependencies. The main thing is to explain to the typescript that this is a valid name. Well, i.e. install types.
I can be mistaken in small things, tk. did not check, but described the correct way in general terms.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question