Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular - can't run rxjs interval with angular universal

Tags:

angular

rxjs

I made a carousel effect through the RXJS's interval timing to change parameter.

I found that it works in development mode(ng serve), but it does not work properly in the universal mode, It's can't enter the page.

For example:

n: number = 0;
max: number = 5;

constructor(){}

ngOnInit() {
  this.carousel();
}

carousel() {
  this.subscription = interval(2000).subscribe(() => {
      //In universal, the console.log message show in node.js background log message not in browser console message. Each time the page is reorganized, it will be executed once and cannot be destroyed.
      console.log(`show the photo: ${this.n}`);
      if (this.n>this.max){
        this.n = 0;
      }else{
       this.n = this.n+1;
      }
  }
}

ngOnDestroy() {
  this.subscription.unsubscribe();
}

I found in universal model, assume that the carousel effect is on the page B, you can successfully enter page B through the link on page A, but opening page B directly will fail.

I tried to start the carousel in ngAfterContentInit() and it doesn't work.

like image 737
Finn Avatar asked Mar 27 '19 05:03

Finn


People also ask

What is rxjs and how do I use it with angular?

Reactive Extensions for JavaScript, or RxJS, is a JavaScript library that uses observables for reactive programming. It can be used with other JavaScript libraries and frameworks, and it integrates well into Angular. Today, we will discuss RxJS and Angular, the benefits of using RxJS in Angular, and how to use them together.

What is @Angular Universal and how to use it?

Angular Universal is an open-source project that extends the functionality of @angular/platform-server. The project makes server-side rendering possible in Angular. This article will discuss the issues and possible solutions we encountered while developing a real application with Angular Universal.

What is $interval in AngularJS?

$interval will repeat a function call or a block a specified number of times with a delay in-between. This is what I would like to do, written in AngularJS:

Is server-side rendering possible with Angular Universal?

The project makes server-side rendering possible in Angular. Another package Socket Engine is a framework-agnostic that theoretically allows any backend to be connected to an SSR server. This article will discuss the issues and possible solutions we encountered while developing a real application with Angular Universal and Express.


1 Answers

If you use angular universal, you need to use isPlatformBrowser to exclude code that can only run on the front end.

Eaxmple:

import { isPlatformBrowser } from '@angular/common';
import { PLATFORM_ID } from "@angular/core";

constructor(
    @Inject(PLATFORM_ID) private platformId: Object
  ) { }

do(){
    if (isPlatformBrowser(this.platformId)) {
      //do something, only runs on the front end
    }
}
like image 178
Finn Avatar answered Sep 24 '22 14:09

Finn