在React中使用箭头功能就可以了
class AppComponent extends Component {
componentDidMount() {
this.createAppComponent()
}
createAppComponent() {
const node = this.node
}
render() {
return <div ref={node => this.node = node}></div>
}
}
如何在Vue组件中初始化相同的node
属性?
在Vue中,您使用$el
的ref
来获取底层DOM元素。
<my-widget ref='theWidget' />
mounted() {
const theElement = this.$refs.theWidget.$el
}
如果在循环中定义ref
,'theWidget'将是一个数组。
如果'component'只是一个HTML元素(例如div
或input
),那么$el
将是未定义的,因为'theWidget'将是组件引用。