Resolve
Resolves allow you to fetch data asynchronously before a state is activated. They ensure required data is available before the state’s views are rendered.Resolve Basics
Define resolves in state declarations:user resolve:
- Executes before entering the state
- Can inject services and dependencies
- Returns data (or a promise for data)
- Makes data available to views and child states
Resolve Syntax
Object Syntax
Simple key-value object:Array Syntax (Advanced)
Explicit dependency declaration:Injectable Dependencies
Available Tokens
Special injectable tokens:Transition Object
Access transition details:Injecting Services
Injecting Other Resolves
Resolve Lifecycle
When Resolves Execute
Resolve Inheritance
Child states inherit parent resolves:Resolve Policies
Control when and how resolves are fetched:When Policy
Controls when the resolve is fetched:- Fetched just before entering the state
- Only fetched if state will be entered
- Good for most use cases
- Fetched early in the transition
- Fetched even if state might be redirected
- Use for critical data needed for redirects
Async Policy
Controls if transition waits for resolve:- Transition waits for resolve to complete
- State doesn’t activate until data is ready
- Views render with data available
- Transition continues immediately
- State activates without waiting
- Views must handle loading state
State-Level Policy
Set default policy for all resolves in a state:Per-Resolve Policy
Override policy for individual resolves:Accessing Resolved Data
In Views (Framework-Specific)
Resolved data is injected into views/components based on the framework.In Transition Hooks
Async Access
In Child Resolves
As shown earlier, child resolves can inject parent resolves as dependencies.Error Handling
Resolve Errors
If a resolve rejects, the transition is aborted:Catching Errors
Common Patterns
Loading User Data
Loading Entity by ID
Loading List Data
Dependent Resolves
Parallel Loading
Conditional Loading
Caching Resolves
Pre-Resolved Data
Provide data without a resolve function:Dynamic Resolves
Add resolves programmatically:Best Practices
// Good - single responsibility
resolve: {
user: (UserService) => UserService.current(),
permissions: (PermissionService) => PermissionService.load()
}
// Avoid - doing too much
resolve: {
everything: (Services) => loadEverything()
}
// EAGER for critical data
resolvePolicy: { when: 'EAGER' }
// LAZY for most data (default)
resolvePolicy: { when: 'LAZY' }
resolve: {
user: async (UserService) => {
try {
return await UserService.load();
} catch (error) {
console.error(error);
return null; // Provide fallback
}
}
}
// Good - declarative dependencies
resolve: {
user: (UserService) => UserService.current(),
profile: (user, ProfileService) => ProfileService.get(user.id)
}
// Avoid - nested promises
resolve: {
data: async (UserService, ProfileService) => {
const user = await UserService.current();
const profile = await ProfileService.get(user.id);
return { user, profile };
}
}
Related Documentation
- State Configuration - Define resolves in states
- Transition Hooks - Access resolves in hooks
- Nested States - Resolve inheritance
API Reference
- Resolvable - Resolve implementation
- ResolveContext - Resolve context