行为主体在离子 4 中未定义
behavior subject is undefined in ionic 4
我有一个名为套接字服务的简单服务
import { Injectable } from '@angular/core';
import { Socket } from 'ngx-socket-io';
import { AuthenticationService } from './authentication.service';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class SocketsService {
isConnected = false;
isOnline = new BehaviorSubject(false);
constructor(private socket: Socket,private authenticationService : AuthenticationService) {
}
connect(){
console.log("connecting to socket server....")
return new Promise((resolve, reject) => {
if(!this.isConnected)
{
this.socket.connect();
console.log("Authenticating socket server....")
this.socket.emit('authentication', {"HESOYAM" : this.authenticationService.user, "AEZAKMI" : this.authenticationService.pass, 'type' : 'mobile-app' });
this.isConnected=true;
this.socket.on("authenticated",function(data){
console.log("socket auth was succesful with data "+data);
console.log(this.isOnline);
//this.isOnline.next(true);
})
}
})
}
当执行 this.isOnline.next(true);
时,它返回一个 isOnline 未定义的错误。
同样控制台日志记录 this.isOnline
在控制台中打印 undefined 。
我正在从页面
调用连接方法
export class TabsPage implements OnInit {
constructor(private socketService : SocketsService, private authenticationService : AuthenticationService) { }
ngOnInit() {
this.authenticationService.refershToken().then(()=>{
this.socketService.connect()
})
}
}
刷新令牌定义如下
refershToken(){
return new Promise((resolve, reject) => {
this.storage.get(user).then((user) => {
this.storage.get(pass).then((pass) => {
if(user && pass){
console.log("refresing token")
this.login(user,pass).subscribe((res: LoginInterface) => {
if(res.auth == true){
this.save_login(user,pass,res.name,res.token)
this.user = user
this.pass = pass
resolve()
}else{
throw new Error('Auth Failed');
}
})
}
});
});
})
}
我想我遗漏了一些非常基本的东西。
PS:我是 Typescript 的新手 Angular
this
对象丢失在您的 SocketsService
这一行:
this.socket.on("authenticated",function(data){ }
请将函数更改为这样的 lambda 表达式
this.socket.on("authenticated", data =>{ })
原因请参考https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
我有一个名为套接字服务的简单服务
import { Injectable } from '@angular/core';
import { Socket } from 'ngx-socket-io';
import { AuthenticationService } from './authentication.service';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class SocketsService {
isConnected = false;
isOnline = new BehaviorSubject(false);
constructor(private socket: Socket,private authenticationService : AuthenticationService) {
}
connect(){
console.log("connecting to socket server....")
return new Promise((resolve, reject) => {
if(!this.isConnected)
{
this.socket.connect();
console.log("Authenticating socket server....")
this.socket.emit('authentication', {"HESOYAM" : this.authenticationService.user, "AEZAKMI" : this.authenticationService.pass, 'type' : 'mobile-app' });
this.isConnected=true;
this.socket.on("authenticated",function(data){
console.log("socket auth was succesful with data "+data);
console.log(this.isOnline);
//this.isOnline.next(true);
})
}
})
}
当执行 this.isOnline.next(true);
时,它返回一个 isOnline 未定义的错误。
同样控制台日志记录 this.isOnline
在控制台中打印 undefined 。
我正在从页面
export class TabsPage implements OnInit {
constructor(private socketService : SocketsService, private authenticationService : AuthenticationService) { }
ngOnInit() {
this.authenticationService.refershToken().then(()=>{
this.socketService.connect()
})
}
}
刷新令牌定义如下
refershToken(){
return new Promise((resolve, reject) => {
this.storage.get(user).then((user) => {
this.storage.get(pass).then((pass) => {
if(user && pass){
console.log("refresing token")
this.login(user,pass).subscribe((res: LoginInterface) => {
if(res.auth == true){
this.save_login(user,pass,res.name,res.token)
this.user = user
this.pass = pass
resolve()
}else{
throw new Error('Auth Failed');
}
})
}
});
});
}) }
我想我遗漏了一些非常基本的东西。
PS:我是 Typescript 的新手 Angular
this
对象丢失在您的 SocketsService
这一行:
this.socket.on("authenticated",function(data){ }
请将函数更改为这样的 lambda 表达式
this.socket.on("authenticated", data =>{ })
原因请参考https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions