有人可以向我解释为什么我在此代码中看不到连接状态吗?

时间:2019-02-27 08:03:09

标签: angular typescript ionic-framework

我需要有关离子网络插件的帮助。这是代码,但对我不起作用。没有控制台日志或任何其他模式。没有错误。 在顶部

import { Network } from '@ionic-native/network';

ionViewDidLoad() {
    this.getWarrentsNumber();
    let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
        console.log('network was disconnected :-(');
    });

    disconnectSubscription.unsubscribe();

    let connectSubscription = this.network.onConnect().subscribe(() => {
        console.log('network connected!');
        setTimeout(() => {
            if (this.network.type === 'wifi') {
                console.log('we got a wifi connection, woohoo!');
            }
        }, 3000);
    });

    connectSubscription.unsubscribe();
}

3 个答案:

答案 0 :(得分:0)

将变量connectSubscriptiondisconnectSubscription声明为类属性,然后尝试在unsubscribe()钩子而不是ioniViewWillLeave()中将它们ionViewDidLoad()插入。 因此,代码看起来类似于以下内容-

connectSubscription: any;
disconnectSubscription: any;

ionViewDidLoad() {
    this.getWarrentsNumber();
    this.disconnectSubscription = this.network.onDisconnect().subscribe(() => {
        console.log('network was disconnected :-(');
    });

    this.connectSubscription = this.network.onConnect().subscribe(() => {
        console.log('network connected!');
        setTimeout(() => {
            if (this.network.type === 'wifi') {
                console.log('we got a wifi connection, woohoo!');
            }
        }, 3000);
    });
}

ioniViewWillLeave() {
    this.disconnectSubscription.unsubscribe();
    this.connectSubscription.unsubscribe();
}

答案 1 :(得分:0)

该代码可以正常运行,但可以运行依赖平台,例如android或ios。我认为它可能无法在浏览器上进行任何操作。请针对平台设备测试您的应用。

否则您可以使用ngOnInit()代替ionViewDidLoad();

答案 2 :(得分:0)

这是插件官方github repo

的引文
  

在线事件在先前未连接的设备收到以下消息时触发   网络连接,以允许应用程序访问Internet。它   依赖与Connection API相同的信息,并在出现以下情况时触发   connection.type从NONE更改为任何其他值。

如您所见,onConnect仅在以前未连接的设备接收到网络连接时才会发出一些东西。

要在设备启动时检查启动状态,可以直接检查this.network.type

您可以创建将处理所有这些问题的服务

@Injectable()
export class MyNetworkService implements OnInit {
  public connection: BehaviorSubject<string> = new BehaviorSubject(null);
  constructor(private network: Network, private platform: Platform) {

    this.connection = this.network.type;
    this.platform.ready().then(() => {
      this.network.onConnect().subscribe(() => {
        this.connection = 'something';
      });
      this.network.onDisconnect().subscribe(() => {
        this.connection = 'none';
      });
    });
  }

  ngOnInit() {
    this._setCurrentConnectionType();

    this.platform.ready().then(() => {
      this.network.onConnect().pipe(timer(3000)).subscribe(this._onConnectHandler);
      this.network.onDisconnect().pipe(timer(3000)).subscribe(this._onDisconnectHandler);
    });
  }

  private _onConnectHandler= () => this.connection.next(this.network.type);

  private _onDisconnectHandler = () => this.connection.next('offline');
}

然后您可以将服务注入到任何地方,并订阅连接。

constructor(private myNetworkService: MyNetworkService) {
  this.myNetworkService.connection.subscribe(type => {
    // you might filter out nulls
    // Before subscribe add .pipe(filter(type => type !== null))
  })

}