vue-facing-decorator icon indicating copy to clipboard operation
vue-facing-decorator copied to clipboard

我发现,在父类的 @Props ,在子类不重复声明,就拿不到。

Open ruojianll opened this issue 1 year ago • 4 comments

我发现,在父类的 @Props ,在子类不重复声明,就拿不到。 案例如下:


interface Props {
  panelModel: IPanelModel<DatasourceMysql, BaseQuery>
}

@ComponentBase({})
class ExampleExtends extends TSX<Props>()(Vue) implements Props {
 public foo = number;
 public bar = number;
 @Prop({ required: true }) panelModel: IPanelModel;
}

在子类

interface Props {
  panelModel: IPanelModel<DatasourceMysql, BaseQuery>
}
@Component
class MyComponent extends TSX<Props>()(BaseQueryPanel) implements Props {
   created() {
       console.log(this.panelModel)// undefined 
   }

}

需要在 MyComponent 重载 @Prop({ required: true }) panelModel: IPanelModel; 才可以拿到。

Originally posted by @zhoulujun in https://github.com/facing-dev/vue-facing-decorator/issues/12#issuecomment-1182940327

ruojianll avatar Jul 13 '22 09:07 ruojianll

@zhoulujun

@ComponentBase
class CompBase extends TSX<{
    b: string
}>()(Vue) {
    @Prop({
        required: true
    })
    b!: string
}

interface Props {
    a: string

}

@Component
export default class Child extends TSX<Props>()(CompBase) implements Props {
    @Prop({
        required: true
    })
    a!: string
  
    z(){
        console.log(this.b)
    }
    render() {
        return <div onClick={()=>{this.z()}}>{this.a}{this.b}</div>
    }
}

我测试这段代码,Child里面可以识别CompBase.b的类型并且可以打印它的值。

ruojianll avatar Jul 13 '22 09:07 ruojianll

我也遇到这问题了,但是我发过代码后没能再现出来bug,后来没时间就把issue关闭了,如果不是一定要用prop的话可以用provide替代,也可以用 ref 直接 set 变量(可以不设置expose不然除了expose中的方法其它都无法调用),

BigGitHubLaLaLa avatar Aug 16 '22 04:08 BigGitHubLaLaLa

@BigGitHubLaLaLa 取不到属性是指ts类型识别不出来还是运行时vue取不到这个值?

ruojianll avatar Aug 16 '22 08:08 ruojianll

@BigGitHubLaLaLa @zhoulujun @Component 继承 @ComponentBase 是通过vue extends继承的,继承规则和mixin一样。可以考虑是否为特定代码导致了vue extends操作props时的冲突或覆盖

ruojianll avatar Aug 16 '22 08:08 ruojianll