Watching data
Watch data changes reactively with the @watch decorator. Support for properties, expressions, and computed values with automatic dependency tracking.
import { watch } from '@aurelia/runtime-html';
class UserProfile {
firstName = 'John';
lastName = 'Doe';
@watch('firstName')
nameChanged(newName, oldName) {
console.log(`Name changed from ${oldName} to ${newName}`);
}
}Two Ways to Use @watch
Name
Type
Description
Watch Options
Option
Type
Default
Description
What You Can Watch
Simple Properties
Nested Properties
Array Properties
Symbol Properties
Numeric Property Keys
Computed Watchers
Complex Computed Example
Real-World Examples
API Data Synchronization
Form Validation
State Management
Dynamic UI Updates
Watcher Lifecycle
Lifecycle Phase
Watcher Active?
Example
Flush Modes
Manual Watcher Classes
ComputedWatcher
ComputedWatcherExpressionWatcher
ExpressionWatcherHow Dependency Tracking Works
Manual Dependency Registration
Callback Signature Details
Class Decorator with Callback Function
Method Name as String Callback
Best Practices
✅ Do's
❌ Don'ts
Performance Tips
Common Errors and Troubleshooting
1. Choose the Right Flush Mode
2. Avoid Mutating Dependencies in Computed Getters
3. Understand Collection Observation
4. Avoid Mutating Dependencies in Computed Getters
5. Be Cautious with Object Identity
Error: AUR0774 - Static Method Decoration
Computed Function Errors
6. Do Not Return Promises or Async Functions
Circular Dependencies
Advanced Watch Patterns
Deep Object Observation
Collection Observation Patterns
Observable Array Methods
Map and Set Observation
Flush Timing Control
Performance Considerations
When to Use Different Patterns
Optimizing Watch Expressions
Last updated
Was this helpful?