Angular+ionic实现退出功能
Angular+ionic实现退出功能
1,html中
退出当前账户
2,ts中
引入AlertController
import { ToastController, LoadingController, NavController,AlertController } from '@ionic/angular';
export class SetUpPage extends UserInfo implements OnInit {constructor(private rest: RestService,private router: Router,private activatedRoute: ActivatedRoute,public appService: AppService,private loadingCtrl: LoadingController,private toastCtrl: ToastController,private navCtrl: NavController,public localStorageService: StorageService,private storageService: StorageService,private alertController: AlertController, //引入AlertController) { super(appService, localStorageService); }}async logoutConfirm() {const alert = await this.alertController.create({header: '提醒',message: '确定退出登录吗?',buttons: [{text: '取消',role: 'cancel',cssClass: 'secondary',handler: (blah) => {}}, {text: '确定',handler: () => {this.logout();//执行退出方法}}]});await alert.present();}//调用接口:logoutasync logout() {this.showLoading(this.loadingCtrl, '退出中...').then((loading) => {this.rest.apiPost('', this.rest.logout + '/' + localStorage.getItem('QZH_TOKEN')).subscribe((res) => {if (res.status === 200) {this.toastSuccess(this.toastCtrl, '退出成功');this.storageService.removeToken();//清除密码及账号// this.storageService.removeStorage('username');// this.storageService.removeStorage('password');// this.storageService.removeStorage('userId');// this.appService.userInfoEvent.emit('update');]this.goBack();} else {this.toastError(this.toastCtrl, res.msg);}loading.dismiss();}, (err) => {this.toastError(this.toastCtrl, err.msg);loading.dismiss();});});}
3,踩坑,关键代码
不可以传参,这里会报错,有坑。
使用传空,拼接localStorage.getItem就可以实现退出功能
this.rest.apiPost('', this.rest.logout + '/' + localStorage.getItem('QZH_TOKEN'))
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
