Jak wstawić dokument w serwisie?
Mam aplikację Angular 2. Aby wyśmiać obiekt Document
w testach, chciałbym wprowadzić go do serwisu w stylu:
import { Document } from '??'
@Injectable()
export class MyService {
constructor(document: Document) {}
}
Usługa Title
kątowa wykorzystuje metodę wewnętrzną getDOM()
.
Czy jest jakiś prosty sposób na wstrzyknięcie Document
do serwisu? Ponadto, jak powinienem odwołać się do niego w tablicy providers
?
4 answers
Jest to wspierane przez Angular przez jakiś czas.
Możesz użyć DOCUMENT
stała dostarczana przez pakiet @angular/common
.
Opis stałej DOCUMENT
(zaczerpnięty z dokumentacji API ):
Token DI reprezentujący główny kontekst renderowania. W przeglądarce jest to dokument DOM.
Przykład jest pokazany poniżej:
My-service.obsługa.ts:
import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Injectable()
export class MyService {
constructor(@Inject(DOCUMENT) private document: Document) {}
}
My-service.obsługa.spec.ts
import { provide } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { MyService } from './my-service';
class MockDocument {}
describe('MyService', () => {
beforeEachProviders(() => ([
provide(DOCUMENT, { useClass: MockDocument }),
MyService
]));
...
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-01-13 18:23:07
Nie jestem w stanie skomentować bezpośrednio pytania adamdport (jeszcze 50 punktów rep), ale tutaj jest tak, jak stwierdzono w angular docs.
Blockquote @GünterZöchbauer wygląda na to, że dokument jest przestarzały. Wiesz, jak to zrobić, gdy zniknie? Na przykład, jak ustawić ikonę favicon dynamicznie?
Zamiast importować z przeglądarki tak:
import { DOCUMENT } from '@angular/platform-browser';
Importuj go z angular common:
import {DOCUMENT} from '@angular/common';
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-02 11:39:07
Oprócz odpowiedzi @ Güntera Zöchbauera.
Angular definition DOCUMENT as an InjectionToken
export const DOCUMENT = new InjectionToken<Document>('DocumentToken');
I wstrzyknąć go z dokumentem w przeglądarce.ts
{provide: DOCUMENT, useFactory: _document, deps: []}
export function _document(): any {
return document;
}
Dlatego, gdy go używamy, wystarczy wstrzyknąć @Inject(DOCUMENT)
Lub użyj tokena bezpośrednio w deps:[DOCUMENT]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-02 17:44:13
import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Injectable()
export class MyService {
constructor(@Inject(DOCUMENT) private document) {}
}
To ": Document" jest przyczyną problemu.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-16 20:34:16