Typescript ';这';类型脚本未定义
我对ionic/angular/typescript和cloudboost还很陌生,我正在努力使这一切都能一起工作 我已经启动了一个新的爱奥尼亚项目,主题是“超级”启动者 我已经成功实现了cloudboost登录功能,但我仍面临一些问题:Typescript ';这';类型脚本未定义,typescript,ionic-framework,cloudboost,Typescript,Ionic Framework,Cloudboost,我对ionic/angular/typescript和cloudboost还很陌生,我正在努力使这一切都能一起工作 我已经启动了一个新的爱奥尼亚项目,主题是“超级”启动者 我已经成功实现了cloudboost登录功能,但我仍面临一些问题: 我没有成功地使用用户提供者,因为它使用Http服务,Cloudboost不允许访问url,并且原始返回是可观察的 我无法在CBUser.logIn的回调函数中访问此,它未定义。 我尝试了几种使用胖箭头的方法,但都不起作用,因此目前,我设法解决了以下问题: 这
- 我没有成功地使用用户提供者,因为它使用Http服务,Cloudboost不允许访问url,并且原始返回是可观察的李>
- 我无法在CBUser.logIn的回调函数中访问此,它未定义。 我尝试了几种使用胖箭头的方法,但都不起作用,因此目前,我设法解决了以下问题: 这个=这个
import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { ModalController, IonicPage, NavController, ToastController } from 'ionic-angular';
import * as CB from 'cloudboost';
import { User } from '../../providers/providers';
import { MainPage } from '../pages';
import { ModalCguPage } from './modal-cgu';
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
// The account fields for the login form.
// If you're using the username field with or without email, make
// sure to add it to the type
account: { username: string, password: string } = {
username: 'defaultuser@defaultsite.co',
password: '01231234'
};
// Our translated text strings
private loginErrorString: string;
private loginSuccessString: string;
private redirectPageSuccess : any = MainPage;
constructor(public navCtrl: NavController,
public user: User,
public toastCtrl: ToastController,
public translateService: TranslateService,
public modalCtrl: ModalController) {
this.translateService.get('LOGIN_ERROR').subscribe((value) => {
this.loginErrorString = value;
})
this.translateService.get('LOGIN_SUCCESS').subscribe((value) => {
this.loginSuccessString = value;
})
}
// Attempt to login in through our User service
doLogin() {
// HERE : get back the main this to use it later
var falseThis = this;
// cloudboost login
let CBUser = new CB.CloudUser();
CBUser.set('username', this.account.username);
CBUser.set('password', this.account.password);
CBUser.logIn({
success: function(user) {
let toast = falseThis.toastCtrl.create({
message: falseThis.loginSuccessString + ' ' + user.username,
duration: 3000,
position: 'top'
});
toast.present();
falseThis.navCtrl.push(MainPage);
}, error: function(error) {
// error: function(error) {
// Unable to log in
let toast = falseThis.toastCtrl.create({
message: falseThis.loginErrorString,
duration: 3000,
position: 'top'
});
toast.present();
}
});
}
}
以下是初学者模板中包含的原始功能:
// Attempt to login in through our User service
doLogin() {
this.user.login(this.account).subscribe((resp) => {
this.navCtrl.push(MainPage);
}, (err) => {
this.navCtrl.push(MainPage);
// Unable to log in
let toast = this.toastCtrl.create({
message: this.loginErrorString,
duration: 3000,
position: 'top'
});
toast.present();
});
}
如果有人能把事情弄清楚,我会非常感激。
多谢各位
编辑工作解决方案: 在login.ts中
// Attempt to login in through our User service
doLogin() {
this.user.login(this.account).then( (user:any) => {
console.log('user displayed ');
console.log(user.username);
// login successful
let toast = this.toastCtrl.create({
message: this.loginSuccessString + user.username,
duration: 3000,
position: 'top'
});
toast.present();
this.navCtrl.push(MainPage);
}).catch( err => {
// Unable to log in
let toast = this.toastCtrl.create({
message: this.loginErrorString,
duration: 3000,
position: 'top'
});
toast.present();
});
}
在user.ts中:
login(account: any) {
let CBUser = new CB.CloudUser();
CBUser.set('username', account.username);
CBUser.set('password', account.password);
return new Promise((resolve, reject) =>{
CBUser.logIn({
success: (user) => {
//Login successful
resolve(user)
},
error: (error) => {
reject(error)
//Error in user login.
}
});
});
}
该示例使用arrow lambdas,它更改了
this
的规则。查看详细信息
success: function(user) {
//this not accessible
}
success: user => {
//this is accessible
}
我无法在CBUser.logIn的回调函数中访问它,它是
未定义。我试过几种方法来对付胖箭,但都没用,所以
目前,我设法解决了这个问题
我想佩斯回答了这个问题。当我面对这个问题时,这个链接帮助了我
对于CloudBoost回调,请使用:
{
success: (obj) => {
//success
},
error: (err) => {
//Error
}
}
我没有成功使用用户提供程序,因为它正在使用Http服务
Cloudboost不提供对url的访问,以及原始返回
是一个可观察的
用户提供程序只是使用Http请求启动的一个示例。要在cloudboost中使用用户提供程序,我建议将登录功能更改为
login(account) {
let CBUser = new CB.CloudUser();
CBUser.set('username', account.username);
CBUser.set('password', account.password);
return new Promise((resolve, reject) =>{
CBUser.logIn({
success: (user) => {
//Login successful
resolve(user)
},
error: (error) => {
reject(error)
//Error in user login.
}
})
})
}
并使用以下命令调用函数:
this.user.login(this.account).then( user => {
//login successful
this.navCtrl.push(MainPage);
}).catch( err => {
this.navCtrl.push(MainPage);
// Unable to log in
let toast = this.toastCtrl.create({
message: this.loginErrorString,
duration: 3000,
position: 'top'
});
toast.present();
});
非常感谢,我现在可以访问它了!我正在尝试
成功(用户:any)=>
,但它不起作用。谢谢你的链接。谢谢你的回答,一切似乎都很好。除此之外,我修改了call函数来添加一条关于成功的欢迎消息,如下所示:this.user.login(this.account)。然后(user=>{//login successful let toast=this.toastCtrl.create({message:this.loginsuccesstring+''+user.username,持续时间:3000,位置:'top'});toast.present();this.navCtrl.push(MainPage);}但我收到一条错误消息:找不到名称“user”。
问题出在哪里?很抱歉,错误消息就是这条:属性“username”在类型“{}”上不存在“.
@axx我没有测试它,但是由于用户类型是CloudObject的一个子类,您应该能够使用user.document.username
替代方法是使用get()函数user.get('username'))
就像我在中描述的那样:user.document.username
,但在生成时失败:Typescript错误属性'document'在类型{}上不存在。
与user.get('username')相同。
它提供Typescript错误属性'get'在类型{}上不存在“.
我尝试更改用户名,以确保与类名没有冲突,但也不起作用。因此,最终要获取user.username,请确保在文件顶部声明(user:any)
或(user:CB.CloudUser)
和作为“cloudboost”中的CB导入*;
(仅当您使用CB.CloudUser时)this.user.login(this.account).然后((用户:any)=>{console.log('user displated');console.log(user.username);//登录成功让toast=this.toastCtrl.create({消息:this.loginsucessstring+user.username,持续时间:3000,位置:'top'));toast.present();this.navCtrl.push(主页);}