Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run a component method only in the client using Angular2 Universal?

I am writing an angular2 universal app. It has a d3 chart, but I was hoping to only render the d3 chart on the client side (browser) and not try to render it on the server. Is there an interface in angular2 universal that will only run a component method only on the client side?

i.e.

class ComponentWithChart implements OnInit, ngUniversalBrowser {

  elem;

  constructor( private viewContainerRef:ViewContainerRef) {}

  ngUniversalBrowserOnInit() {

    this.elem = this.viewContainerRef.element.nativeElement;

    d3.select(this.elem).append('div').style({
      'background-color':'yellow'
    });
  }
}

Is there anything like the example above that might allow me to only run the ngUniversalBrowser method only in the browser OnInit?

like image 772
Subtubes Avatar asked Jul 24 '16 06:07

Subtubes


People also ask

Does angular run on server or client?

Angular Universal executes on the server, generating static application pages that later get bootstrapped on the client. This means that the application generally renders more quickly, giving users a chance to view the application layout before it becomes fully interactive.

Is angular server-side or client-side?

Angular Universal executes on the server-side by generating static pages and later are sent to the client browser for display. Thus, Angular Universal renders the app more quickly and allows users to view the application's layout.

What is client-side rendering and server side rendering?

Client-side rendering manages the routing dynamically without refreshing the page every time a user requests a different route. But server-side rendering is able to display a fully populated page on the first load for any route of the website, whereas client-side rendering displays a blank page first.

Should I use angular universal?

A primary benefit for using Angular Universal is that it improves web crawler support for enhanced Search Engine Optimization (SEO). With traditional client-side rendered SPAs, anything that is not in that shell of an . html is all rendered by the JavaScript.


2 Answers

import { isBrowser } from 'angular2-universal';

isBrowser is a boolean that you can import in your component and then execute code coditionaly only if it's running on client.

if (isBrowser) {
     // this will not run on server
}
like image 53
hex Avatar answered Nov 02 '22 19:11

hex


I think i got an answer. But it's a complete hack, and i'm sure they weren't intending for you to do this.

I ran into a dead-end myself (which forced me to turn off angular-universal/prerendering). My loss might be your gain.

There are particular objects that aren't available when you are pre-rendering. Mainly, "window", ie, "document.window".

Why don't you try adding a conditional statement in your component that checks whether the object exists. If it doesn't, load up your yellow background. Otherwise, load as normal.

I'm not sure if this means your component will fail to refresh when the "true" client finishes loading. But i'm sure you can set something up to watch for "window" and make it happen.

ngInit() {  
    if (!window) {
      // yellow background placeholder
    } else
      // real plumbing
    }
}
like image 22
Ralph N Avatar answered Nov 02 '22 20:11

Ralph N