feat(angular): add standalone providers, route strategy, component binding provider (#27997)

This commit is contained in:
Liam DeBeasi
2023-08-15 10:45:37 -05:00
committed by GitHub
parent 84212acce4
commit c65e08dcd1
6 changed files with 47 additions and 25 deletions

View File

@@ -35,11 +35,9 @@ export {
Platform,
AngularDelegate,
NavParams,
IonicRouteStrategy,
} from '@ionic/angular/common';
// ROUTER STRATEGY
export { IonicRouteStrategy } from './util/ionic-router-reuse-strategy';
// TYPES
export * from './types/ionic-lifecycle-hooks';

View File

@@ -1,13 +1,11 @@
import { CommonModule, DOCUMENT } from '@angular/common';
import { ModuleWithProviders, APP_INITIALIZER, NgModule, NgZone } from '@angular/core';
import { Router } from '@angular/router';
import {
ModalController,
PopoverController,
ConfigToken,
AngularDelegate,
INPUT_BINDER,
RoutedComponentInputBinder,
provideComponentInputBinding,
} from '@ionic/angular/common';
import { IonicConfig } from '@ionic/core';
@@ -76,23 +74,8 @@ export class IonicModule {
multi: true,
deps: [ConfigToken, DOCUMENT, NgZone],
},
{
provide: INPUT_BINDER,
useFactory: componentInputBindingFactory,
deps: [Router],
},
provideComponentInputBinding(),
],
};
}
}
function componentInputBindingFactory(router?: Router) {
/**
* We cast the router to any here, since the componentInputBindingEnabled
* property is not available until Angular v16.
*/
if ((router as any)?.componentInputBindingEnabled) {
return new RoutedComponentInputBinder();
}
return null;
}

View File

@@ -1,64 +0,0 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router';
/**
* Provides a way to customize when activated routes get reused.
*/
export class IonicRouteStrategy implements RouteReuseStrategy {
/**
* Whether the given route should detach for later reuse.
*/
shouldDetach(_route: ActivatedRouteSnapshot): boolean {
return false;
}
/**
* Returns `false`, meaning the route (and its subtree) is never reattached
*/
shouldAttach(_route: ActivatedRouteSnapshot): boolean {
return false;
}
/**
* A no-op; the route is never stored since this strategy never detaches routes for later re-use.
*/
store(_route: ActivatedRouteSnapshot, _detachedTree: DetachedRouteHandle): void {
return;
}
/**
* Returns `null` because this strategy does not store routes for later re-use.
*/
retrieve(_route: ActivatedRouteSnapshot): DetachedRouteHandle | null {
return null;
}
/**
* Determines if a route should be reused.
* This strategy returns `true` when the future route config and
* current route config are identical and all route parameters are identical.
*/
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
if (future.routeConfig !== curr.routeConfig) {
return false;
}
// checking router params
const futureParams = future.params;
const currentParams = curr.params;
const keysA = Object.keys(futureParams);
const keysB = Object.keys(currentParams);
if (keysA.length !== keysB.length) {
return false;
}
// Test for A's keys different from B.
for (const key of keysA) {
if (currentParams[key] !== futureParams[key]) {
return false;
}
}
return true;
}
}