Reputation: 653
In React use of arrow function does the trick
class AppComponent extends Component {
componentDidMount() {
this.createAppComponent()
}
createAppComponent() {
const node = this.node
}
render() {
return <div ref={node => this.node = node}></div>
}
}
How can I initialize same node
property in Vue component?
Upvotes: 1
Views: 575
Reputation: 29109
In Vue, you use the $el
of a ref
to get the underlying DOM element.
<my-widget ref='theWidget' />
mounted() {
const theElement = this.$refs.theWidget.$el
}
If you define the ref
in a loop, 'theWidget' will be an array.
If the 'component' is just an HTML element (e.g. div
or input
) $el
will be undefined because 'theWidget' will be the component reference.
Upvotes: 1