// 在EntryAbility.ets中进行静默登录选项初始化
PersistentStorage.persistProp('isRemembered', false); Button($r('app.string.login')).onClick(async () => {
let index: number = this.accountList.findIndex(v => v.userName === this.userName);
if (index > -1) {
// 数据库存在用户信息,检验密码是否正确
} else {
// 数据库中不存在用户信息,进行注册
}
});
// 登录页进行状态更新
Checkbox({ name: 'checkbox', group: 'checkboxGroup' }).onChange(() => {
this.isRemembered = !this.isRemembered;
}); @Component
struct Review {
// 同步登录状态
@StorageLink('isLogin') isLogin: boolean = false;
dialogController: CustomDialogController | null = new CustomDialogController({
builder: CustomDialogTest({
isLogin: this.isLogin
}),
alignment: DialogAlignment.Bottom,
})
build() {
Button('点击评论').onClick((event: ClickEvent) => {
// 根据登录状态判断是否要弹窗
if (this.isLogin) {
// 已登录,进行评论操作
this.dialogController?.open();
console.info('登录成功:', this.isLogin);
} else {
this.dialogController?.open();
console.info('请先登录:', this.isLogin);
}
})
}
} @Entry
@Component
struct LoginPage {
@StorageLink('isLogin') isLogin: boolean = false;
pageInfo: NavPathStack = new NavPathStack();
build() {
Scroll() {
Navigation(this.pageInfo) {
Column() {
Column({ space: 5 }) {
Row() {
Checkbox()
.select(this.isLogin)
.onChange((value) => {
if (value) {
this.isLogin = true;
} else {
this.isLogin = false;
}
});
Text('点击改变登录状态');
}
.width('100%')
.height('90%')
.justifyContent(FlexAlign.Center);
};
Column() {
Row({ space: 5 }) {
Button('点击登录')
.width('40%')
.onClick(() => {
// 保存登录状态
this.getUIContext().getPromptAction().showToast({ message: '已登录' });
AppStorage.setOrCreate('isLogin', true);
// 持久化操作可在EntryAbility中进行
PersistentStorage.persistProp('isLogin', true);
});
Review();
};
}
.width('100%')
.layoutWeight(1)
.justifyContent(FlexAlign.End);
}
.border({ color: '#f1f3f5' })
;
}
.mode(NavigationMode.Stack)
.hideToolBar(true)
.hideTitleBar(true)
.onAppear(() => {
console.info('组件挂载');
});
};
}
}
@Component
struct Review {
// 同步登录状态
@StorageLink('isLogin') isLogin: boolean = false;
build() {
Button('点击评论')
.width('40%')
.onClick(() => {
// 根据登录状态判断是否要弹窗
if (this.isLogin) {
// 已登录,进行评论操作
this.getUIContext().getPromptAction().showToast({ message: `登录状态:${this.isLogin}` });
console.info('登录成功:', this.isLogin);
} else {
this.getUIContext().getPromptAction().showToast({ message: `登录状态:${this.isLogin}` });
console.info('请先登录:', this.isLogin);
}
});
}
} 效果图如下:
