Проблема заключается в том, что когда я обращаюсь к удаленному приложению из отдельной вкладки браузера, я вижу, что данные извлекаются с сервера, но когда я перехожу от хоста/оболочки к удаленному приложению, данные не определены. Вот фрагменты кода mijn. Я использую Angular 20 и автономные компоненты.
==============> Фрагменты кода в приложении Shell/Host: хост
==== app.routes.ts
Код: Выделить всё
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
Код: Выделить всё
@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;
}
}
Код: Выделить всё
export function provideConfigData(): EnvironmentProviders {
return makeEnvironmentProviders([
provideAppInitializer(() => {
const service = inject(MyConfigService);
return service.initConfig();
})
]);
};
Код: Выделить всё
export const appConfig: ApplicationConfig = {
providers: [
provideConfigData()
]
};
Код: Выделить всё
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;
}
Код: Выделить всё
export const remoteRoutes: Route[] = [
{
path: '',
component: RemoteEntry,
canActivate: [checkPermissions] // The gaurd is used here.
}
];
Подробнее здесь: https://stackoverflow.com/questions/797 ... ccesing-th
Мобильная версия