admin管理员组文章数量:1245052
I have an angular 18 project with standalone components and a boostraping:
main.ts:
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/appponent';
bootstrapApplication(AppComponent, appConfig)
.catch((err) => console.error(err));
appConfig.ts
import {
APP_INITIALIZER,
ApplicationConfig,
provideZoneChangeDetection,
} from '@angular/core';
import { provideRouter, Router } from '@angular/router';
import { routes } from './app.routes';
import { provideAnimations } from '@angular/platform-browser/animations';
import { provideToastr } from 'ngx-toastr';
import { onAppStart } from './app.initialiser';
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideAnimations(), // required animations providers
provideToastr(), // Toastr providers
{
provide: APP_INITIALIZER,
useValue: onAppStart,
multi: true,
deps: [Router],
},
],
};
app.initialiser.ts
import { Router } from '@angular/router';
export function onAppStart(router: Router): Promise<any> {
return new Promise((resolve, reject) => {
console.log(router);
resolve(true);
});
}
When running the app, router
in app.initialiser.ts is undefined and i can't see why. Every deps
I provide in app.config.ts is emtpy. What am I missing?
I have an angular 18 project with standalone components and a boostraping:
main.ts:
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/appponent';
bootstrapApplication(AppComponent, appConfig)
.catch((err) => console.error(err));
appConfig.ts
import {
APP_INITIALIZER,
ApplicationConfig,
provideZoneChangeDetection,
} from '@angular/core';
import { provideRouter, Router } from '@angular/router';
import { routes } from './app.routes';
import { provideAnimations } from '@angular/platform-browser/animations';
import { provideToastr } from 'ngx-toastr';
import { onAppStart } from './app.initialiser';
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideAnimations(), // required animations providers
provideToastr(), // Toastr providers
{
provide: APP_INITIALIZER,
useValue: onAppStart,
multi: true,
deps: [Router],
},
],
};
app.initialiser.ts
import { Router } from '@angular/router';
export function onAppStart(router: Router): Promise<any> {
return new Promise((resolve, reject) => {
console.log(router);
resolve(true);
});
}
When running the app, router
in app.initialiser.ts is undefined and i can't see why. Every deps
I provide in app.config.ts is emtpy. What am I missing?
- deps: [Router] would be passed to the factory, if you were using useFactory instead of useValue. – Andrei Commented Feb 15 at 22:21
1 Answer
Reset to default 1Do not use as param, instead inject the router in your function.
Here you are:
import { Router } from '@angular/router';
export function onAppStart(): Promise<any> {
const router = inject(Router);
return new Promise((resolve, reject) => {
console.log(router);
resolve(true);
});
}
And you can remove
deps: [Router],
本文标签: typescriptAngular 18 APPINITIALIZER does not add provided depsStack Overflow
版权声明:本文标题:typescript - Angular 18 APP_INITIALIZER does not add provided deps - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740237681a2246772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论