aisuda / vue2-amis-custom-widget

amis自定义组件模板(vue2.0技术栈)
13 stars 7 forks source link

组件转换中产生的this指向问题 #6

Closed large2b closed 6 months ago

large2b commented 1 year ago

createVu2Component 中

this.vm = new Vue({
                data: extendObject(amisData, typeof data === 'function' ? data() : data),
                ...rest,
                props: rest.props || {},
            });

如果 vue 组件的 data 中有 this 比如:

data() {
  return {
    name: this.me
  }
}

就会产生 this 指向问题

我认为应该要

createVu2Component 中
this.vm = new Vue({
                data: extendObject(amisData, typeof data === 'function' ? data.call(this) : data),
                ...rest,
                props: rest.props || {},
            });

来把this指向vue

wibetter commented 6 months ago

@large2b 确实存在这种现象,但createVu2Component 中 this 和 vue组件中的 this 不是同一个,所以不能使用这种方式绑定this。可以使用箭头函数来避免this指向变动。