Cards support JS lifecycle functions that you can implement in a template to run logic at specific points in the card's existence.
beforeCreate
beforeCreate runs during JS environment initialization, before the template node is created. Use it to preprocess data sent by the server.
The beforeCreate method has the following limits:
-
JS APIs cannot be called.
-
You cannot use asynchronous methods, such as
setTimeout setInterval. -
The
beforeCreatemethod takes no parameters and returns no value. -
If a server-sent field does not exist, wrap it in a
try catchblock. Otherwise, an exception causes template rendering to fail.
onCreated
Called once after the template node is created. All JS syntax is supported. Do not modify DOM nodes in this method because it may cause concurrency issues.
didAppear
Called when the template view enters the screen. If the view enters the screen multiple times, the method is called each time.
didDisappear
Called when the template view leaves the screen. If the view leaves the screen multiple times, the method is called each time.
onBackground
Called when the application moves to the background. If the application moves to the background multiple times, the method is called each time.
onForeground
Called when the application returns to the foreground. If the application returns to the foreground multiple times, the method is called each time.
onUpdated
Called when the card is updated. The parameter contains the changed fields.
onDestroyed
Called when the card is destroyed.
The onDestroyed method cannot call JS APIs, component methods, or timers with asynchronous logic. You can use it to print logs with console.info or cancel timers.
Precautions
Lifecycle methods must use standard function syntax. Arrow functions are not supported.
onCreated() {
console.info();
}
Example code
Download detailLifeCycle.zip for the complete example code.