vue2怎么把当前组件实例作为一个参数传递给子组件?-灵析社区

刘传疯子

renderComponent: { // 自定义内容 functional: true, render: (h, context) => { const render = context.props.render const params = context.props.params const attrs = context.data.attrs debugger return render(h, params, attrs) }, props: { render: { type: Function, required: true }, params: { type: Object, required: true } } }, 这是自定义的一个函数式组件,在使用它的时候,把当前组件的this传递给它时,打断点看到的是undefined,我不明白是为什么。按理说attrs里的instance应该是传过去的组件实例 * * * 补充:发现如果instance绑定的是一个函数的返回值,就能把this顺利传过去了,这是为什么,或者说在模板里写this指的是什么?

阅读量:397

点赞量:18

问AI
777七月
为什么要把组件实例传递给子组件?如果子组件要获取父级组件属性的可以用 "$parent" 呀。 但其实应该使用 "props" 传递给子组件需求的属性,以及使用 "$emit" 来触发父级组件的方法。 而不是粗暴的直接把组件实例传递给子组件。