我使用的安全登录系统有点问题。我想做的是检查设备的本机存储中是否有一个名为"user"的项,然后检查该用户是否存在于我们的数据库中,并从数据库中的行中读取唯一id以用于方法。
import {Component} from "@angular/core";
import { NativeStorage } from 'ionic-native';
import {Events, NavController, NavParams, ModalController, LoadingController} from 'ionic-angular';
import {Login} from '../login/login';
import {ChallengePage} from '../challenge/challenge';
import {AddTeamPage} from '../add-team/add-team';
import {EditTeamPage} from '../edit-team/edit-team';
import {Users} from '../../providers/users/users';
import {Teams} from '../../providers/teams/teams';
import {Results} from '../../providers/results/results';
import {Players} from '../../providers/players/players';
import {Packs} from '../../providers/packs/packs';
@Component({
templateUrl: 'dashboard.html'
})
export class Dashboard {
_id: string;
user: any;
team: any;
teamExists: boolean;
gamesPlayed: boolean;
sample: any;
strip: any;
last5: any;
constructor(
private nav: NavController,
public events: Events,
private _navParams: NavParams,
private playerService: Players,
private userService: Users,
private teamService: Teams,
private resultService: Results,
private packService: Packs,
private modalCtrl: ModalController,
private loadingCtrl: LoadingController
) {
let loading = this.loadingCtrl.create({
content: "Loading..."
});
loading.present();
this.teamExists = false;
this.gamesPlayed = false;
var user = {};
var _id = {};
//main id
NativeStorage.getItem('user')
.then(function (data) {
user = data;
console.log("native storage object found");
userService.accountExists(this.user.email).then((data) => {
user = data;
if (user) {
_id = user['_id'];
console.log("user found!", user);
console.log("_id for logged in user", _id);
} else {
console.log("no existing user account found!");
nav.setRoot(Login);
nav.push(Login);
nav.pop();
}
});
}, function (error) {
console.error("Error getting user from native storage");
});
}
addTeam(id) {
this.nav.push(AddTeamPage, { 'user_id': id });
}
EDIT:在模板中添加了调用函数的代码。
<ion-header class="gradient-dark-up">
<ion-navbar class="navbar-primary">
<button ion-button color="main" icon-left menuToggle>
<ion-icon class="header-main" name="menu"></ion-icon>
</button>
<ion-title><h1 class="header-main">Dashboard</h1></ion-title>
<ion-buttons end>
<button ion-button icon-only color="primary" *ngIf="!teamExists" (click)="addTeam(_id)"><ion-icon class="header-main" name="add"></ion-icon></button>
</ion-buttons>
</ion-navbar>
</ion-header>
因此,addTeam的id应该通过点击模板上的按钮读取,但当我试图传入_id时,它总是未定义的,即使在构造函数中,当我将值输出到控制台时,它显示了正确的_id。
我认为这只是我缺乏angular 2绊倒我的经验,所以如果我做了一些根本错误的事情,请随时纠正我。
提前谢谢。
如果使用this.
访问当前类的属性,则不要使用function () {
NativeStorage.getItem('user')
.then(function (data) {
而是使用箭头功能
NativeStorage.getItem('user')
.then((data) => {