|
| 1 | +# @angular/app-shell |
| 2 | + |
| 3 | +This is a simple library to make it easy to show/hide certain elements |
| 4 | +in a pre-rendered component. See the [App Shell Guide](../guides/app-shell.md) |
| 5 | +for more information about how it's used to build App Shell components. |
| 6 | + |
| 7 | +# Install |
| 8 | + |
| 9 | +`$ npm install @angular/app-shell` |
| 10 | + |
| 11 | +# Usage |
| 12 | + |
| 13 | +In the providers setup for the prerender environment, import and add |
| 14 | +the `APP_SHELL_BUILD_PROVIDERS`. For now, this provides a single `boolean` |
| 15 | +provider, `IS_PRERENDER`, which can be injected anywhere in the application |
| 16 | +to change JavaScript logic depending on the execution context. |
| 17 | + |
| 18 | +`main-app-shell.ts` (or other entry point): |
| 19 | +```typescript |
| 20 | +// In pre-render context, such as main-app-shell.ts |
| 21 | +import { APP_SHELL_BUILD_PROVIDERS } from '@angular/app-shell'; |
| 22 | +// ... |
| 23 | +providers: [ |
| 24 | + APP_SHELL_BUILD_PROVIDERS, |
| 25 | + //... |
| 26 | +] |
| 27 | +``` |
| 28 | + |
| 29 | +There's a similar set of providers for the runtime environment, which |
| 30 | +should be added to your providers in main.ts: `APP_SHELL_RUNTIME_PROVIDERS`. |
| 31 | + |
| 32 | +`main.ts`: |
| 33 | +```typescript |
| 34 | +import { APP_SHELL_RUNTIME_PROVIDERS } from '@angular/app-shell'; |
| 35 | +//... |
| 36 | +bootstrap(AppComponent, APP_SHELL_RUNTIME_PROVIDERS); |
| 37 | +``` |
| 38 | + |
| 39 | +Then in the component(s) that will be shared between pre-render and runtime, |
| 40 | +add the `APP_SHELL_DIRECTIVES` and `IS_PRERENDER`. `APP_SHELL_DIRECTIVES` |
| 41 | +comes with two structural directives: |
| 42 | + |
| 43 | + * `*shellRender` designates a component that should **only** be rendered in the App Shell, |
| 44 | + and **not** rendered at runtime. |
| 45 | + * `*shellNoRender` designates a component that should **not** be rendered in the App Shell, |
| 46 | + and **only** rendered at runtime. |
| 47 | + |
| 48 | +The directives could be thought of as `*ngIf="isPrerender"` and `*ngIf="!isPrerender"`. |
| 49 | + |
| 50 | +`app.component.ts`: |
| 51 | +```typescript |
| 52 | +import { Inject, Component } from '@angular/core'; |
| 53 | +import { Routes } from '@angular/router'; |
| 54 | +import { APP_SHELL_DIRECTIVES, IS_PRERENDER } from '@angular/app-shell'; |
| 55 | +//... |
| 56 | +@Component({ |
| 57 | + selector: 'app-component', |
| 58 | + template: ` |
| 59 | + <md-toolbar> |
| 60 | + Hello |
| 61 | + </md-toolbar> |
| 62 | + <loading-indicator *shellRender></loading-indicator> |
| 63 | + <router-outlet *shellNoRender></router-outlet> |
| 64 | + `, |
| 65 | + directives: [ APP_SHELL_DIRECTIVES, ROUTER_DIRECTIVES] |
| 66 | +}) |
| 67 | +@Routes([{ |
| 68 | + // ... |
| 69 | +}]) |
| 70 | +class AppComponent { |
| 71 | + constructor(@Inject(IS_PRERENDER) isPrerender:boolean) { |
| 72 | + if (!isPrerender) { |
| 73 | + // fetch some data |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Kudos to @mgechev for pairing on this library, and letting @jeffbcross get the git commit credit. |
0 commit comments