Accessing $id in a component's init function #4171
Unanswered
docdunning
asked this question in
1. Help
Replies: 1 comment 3 replies
-
Yes, you are correct. init runs before all the other directives are computed (except for x-data). It's kind of the equivalent to the old beforeMount in VueJs/React. If you want to do it after (aka component mounted), nextTick is the right solution. AN alternative solution is to use id in x-data |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using
$id
to generate unique ids, and it's working fine in that respect.However, I'd like to access the id when the component is initialised, and that doesn't quite work as I expected.
See the example code below. In the component's
init()
function, it logsthis.$el
to the console, which shows that the id has been generated:<div x-data="block" :id="$id('block')" id="block-1">Block 1</div>
Then it logs
this.$el.id
, which I expect to showblock-1
, but it's an empty string.As per the code below, I can use
$nextTick
to logthis.$el.id
, in which case it does show the correct value for the id.Is
$nextTick
the best solution, or have I misunderstood something about the sequence of events?Beta Was this translation helpful? Give feedback.
All reactions