angular: can't get animation working with ng4 and lazy loading

I’m submitting a … (check one with “x”)

[ x] bug report => search github for a similar issue or PR before submitting (or maybe I missed something :/ )
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior Animation working in main module but not in lazy loaded module.

Expected behavior work everywhere if I load animation module in a shared module.

Minimal reproduction of the problem with instructions Can’t get Angular4 to work with lazy loading and animation. So I loaded BrowserAnimationsModule in my app-modules.ts and animation works fine in main module, but not in lazy loaded modules, so I tried to load BrowserAnimationsModulein my shared module as I get an error of :

 If you need access to common directives ```

This is my shared module:

    var sharedComponents = [Tabs, Tab, Infobox, Sliderpanel, Slideritem, SlideritemContent, PanelSplitMain, PanelSplitSide, PanelSplitContainer, ListToArrayPipe, FormatSecondsPipe, MatchBodyHeight, ScreenTemplate, BlurForwarder, DraggableList, AddContent, Loading,
        FontSelector, BlockPropContainer, BlockPropCommon, BlockPropHtml, BlockPropClock, BlockPropWeather, BlockPropInstagram, BlockPropJsonPlayer, BlockPropJsonItem, LivePreview, LocationMap, MediaPlayer, FilterModelPipe, SvgIcon,
        BlockPropScene, BlockPropCalendar, BlockPropSheets, BlockPropTwitter, BlockPropVideo, BlockPropImage, BlockPropLabel, BlockPropMrss, BlockPropLocation, BlockPropRss, BlockPropDigg, BlockPropFasterQ, BlockPropCollection, BlockPropQR, BlockPropYouTube, JsonEventGrid];
    
    @NgModule({
        imports: [CommonModule, FormsModule, HttpModule, JsonpModule, ReactiveFormsModule, ContextMenuModule, ChartModule, ReactiveFormsModule, ColorPickerModule, DropdownModule, RadioButtonModule, SimpleGridModule, Ng2Bs3ModalModule, AgmCoreModule, VgCoreModule, VgControlsModule, VgOverlayPlayModule, VgBufferingModule],
        exports: [CommonModule, FormsModule, HttpModule, JsonpModule, ReactiveFormsModule, ContextMenuModule, ChartModule, ColorPickerModule, DropdownModule, RadioButtonModule, SimpleGridModule, Ng2Bs3ModalModule, AgmCoreModule, VgCoreModule, VgControlsModule, VgOverlayPlayModule, VgBufferingModule, ...sharedComponents],
        entryComponents: [ScreenTemplate],
        declarations: [...sharedComponents]
    })
    
    export class SharedModule {
        static forRoot(): ModuleWithProviders {
            return {
                ngModule: SharedModule,
                providers: []
            };
        }
    }

and as soon as I add to it the BrowserAnimationsModule it will error out,

this is the project if you wish to look at: https://github.com/born2net/studioweb

@angular/cli: 1.0.0
node: 6.9.5
os: win32 x64
@angular/animations: 4.0.0
@angular/common: 4.0.0
@angular/compiler: 4.0.0
@angular/core: 4.0.0
@angular/forms: 4.0.0
@angular/http: 4.0.0
@angular/platform-browser: 4.0.0
@angular/platform-browser-dynamic: 4.0.0
@angular/router: 4.0.0
@angular/cli: 1.0.0
@angular/compiler-cli: 4.0.0
@angular/language-service: 4.0.0
@ngtools/webpack: 1.3.0

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 29 (2 by maintainers)

Most upvoted comments

http://angularjs.blogspot.com.by/2017/03/angular-400-now-available.html :

If you rely on Animations, import the new BrowserAnimationsModule from @angular/platform-browser/animations in your root NgModule.

does it say to import it in a shared module?

@Toxicable I am using angular 8 and I have done exactly as you have suggested. Just in AppModule. However, I cannot get the animations to work in my lazy loaded modules. Could you advice me on this matter? Thanks

@born2net no, that’ll break it, you’re not meant to import AnimationsModule anywhere else except the root as stated above

@Toxicable I ran into the same problem. Please follow the repro instructions:

  • Clone project https://github.com/racerhere/animation-lazy-error

  • npm install

  • ng serve

  • Go to http://localhost:4200/eager. Everything works when modules are eagerly loaded.

  • Go to http://localhost:4200/lazy. See console for

    Error: BrowserModule has already been loaded. If you need access to common directives such as NgIf and NgFor from a lazy loaded module, import CommonModule instead.

  • Comment the import of NoopAnimationsModule in src\app\shared\shared.module.ts (lines 6 and 15).

  • Go to http://localhost:4200/lazy. Works fine now.