0π
β
Itβs because self
happens to be another reference to the window
object, and is available in all modules.
Letβs walk thru the steps and see why this bug is happening.
- You load up
client:/id
. - The
created
method inClientReview
does ajax request, and assignsself
tothis
. - The
mounted
method inClientBar
does ajax request, and reassignsself
tothis
. Note that this also changed theself
variable reference inClientReview
. - The
ClientReview
ajax finishes and assignsself.clientInfo = response.data;
. Sinceself
is a reference toClientBar
, andClientBar
does not declareclientInfo
as a root data property, this assignment does nothing. ClientBar
ajax finishes and assignsself.client = response.data;
, which works.- You route away from
ClientReview
. - You route back to
ClientReview
. Repeat step 2. ClientReview
successfully renders becauseClientBar
has already rendered, and does not reassignself
.
The answer is to do let self = this
instead of self = this
.
The lesson is ALWAYS DECLARE YOUR VARIABLES with var
, let
or const
.
π€Eric Guan
Source:stackexchange.com