Login

Language :
TitleAngular Universal 환경에서 document.referrer 개체를 사용하는 방법(팁)
How to use the document.referrer object in an Angular Universal environment.
WriterJi-Seob LeeWrite DateFeb 27 2019Modify DateDec 31 2025View Count7050

This is how to use the document.referrer object in an Angular Universal environment.

 

import { Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformServer } from '@angular/common';

@Component({
  selector: 'app-board-view',
  templateUrl: './board-view.component.html',
  styleUrls: ['./board-view.component.css']
})
export class BoardViewComponent implements OnInit {
  
  constructor(
    //....
    @Inject(PLATFORM_ID) private platformId
  ) { }

  ngOnInit() {
    let referrer = "";
    if (!isPlatformServer(this.platformId)) {
      referrer = document.referrer;
    }
  }

}

 

At runtime, according to the execution value of "isPlatformServer(this.platformId)",

in Angular Universal , which runs on a server, a document object of javascript is not referred to,

but only in the client's browser, the document object is referred to.

 

This allows the viewing of source from the server end to run normally,

and allows the browser client to program using the document.referrer object.

 

If you use a JavaScript document object in Angular Universal,

it will not be SSR (Server Side Rendering).

 

It's a little incomplete.

 

I came across this while surfing the internet,

 

if (typeof window !== "undefined") {
  referrer = document.referrer;
}

 

Without using PLATFORM_ID

It could be handled simply as above.

Comment

Name               Password 
Content
Check Password.

Please enter your password when registering your comment.


Privacy Policy