API: Options/Lifecycle Hooks Flashcards
All lifecycle hooks automatically have their this
context bound to the instance, thus…
you should never use arrow functions to define lifecycle hooks. They will not be bound to the instance if you do.
beforeCreate
Type: function
- Synchronously called immediately…
- after the instance has been initialized
- before data observation and event/watcher setup
created
Type: function
- Synchronously called…
-
after the instance is created, and all options are processed, which means the following have been setup:
- data observation
- computed properties
- methods
- watch/event callbacks
-
after the instance is created, and all options are processed, which means the following have been setup:
- Mounting phase has not begun, thus the
$el
property will not be available yet
beforeMount
Type: function
- Synchronously called right before mounting begins
- The
render
function is about to be called for the first time
This hooks is not called during server-side rendering
mounted
Type: function
- Called right after instance has been mounted
-
el
is replaced by newly createdvm.$el
-
mounted
does not guarantee that all child components have been mounted- If need the entire view to be rendered use
vm.$nextTick
inside of themounted
- If need the entire view to be rendered use
This hook is not called during server-side rendering
beforeUpdate
Type: function
- Called when data changes, before the DOM is patched
- Good place to access the DOM before an update
- Such as removing manually added event listeners
This hook is not called during server-side rendering, because only the initial render is performed server-side
updated
Type: function
- Called after a data change causes the virtual DOM to be re-rendered and patched
- Perform DOM-dependent operations here
- Avoid changing state in this hook, better to use a computed property or a watcher
-
updated
does not guarantee that all child components have also been re-rendered- use
vm.$nextTick
inside ofupdated
to ensure entire view has been re-rendered
- use
This hook is not called during server-side rendering
activated
Type: function
- Called when a kept-alive component is activated
This hook is not called during server-side rendering
deactivated
Type: function
- Called when a kept-alive component is deactivated
This hook is not called during server-side rendering
beforeDestroy
Type: function
- Called right before a Vue instance is destroyed
- At this point, the instance is still fully functional
This hook is not called during server-side rendering.
destroyed
Type: function
- Called after a Vue instance has been destroyed
- When this hook is called…
- all directives of the Vue instance have been unbound
- all event listeners have been removed
- and all child Vue instances have also been destroyed
This hook is not called during server-side rendering
errorCaptured
Type: (err: Error, vm: Component, info: String) =\> ?boolean
- Called when an error from any descendent component is captured
- Arguments:
-
err
: the error -
vm
: the component instance that triggered the error -
info
: string containing information on where the error was captured
-
- The hook can return
false
to stop the error from propogating further - Component state can be modified in here, but be sure to put proper checks into place to prevent causing an infinite render loop
Error Propogation Rules
- By default, all errors are still sent to the global
config.errorHandler
if it’s defined- Used to report errors to an analytics service in a single place
- If multiple
errorCaptured
hooks exist along the inheritance component chain, they will all be invoked on the same error - If
errorCaptured
itself throws an error, both this error, and the original are sent to the globalconfig.errorHandler
- An
errorCaptured
hook returningfalse
will prevent any othererrorCaptured
hooks and the globalconfig.errorHandler
from being invoked for this error- Good idea to let an error propogate to the global
config.errorHandler
so it can properly be reported
- Good idea to let an error propogate to the global