NgModule syntax with Ivy turned on
See original GitHub issueDescription
When having a simple Angular 9 app and do some logic in NgModule
I can not build/serve the app successfully.
🔬 Minimal Reproduction
- ng new <project name>
- Be on Angular 9
- Change
app.module
to:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
function getNgModuleData(){
return {
declarations: [AppComponent],
imports: [BrowserModule],
providers: [],
bootstrap: [AppComponent]
}
}
const { declarations, imports, providers, bootstrap } = getNgModuleData();
@NgModule({ declarations, imports, providers, bootstrap })
export class AppModule { }
🔥 Exception or Error
▶ yarn build
yarn run v1.13.0
$ ng build
ERROR in src/app/app.module.ts:17:13 - error NG1010: Expected array when reading the NgModule.declarations of AppModule
17 @NgModule({ declarations, imports, providers, bootstrap })
~~~~~~~~~~~~
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
What’s more: bootstrap
property is underlined with red line in my IDE saying that: Expression does not resolve to an array of class types or a class type
Workaround
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
function getNgModuleData(){
return {
declarations: [AppComponent],
imports: [BrowserModule],
providers: [],
bootstrap: [AppComponent]
}
}
const declarations = getNgModuleData().declarations;
const imports = getNgModuleData().imports;
const providers = getNgModuleData().providers;
const bootstrap = getNgModuleData().bootstrap;
@NgModule({ declarations, imports, providers, bootstrap })
export class AppModule { }
This works fine. The app can be built and served but still the red squiggly line under bootstrap is visible.
Also this:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
function getNgModuleData(){
return {
declarations: [AppComponent],
imports: [BrowserModule],
providers: [],
bootstrap: [AppComponent]
}
}
@NgModule({...getNgModuleData()})
export class AppModule { }
can be built with no errors but when serving(ng serve) I’m getting:
main.ts:12 Error: No ErrorHandler. Is platform module (BrowserModule) included?
at core.js:42781
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Object.onInvoke (core.js:41836)
at ZoneDelegate.invoke (zone-evergreen.js:363)
at Zone.run (zone-evergreen.js:123)
at NgZone.run (core.js:41609)
at PlatformRef.bootstrapModuleFactory (core.js:42770)
at core.js:42850
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Zone.run (zone-evergreen.js:123)
🌍 Your Environment
**Angular Version:**
{
"name": "ng-ex",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~9.1.4",
"@angular/common": "~9.1.4",
"@angular/compiler": "~9.1.4",
"@angular/core": "~9.1.4",
"@angular/forms": "~9.1.4",
"@angular/platform-browser": "~9.1.4",
"@angular/platform-browser-dynamic": "~9.1.4",
"@angular/router": "~9.1.4",
"rxjs": "~6.5.5",
"tslib": "^1.10.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.901.4",
"@angular/cli": "~9.1.4",
"@angular/compiler-cli": "~9.1.4",
"@angular/language-service": "~9.1.4",
"@types/node": "^12.11.1",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "^5.1.2",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.8.3"
}
}
Is it a bug? Is it something well known? I’m aware of this: https://github.com/angular/angular/issues/30840 but this issue does not cover all of the cases I’m experiencing in my example.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
How do I enable Ivy for Angular 8 or 9? - Stack Overflow
To start a new project with Ivy enabled, use the --enable-ivy flag with the ng new command: ng new shiny-ivy-app --enable-ivy.
Read more >Manually Lazy Load an Angular Module with ViewEngine and ...
Find out how to lazy load an NgModule compatible with ViewEngine and Ivy. ... (codename: Ivy), basically the part of Angular that turns...
Read more >What is Angular Ivy? | Ninja Squad
In Angular, when your write a component, you write the component in TypeScript and its template in HTML, augmented by Angular template syntax...
Read more >Entry components - Angular
An entry component is any component that Angular loads imperatively, (which means you're not referencing it in the template), by type. You specify...
Read more >Automatically upgrade lazy-loaded Angular modules for Ivy!
It did the job, but it was fairly magical, and it relied on a special string syntax, and some compiler wizardry in the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We special case functions that simply return an expression. Such as in this case. Anything more complicated is deemed a “DynamicValue” and there is not much we can do with it.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.