activate method

Future<void> activate(
  1. ComponentFactory<Object> componentFactory,
  2. RouterState? oldState,
  3. RouterState newState
)

Activates and renders the component created by componentFactory.

If the component has already been activated and is reusable, a cached instance will be reused instead of creating a new one.

Implementation

Future<void> activate(
  ComponentFactory<Object> componentFactory,
  RouterState? oldState,
  RouterState newState,
) async {
  final activeComponent = _activeComponent;
  if (activeComponent != null) {
    final shouldReuse = await _shouldReuse(
      activeComponent.instance,
      oldState!,
      newState,
    );
    if (shouldReuse) {
      // If both routes render the same component, don't detach it from DOM.
      if (identical(_activeComponentFactory, componentFactory)) return;
      // Detach the active component, keeping it cached for reuse.
      for (var i = _viewContainerRef.length - 1; i >= 0; --i) {
        _viewContainerRef.detach(i);
      }
    } else {
      // Destroy the active component.
      _loadedComponents.remove(_activeComponentFactory);
      activeComponent.destroy();
      _viewContainerRef.clear();
    }
  }
  // Render the new component in the outlet.
  _activeComponentFactory = componentFactory;
  final component = prepare(componentFactory);
  _viewContainerRef.insert(component.hostView);
  // ignore: deprecated_member_use
  component.changeDetectorRef.detectChanges();
}