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

ngOnDestroy throws error when in SSR #163

Closed
RomanLavrinenko opened this issue Aug 8, 2017 · 6 comments
Closed

ngOnDestroy throws error when in SSR #163

RomanLavrinenko opened this issue Aug 8, 2017 · 6 comments
Assignees
Labels
Milestone

Comments

@RomanLavrinenko
Copy link

Hey, first of all thanks a lot for your work! It's great!

Could you please provide SSR (universal) support?
I faced with next:
TypeError: Cannot read property 'removeChild' of undefined at DatePickerComponent.ngOnDestroy (webpack-internal:///77:272:29) at callProviderLifecycles (webpack-internal:///2:11392:18) at callElementProvidersLifecycles (webpack-internal:///2:11361:13) at callLifecycleHooksChildrenFirst (webpack-internal:///2:11345:17) at destroyView (webpack-internal:///2:12681:5) at callViewAction (webpack-internal:///2:12827:13) at execComponentViewsAction (webpack-internal:///2:12739:13) at destroyView (webpack-internal:///2:12680:5) at callViewAction (webpack-internal:///2:12827:13) at execComponentViewsAction (webpack-internal:///2:12739:13)

Looks like this happened because server try to execute 'removeChild' of nothing.

I resolved this by checking isPlatformBrowser(this.platformId) and render datepicker only in browser and all works, but yeah, when i get html from the server side, there is no datepicker and i can't see it on browser until browser bundle render datepicker. So there is some delay between getting page html and datepicker.

Probably this is not a big issue because this happens only if user go to page directly by ulr link, but SSR support would be a good investment.

Thanks

@vlio20
Copy link
Owner

vlio20 commented Aug 8, 2017

I would be more than happy to to add this capability, though I am not sure what is missed. Can you post some demo project for me to starting on?

@Hampfter
Copy link

I have a similar Problem. The quick and dirty solutuion would be to modify the code this way:

DatePickerComponent.prototype.ngOnDestroy = function () {
   this.handleInnerElementClickUnlisteners.forEach(function (ul) { return ul(); });
    if (this.appendChild)
      this.appendToElement.removeChild(this.calendarWrapper);

};

@HuChaoyong
Copy link

I got same error in my application.

 HistoricalDataContent.html:1 ERROR TypeError: Cannot read property 'removeChild' of undefined
at DatePickerComponent.webpackJsonp../node_modules/ng2-date-picker/date-picker/date-picker.component.js.DatePickerComponent.ngOnDestroy (date-picker.component.js:269)
at callProviderLifecycles (vendor.dll.js:11709)
at callElementProvidersLifecycles (vendor.dll.js:11678)
at callLifecycleHooksChildrenFirst (vendor.dll.js:11662)
at destroyView (vendor.dll.js:12923)
at callViewAction (vendor.dll.js:13069)
at execComponentViewsAction (vendor.dll.js:12981)
at destroyView (vendor.dll.js:12922)
at callWithDebugContext (vendor.dll.js:13748)
at Object.debugDestroyView [as destroyView] (vendor.dll.js:13302)

this is my main code

    import{ Component,
    ViewChild,
    ComponentFactoryResolver,
    ViewContainerRef,
    ReflectiveInjector} from '@angular/core';
 @ViewChild('content',{read: ViewContainerRef}) bodyContainerRef: ViewContainerRef;
 constructor ( ... ) { ... }
  changeContent(parameter) {
    // console.log(parameter);
    const WidgetFactory = this.componentFactoryResolver.resolveComponentFactory(parameter.component);
    this.bodyContainerRef.clear();
    const injector: Injector = ReflectiveInjector.resolveAndCreate([{
        provide: 'parameter',
        useValue: parameter.obj
    }]);
    const componentRef = this.bodyContainerRef.createComponent(WidgetFactory, 0, injector);
    // this.mainContentService.changed.emit(parameter.obj);
}

<dp-date-picker [(ngModel)]="startDate" [config]="datePickerConfig">startTime</dp-date-picker>
tag in the "parameter.component"
datePickerConfig = { locale: 'zh-cn', format: 'YYYY-MM-DD' };

when the app first load, and run methodchangeContent is OK, but when changeContent push another component, it will get this error.

@vlio20
Copy link
Owner

vlio20 commented Sep 1, 2017

The component wasn't built for SSR (yet) I will be glad for a PR.

@vlio20 vlio20 added this to the 2.6.0 milestone Oct 14, 2017
@vlio20 vlio20 self-assigned this Oct 14, 2017
@vlio20
Copy link
Owner

vlio20 commented Oct 14, 2017

@Hampfter, why is your solution considered dirty?

The problem is with ngAfterViewInit.

Solution example:
https://github.com/angular/material2/pull/6050/files

@vlio20 vlio20 modified the milestones: 2.6.0, 2.7.0 Oct 14, 2017
@vlio20 vlio20 modified the milestones: 2.7.0, 2.6.0 Oct 14, 2017
@vlio20 vlio20 changed the title Possibility of SSR support? ngOnDestroy throws error when in SSR Oct 21, 2017
vlio20 added a commit that referenced this issue Oct 21, 2017
fix for ngOnDestroy throws error when in SSR - resolves #163
@vlio20
Copy link
Owner

vlio20 commented Oct 21, 2017

resolved in c2a0c8b

@vlio20 vlio20 closed this as completed Oct 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants