| sidebar_position | 2 |
|---|
Creates a resource in the store which gets automatically loaded when an instance of the store is created.
function withInitialHypermediaResource<ResourceName extends string, TResource>(
resourceName: ResourceName, initialValue: TResource, url: string | Promise<string> | (() => string | Promise<string>)): SignalStoreFeature;- resourceName: The name of how the resource will be declared in the store.
- initialValue: The initial value of the resource before it is loaded from a URL.
- url: The URL from which the resource will be loaded. Accepts four forms:
- A
string: the URL is used directly at store creation time. - A
Promise<string>: the promise is awaited and the resolved value is used as the URL. - A function returning a
string: the function is called at store creation time, allowing injection of Angular services (e.g.() => inject(MyToken)). - A function returning a
Promise<string>: the function is called at store creation time and the resulting promise is awaited before loading. Useful for asynchronous URL resolution via injected services.
- A
With this feature the following state properties are added to the interface of the store:
<resourceName>: DeepSignal<TResource>A deep signal containing the data of the resource.
<resourceName>State: DeepSignal<
{
url: string,
isLoading: boolean,
isLoaded: boolean
}>A deep signal containing meta information about the resource.
- url: The URL from which the resource was last loaded.
- isLoading: Whether the resource is currently being loaded.
- isLoaded: Whether the resource currently in the state was loaded from the server.
With this feature the following methods are added to the interface of the store:
load<resourceName>FromUrl(url: string | null, fromCache?: boolean): Promise<void>
load<resourceName>FromUrl(url: Signal<string | null>): EffectRefLoads the resource from the provided URL. Two overloads are available:
- When called with a plain value (
string | null): loads the resource immediately and returns aPromise<void>that resolves when the request completes. Passingnullresets the resource to its initial value.- fromCache: When
true, skips loading if the resource is already loaded from the same URL. Defaults tofalse.
- fromCache: When
- When called with a
Signal<string | null>: sets up a reactive effect that automatically reloads the resource whenever the signal's value changes, and returns anEffectRefthat can be used to destroy the effect.
load<resourceName>FromLink(linkRoot: unknown, linkName: string): Promise<void>Loads the resource from the provided URL.
- linkRoot: An object containing the link to use.
- linkName: The name of the link to use to load the resource.
reload<resourceName>(): Promise<void>Reloads the resource from the last used URL.