ts里定义property的时候怎么没有notify了?


或者是我的写法不对?

这个已经不再支持, http://forum.cocos.com/t/es6-classes-typescript/46550/10 这个帖子里面有讲到

可以用set()代替。

如何代替?

麻烦一点,大概是(手打,未测试):

@property({
    default: ''
})
_eventParams: string = '';
@property({
    displayName: '事件参数',
    tooltip: '触发...',
    type: cc.String
})
get eventParam(): string {
    return this._eventParams;
}
set eventParam(newEventParam: string) {
    this.notify(this._eventParams, newEventParam);
    this._eventParams = newEventParam;
}

notify(oldEventParam: string, newEventParam: string) {
    cc.log(oldEventParam, newEventParam);
}
1赞

###好方法,测试通过了
###测试结果


###代码如下
@ccclass
export default class ReplaceNotify extends cc.Component {
@property(cc.Label)
tipLabel:cc.Label;
@property({
default:’’
})
_text:string=’’;

@property({
    displayName:'测试文本',
    tooltip:'用于测试',
    type:cc.string
})
get text():string{
    return this._text;
}
set text(value){
    this.notify(this._text,value);
    this._text=value;

}
notify(oldValue:string,newValue:string){
    console.log(`oldValue:${oldValue},newValue:${newValue}`);
    if(!!this.tipLabel){
        this.tipLabel.string=`oldValue:${oldValue},newValue:${newValue}`;
    }
}  

}

@property(cc.Sprite)
set barSprite(value: cc.Sprite) {
    this._barSprite = value;
    this._initBarSprite();
}

get barSprite(): cc.Sprite {
    return this._barSprite;
}

private _barSprite: cc.Sprite;

这样写报错
The ‘default’ value of ‘MProgressBar.barSprite’ should not be used with a ‘get’ function.

@property()
get itemCount(): number {
    return this._itemCount;
}

set itemCount(value: number) {
    this._itemCount = value;
    this.flushList();
}

这样写没法序列化

很明显的BUG,看来都是用 JS在写代码 不然就是这个引擎没人用

_barSprite也要加@property才能序列化。

这不是 bug,这是有意这么设计的

请问下这个问题的最佳解决方案是什么?

不用 notify,自己用 get set 实现。

:cry:但是用了getter setter不就不能被序列化了么

必须set/get方式必须有 displayname属性 不然的话 set/get 序列化在编辑器中就回报错