@State修饰一个单例模式类的实例时,变更该实例中的属性,UI没有刷新。
效果演示如下:在Page2中改变颜色后,返回Index页面,颜色没有同步刷新。

问题代码如下:
// Index.ets
import { VoiceReadHelper } from './VoiceReadHelper';
@Entry
@Component
struct Index {
@State private voiceReadHelper: VoiceReadHelper = VoiceReadHelper.getInstance();
build() {
Column() {
Text()
.width(100)
.height(100)
.backgroundColor(this.voiceReadHelper.isSpeaking ? '#61CFBE' : '#8981F7')
.borderRadius(8)
.margin({ bottom: 100 })
.onClick(() => {
this.voiceReadHelper.isSpeaking = !this.voiceReadHelper.isSpeaking;
});
Text('去Page2').onClick(() => {
this.getUIContext().getRouter().pushUrl({ url: 'pages/Page2' });
});
}
.height('100%')
.width('100%');
}
} // Page2.ets
import { VoiceReadHelper } from './VoiceReadHelper';
@Entry
@Component
struct Page2 {
@State private voiceReadHelper: VoiceReadHelper = VoiceReadHelper.getInstance();
build() {
Column() {
Text()
.width(100)
.height(100)
.backgroundColor(this.voiceReadHelper.isSpeaking ? '#61CFBE' : '#8981F7')
.borderRadius(8)
.margin({ bottom: 100 })
.onClick(() => {
this.voiceReadHelper.isSpeaking = !this.voiceReadHelper.isSpeaking;
});
Text('这是Page2');
}
.height('100%')
.width('100%');
}
} export class VoiceReadHelper {
private static instance: VoiceReadHelper | null = null;
isSpeaking = false;
private constructor() {
}
static getInstance(): VoiceReadHelper {
if (VoiceReadHelper.instance == null) {
VoiceReadHelper.instance = new VoiceReadHelper();
}
return VoiceReadHelper.instance;
}
} @Observed
export class VoiceReadHelper {
private static instance: VoiceReadHelper | null = null;
isSpeaking = false;
private constructor() {
}
static getInstance(): VoiceReadHelper {
if (VoiceReadHelper.instance == null) {
VoiceReadHelper.instance = new VoiceReadHelper();
}
return VoiceReadHelper.instance;
}
} export class VoiceReadHelper {
private static instance: VoiceReadHelper | null = null;
isSpeaking = false;
private constructor() {
}
static getInstance(): VoiceReadHelper {
if (VoiceReadHelper.instance == null) {
VoiceReadHelper.instance = new VoiceReadHelper();
AppStorage.setOrCreate('voiceReadHelper', VoiceReadHelper.instance);
}
return VoiceReadHelper.instance;
}
} // Index.ets
import { VoiceReadHelper } from './VoiceReadHelper';
@Entry
@Component
struct Index {
@StorageLink('voiceReadHelper') voiceReadHelper: VoiceReadHelper = VoiceReadHelper.getInstance();
build() {
Column() {
Text()
.width(100)
.height(100)
.backgroundColor(this.voiceReadHelper.isSpeaking ? '#61CFBE' : '#8981F7')
.borderRadius(8)
.margin({ bottom: 100 })
.onClick(() => {
this.voiceReadHelper.isSpeaking = !this.voiceReadHelper.isSpeaking;
});
Text('去Page2').onClick(() => {
this.getUIContext().getRouter().pushUrl({ url: 'pages/Page2' });
});
}
.height('100%')
.width('100%');
}
} // Page2.ets
import { VoiceReadHelper } from './VoiceReadHelper';
@Entry
@Component
struct Page2 {
@StorageLink('voiceReadHelper') voiceReadHelper: VoiceReadHelper = VoiceReadHelper.getInstance();
build() {
Column() {
Text()
.width(100)
.height(100)
.backgroundColor(this.voiceReadHelper.isSpeaking ? '#61CFBE' : '#8981F7')
.borderRadius(8)
.margin({ bottom: 100 })
.onClick(() => {
this.voiceReadHelper.isSpeaking = !this.voiceReadHelper.isSpeaking;
});
Text('这是Page2');
}
.height('100%')
.width('100%');
}
}