Check internet connection in web using Angular 4

后端 未结 10 2093
感情败类
感情败类 2020-12-15 19:37

I am using Angular 4 for my application development. I need to check If the network connection is available for its have any Connection issue using Angular for. Is it possib

10条回答
  •  [愿得一人]
    2020-12-15 19:51

    For the required task, There is a npm package, ng-connection-service. Below is the complete code:

    import { Component,OnInit } from '@angular/core';
    import { ConnectionService } from 'ng-connection-service';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    
    export class AppComponent {
      title = 'internet-connection-check';
      status = 'ONLINE'; //initializing as online by default
      isConnected = true;
    
      constructor(private connectionService:ConnectionService){
        this.connectionService.monitor().subscribe(isConnected => {
          this.isConnected = isConnected;
          if(this.isConnected){
            this.status = "ONLINE";
          } else {
            this.status = "OFFLINE"
          }
          alert(this.status);
        });
      }
      ngOnInit(){
    
      }
    
    }
    

    Also you can find the working complete code over: click here

提交回复
热议问题