Das Problem besteht darin, dass ich sehe, dass die Daten vom Server abgerufen werden, wenn ich über einen separaten Browser-Tab auf die Remote-App zugreife, aber wenn ich vom Host/Shell zur Remote-App navigiere, sind die Daten undefiniert. Hier sind meine Codeausschnitte. Ich verwende Angular 20 und eigenständige Komponenten.
Codeausschnitte in der Shell/Host-App: host
app.routes.ts
Code: Select all
export const appRoutes: Route[] = [
{
// this navigates to the remote app when a user clicks on the 'marketing' link from the host ui
path: 'marketing',
loadChildren: () => import('marketing/Routes').then((m) => m!.remoteRoutes),
}
]
MyConfigService
Code: Select all
@Injectable({
providedIn: "root"
})
export class MyConfigService {
dataFromServer: string = '';
public initConfig() {
// In this class i fetch data from the server and save it in the local variable as well as in localstorage
dataFromServer = resultFromServer;
localstorate.setItem("data", resultFromServer)
// I also print the results to the console with console.log();
}
get DataFromServer(): string {
return this.dataFromServer;
}
}
Code: Select all
export function provideConfigData(): EnvironmentProviders {
return makeEnvironmentProviders([
provideAppInitializer(() => {
const service = inject(MyConfigService);
return service.initConfig();
})
]);
};
Code: Select all
export const appConfig: ApplicationConfig = {
providers: [
provideConfigData()
]
};
Code: Select all
export const checkPermissions: CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
const service = inject(MyConfigService);
const dataFromServer = service.DataFromServer
// When I access the remote app from its own browser tab with localhost:4201, I see "dataFromServer" and the data from localstorage been logged to the console but when I click I link from the host app to navigate to the remote app the console prints undefined values
console.log('=====1=======:', dataFromServer);
console.log('=====2=======:', localstorage.getItem('data'));
if (dataFromServer) {
return true;
}
return false;
}
Code: Select all
export const remoteRoutes: Route[] = [
{
path: '',
component: RemoteEntry,
canActivate: [checkPermissions] // The guard is used here.
}
];
Mobile version