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

Check Platform before call createCustomElement #26

Open
gogoout opened this issue Jul 16, 2020 · 1 comment
Open

Check Platform before call createCustomElement #26

gogoout opened this issue Jul 16, 2020 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@gogoout
Copy link

gogoout commented Jul 16, 2020

Including this library in a SSR project will break it. See angular/angular#24551

The solution is quite simple, just check platform before call createCustomElement

ngx-element.module.ts https://github.com/brunob15/ngx-element/blob/master/projects/ngx-element/src/lib/ngx-element.module.ts

import { NgModule, Injector, ModuleWithProviders, Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { NgxElementComponent } from './ngx-element.component';
import { LAZY_CMPS_PATH_TOKEN } from './tokens';

@NgModule({
	declarations: [NgxElementComponent],
	entryComponents: [NgxElementComponent]
})
export class NgxElementModule {

	constructor(private injector: Injector, @Inject(PLATFORM_ID) private platformId: string) {
                // conditional import angular/elements
		if (isPlatformBrowser(platformId)) {
			const { createCustomElement } = require('@angular/elements');
			const ngxElement = createCustomElement(NgxElementComponent, { injector });
			customElements.define('ngx-element', ngxElement);
		}
	}

	static forRoot(modulePaths: any[]): ModuleWithProviders<NgxElementModule> {
		return {
			ngModule: NgxElementModule,
			providers: [
				{
					provide: LAZY_CMPS_PATH_TOKEN,
					useValue: modulePaths
				}
			]
		};
	}

	ngDoBootstrap() {}
}
@PatrickJS
Copy link

LGTM

@brunob15 brunob15 added enhancement New feature or request help wanted Extra attention is needed labels Oct 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants