feat(upgrade): support downgrading multiple modules (#26217)
Currently, calling `downgradeModule()` more than once is not supported. If one wants to downgrade multiple Angular modules, they can create a "super-module" that imports all the rest and downgrade that. This commit adds support for downgrading multiple Angular modules. If multiple modules are downgraded, then one must explicitly specify the downgraded module that each downgraded component or injectable belongs to, when calling `downgradeComponent()` and `downgradeInjectable()` respectively. No modification is needed (i.e. there is no need to specify a module for downgraded components and injectables), if an app is not using `downgradeModule()` or if there is only one downgraded Angular module. Fixes #26062 PR Close #26217
This commit is contained in:

committed by
Kara Erickson

parent
6c5c97b2f9
commit
93837e9545
@ -46,8 +46,14 @@ interface Thenable<T> {
|
||||
*
|
||||
* @param info contains information about the Component that is being downgraded:
|
||||
*
|
||||
* * `component: Type<any>`: The type of the Component that will be downgraded
|
||||
* * `propagateDigest?: boolean`: Whether to perform {@link ChangeDetectorRef#detectChanges
|
||||
* - `component: Type<any>`: The type of the Component that will be downgraded
|
||||
* - `downgradedModule?: string`: The name of the downgraded module (if any) that the component
|
||||
* "belongs to", as returned by a call to `downgradeModule()`. It is the module, whose
|
||||
* corresponding Angular module will be bootstrapped, when the component needs to be instantiated.
|
||||
* <br />
|
||||
* (This option is only necessary when using `downgradeModule()` to downgrade more than one
|
||||
* Angular module.)
|
||||
* - `propagateDigest?: boolean`: Whether to perform {@link ChangeDetectorRef#detectChanges
|
||||
* change detection} on the component on every
|
||||
* [$digest](https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest). If set to `false`,
|
||||
* change detection will still be performed when any of the component's inputs changes.
|
||||
@ -59,7 +65,7 @@ interface Thenable<T> {
|
||||
* @publicApi
|
||||
*/
|
||||
export function downgradeComponent(info: {
|
||||
component: Type<any>; propagateDigest?: boolean;
|
||||
component: Type<any>; downgradedModule?: string; propagateDigest?: boolean;
|
||||
/** @deprecated since v4. This parameter is no longer used */
|
||||
inputs?: string[];
|
||||
/** @deprecated since v4. This parameter is no longer used */
|
||||
@ -96,7 +102,9 @@ export function downgradeComponent(info: {
|
||||
let ranAsync = false;
|
||||
|
||||
if (!parentInjector) {
|
||||
const lazyModuleRef = $injector.get(LAZY_MODULE_REF) as LazyModuleRef;
|
||||
const downgradedModule = info.downgradedModule || '';
|
||||
const lazyModuleRefKey = `${LAZY_MODULE_REF}${downgradedModule}`;
|
||||
const lazyModuleRef = $injector.get(lazyModuleRefKey) as LazyModuleRef;
|
||||
needsNgZone = lazyModuleRef.needsNgZone;
|
||||
parentInjector = lazyModuleRef.injector || lazyModuleRef.promise as Promise<Injector>;
|
||||
}
|
||||
|
@ -43,16 +43,35 @@ import {INJECTOR_KEY} from './constants';
|
||||
*
|
||||
* {@example upgrade/static/ts/full/module.ts region="example-app"}
|
||||
*
|
||||
* <div class="alert is-important">
|
||||
*
|
||||
* When using `downgradeModule()`, downgraded injectables will not be available until the Angular
|
||||
* module that provides them is instantiated. In order to be safe, you need to ensure that the
|
||||
* downgraded injectables are not used anywhere _outside_ the part of the app where it is
|
||||
* guaranteed that their module has been instantiated.
|
||||
*
|
||||
* For example, it is _OK_ to use a downgraded service in an upgraded component that is only used
|
||||
* from a downgraded Angular component provided by the same Angular module as the injectable, but
|
||||
* it is _not OK_ to use it in an AngularJS component that may be used independently of Angular or
|
||||
* use it in a downgraded Angular component from a different module.
|
||||
*
|
||||
* </div>
|
||||
*
|
||||
* @param token an `InjectionToken` that identifies a service provided from Angular.
|
||||
* @param downgradedModule the name of the downgraded module (if any) that the injectable
|
||||
* "belongs to", as returned by a call to `downgradeModule()`. It is the module, whose injector will
|
||||
* be used for instantiating the injectable.<br />
|
||||
* (This option is only necessary when using `downgradeModule()` to downgrade more than one Angular
|
||||
* module.)
|
||||
*
|
||||
* @returns a [factory function](https://docs.angularjs.org/guide/di) that can be
|
||||
* used to register the service on an AngularJS module.
|
||||
*
|
||||
* @publicApi
|
||||
*/
|
||||
export function downgradeInjectable(token: any): Function {
|
||||
export function downgradeInjectable(token: any, downgradedModule: string = ''): Function {
|
||||
const factory = function(i: Injector) { return i.get(token); };
|
||||
(factory as any)['$inject'] = [INJECTOR_KEY];
|
||||
(factory as any)['$inject'] = [`${INJECTOR_KEY}${downgradedModule}`];
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
Reference in New Issue
Block a user