Standalone & Tokens — Series 2

Preview — 3 of 10 questions

When does this callback run for a root-provided service?

javascript
@Injectable({ providedIn: 'root' })
export class SocketService {
  constructor() {
    inject(DestroyRef).onDestroy(() => this.socket.close());
  }
}
AWhen the injector that created it is destroyed — for a root service, that is application teardown, so in practice almost never during a session
BWhen the last component injecting it is destroyed
COn every navigation
DNever; DestroyRef is only available in components

What must you remember about this injector?

javascript
const injector = createEnvironmentInjector([FeatureStore], this.parentInjector);
AIt is destroyed automatically with the component that created it
BIt cannot provide services that are also provided in root
CYou own its lifetime: nothing destroys it for you, so injector.destroy() must be called — otherwise its services, and everything they retain, outlive whatever created it
DIt replaces the parent injector for the whole application

Both /admin and /reports list ReportCache in their route providers. How many instances exist?

javascript
// shared: root, or a parent route both children sit under
@Injectable({ providedIn: 'root' }) export class ReportCache {}
AOne, shared: the token is the same class
BOne per route injector — two instances, each destroyed when its own route is left, so state is not shared between the features
CTwo, but the second replaces the first
DOne per navigation, so a fourth visit means four instances

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.