Are x-ref / $refs scoping rules documented anywhere? #4284
-
The docs show an example like: <button @click="$refs.text.remove()">Remove Text</button>
<span x-ref="text">Hello 👋</span> I have already found the discussion on old bugs pointing out that this isn't sufficient and there has to be a parent element with an But in my case this still doesn't seem to work I have (simplified src): <div x-data="{}" class="mdc-menu-surface--anchor" id="demo-menu">
<button x-data="MDCRipple()" @click="$refs.mymenu1.open=true">Click me</button>
<div x-data="MDCMenu()" x-ref="mymenu1"></div>
</div> But I get If I While typing out this message I thought of something it might be... getting rid of the so it seems to be because the refs are scoped to a component and in case of nested components the child component refs do not become part of the parent component refs scope I can see how that probably makes sense, would be great if all this was explained in the docs though |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Is the full syntax documented anywhere? I can see that e.g. elsewhere I've seen this: <li :id="$id('list-item', item.id)">...</li> but I can't find what is the significance of colon prefix on |
Beta Was this translation helpful? Give feedback.
-
It's the same as properties. A child component cannot see the property of a different child component, same for refs. Doing differently would require the child to somehow inject the reference into the parent, potentially overriding a reference to the parent itself or a different object. |
Beta Was this translation helpful? Give feedback.
-
yeah, refs are added to the nearest component root. Otherwise multiple of the same component in a loop would fight over things. |
Beta Was this translation helpful? Give feedback.
yeah, refs are added to the nearest component root.
Otherwise multiple of the same component in a loop would fight over things.