Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(component): fix the issue of SSR #1

Merged
merged 5 commits into from
Apr 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/components/angularx-qrcode/angularx-qrcode.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import {
Input,
OnChanges,
OnInit,
SimpleChange
SimpleChange,
Inject,
PLATFORM_ID,
AfterViewInit
} from '@angular/core';

import * as QRCode from 'qrcodejs2';
declare var require: any;
let QRCode: any;

import { isPlatformServer } from '@angular/common';

@Component({
selector: 'qrcode',
changeDetection: ChangeDetectionStrategy.OnPush,
template: ''
})

export class QRCodeComponent implements OnChanges, OnInit {
export class QRCodeComponent implements OnChanges, OnInit, AfterViewInit {

/** @internal */
@Input() public allowEmptyString: boolean = false;
Expand All @@ -31,10 +37,19 @@ export class QRCodeComponent implements OnChanges, OnInit {
public qrcode: any;

constructor(
public el: ElementRef
public el: ElementRef,
@Inject(PLATFORM_ID) private readonly platformId: any,
) { }

public ngOnInit() {
public ngOnInit() {}

public ngAfterViewInit() {
if (isPlatformServer(this.platformId)) {
return;
}
if (!QRCode) {
QRCode = require('qrcodejs2');
}
try {
if (!this.isValidQrCodeText(this.qrdata)) {
throw new Error('Empty QR Code data');
Expand Down