Error in Angular - has no exported member 'NgModule', 'BrowserModule', 'RouterModule' - javascript

I am trying a simple angular project with Django. I have a working Django project. I am getting this error during compiling.
[0] app/app.module.ts(1,10): error TS2305: Module '"/user/blah-blah/angular/node_modules/#angular/core/index"' has no exported member 'NgModule'.
[0] app/app.module.ts(2,10): error TS2305: Module '"/user/blah-blah/angular/node_modules/#angular/platform-browser/index"' has no exported member 'BrowserModule'.
[0] app/app.module.ts(3,10): error TS2305: Module '"/user/blah-blah/angular/node_modules/#angular/router/index"' has no exported member 'RouterModule'.
app.module.ts:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { RouterModule } from '#angular/router';
import { AppComponent } from './app.component';
import { DepartmentListComponent } from './department-list.component';
import { EmployeeListComponent } from './employee-list.component';
#NgModule({
imports: [
BrowserModule,
RouterModule.forRoot([
{path: 'departments', component: DepartmentListComponent},
{path: 'employees', component: EmployeeListComponent}
])
],
declarations: [ AppComponent, DepartmentListComponent, EmployeeListComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
I have been following this tutorial for angular and this to use Django with Angular. According to one of the suggestions here in stack overflow, all rc4 has to be changed to rc5 in package.json but mine are all rc5 already. Where could I be possibly going wrong ?

Related

1. If 'selector' is an Angular component, then verify that it is part of this module

I can't register my component. If I set
<app-actions-button></app-actions-button>
This is work good.
But if I set
<app-actions-button (send)="some($event)"></app-actions-button>
i got error:
ERROR in src/app/test.component.html:162:1 - error NG8001: 'app-actions-button' is not a known element:
If 'app-actions-button' is an Angular component, then verify that it is part of this module.
If 'app-actions-button' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component to suppress this message.
Check my module:
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { ActionsButtonRoutingModule } from '../actions-button/actions-button-routing.module';
import { ActionsButtonComponent } from './actions-button.component';
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
import { ModalModule } from "ngx-bootstrap/modal";
#NgModule({
declarations: [ActionsButtonComponent],
imports: [
CommonModule,
ActionsButtonRoutingModule,
FormsModule, ReactiveFormsModule,
ActionsButtonComponent
],
exports: [ActionsButtonComponent],
})
export class ActionsButtonModule { }
Your component app-actions-button dose not have an #Output property and it fails to compile.
You can read more on how to implement it here in the #Output Angular docs

Angular 6 - Importing shared module for child modules

I'm trying to make a shared module, but it's don't want to work somehow.
The shared module looks like this:
import { ModuleWithProviders, NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { SharedMetaModule } from './shared-meta';
import { ApplicationState } from './state/application-state';
import { MilitaryTimePipe } from './pipes/military-time-pipe';
import { KeysPipe } from './pipes/object-pipe';
import { GirlsClass } from './advertisers/girls';
#NgModule({
imports: [CommonModule],
declarations: [
KeysPipe,
MilitaryTimePipe
],
exports: [
SharedMetaModule,
KeysPipe,
MilitaryTimePipe
],
providers: [ApplicationState]
})
export class SharedModule {
static forRoot(): ModuleWithProviders {
return { ngModule: SharedModule };
}
}
I have an app.module.ts like this:
import { SharedModule } from '#shared/shared.module';
#NgModule({
imports: [
...
SharedModule.forRoot(),
Then I have a profile-gallery.module.ts where a pipe from the shared module would be used.
If I don't import the shared module in the profile-gallery module I got this error:
The pipe 'keys' could not be found.
If I import the shared module to the profile-gallery module I got this error:
MetaModule already loaded; import in root module only.
How could work the shared module in this situation?
The purpose of a Shared module is to import it in all the required modules, more than once. So there is no need for the forRoot method that makes sure it's imported only once.
Remove the forRoot method entirely and import the module where needed:
import { SharedModule } from '#shared/shared.module';
#NgModule({
imports: [
...
SharedModule,

Angular 2 provider is not accessible in components

I am new on angular 2. I was following the legacy quick-start application, Tour of Heroes.
I created services as mentioned in the application.
I have a service HeroService. Which is used to pull all the heroes data.
I have included the HeroService at the app.module file as provider to be able to access it through out the application for all application.
My app.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { RouterModule } from '#angular/router';
import { AppComponent } from './app.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroesComponent } from './heroes.component';
import { HeroService } from './hero.service';
#NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot([
{
path: 'heroes',
component: HeroesComponent
}
])
],
declarations: [ AppComponent, HeroesComponent, HeroDetailComponent ],
providers: [ HeroService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
And my Component File where I want to Access the HeroService to get data is:
export class HeroesComponent implements OnInit {
constructor(private heroService: HeroService) { }
}
The problem is I am getting an error like this, when i build the project :
Cannot find name 'HeroService'.
I have followed all the steps correctly. If I import the HeroService in my HeroesComponent, it seems to work. But fails when not imported.
Am I missing some step. As far as I understood declaring the provider on app.module will register it to use throughout the application/components without need of importing it each time.
Please correct me If I am wrong somewhere.
You need to import inside the component as well
import { HeroService } from './hero.service';

Angular2 unable to import FormsModule with javascript

I am doing the Angular2 Heroes Tutorial:
https://angular.io/docs/ts/latest/tutorial/toh-pt1.html
in plain Javascript (not Typescript) and i am having trouble to Import the Forms Module as mentioned: (description is only for Typescript):
Before we can use two-way data binding for form inputs, we need to import the FormsModule package in our Angular module. We add it to the NgModule decorator's imports array. This array contains the list of external modules used by our application.
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { AppComponent } from './app.component';
#NgModule({
imports: [
BrowserModule,
FormsModule
],
declarations: [
AppComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
When i am adding the Forms module to my app.module.js import array, it fails to find the module:
zone.js:129 Uncaught Error: Unexpected value 'undefined' imported by the module 'class2'
Here is my app.module.js:
(function(app) {
app.AppModule =
ng.core.NgModule({
imports: [ ng.platformBrowser.BrowserModule, ng.common.FORM_DIRECTIVES],
declarations: [ app.AppComponent],
bootstrap: [ app.AppComponent ]
})
.Class({
constructor: function() {}
});
})(window.app || (window.app = {}));
in my node_modules folder the Forms module exists. If i remove "ng.common.FORM_DIRECTIVES" from the imports array, no error is thrown.
The Content of console.dir(ng) is:
Object
common:Object
compiler:Object
core:Object
coreTokens:Object
platformBrowser:Object
platformBrowserDynamic:Object
probe
:
inspectNativeElement(element /** TODO #9100 */)
__proto__:Object
The content of console.dir(ng.forms) is:
undefined
I am sorry, i found the error. As so often it was nothing having to do with typescript or angular, i just had to add the script tag in the index.html file to load the forms.umd.js:
<script src="../node_modules/#angular/forms/bundles/forms.umd.js"></script>
now i can import the Forms Module with the following code and i can use the ngModule functionality:
ng.core.NgModule({
imports: [ ng.platformBrowser.BrowserModule, ng.forms.FormsModule],
declarations: [ app.AppComponent],
bootstrap: [ app.AppComponent ]
})
Try using import { FORM_DIRECTIVES } from 'angular2/common';
Source: Angular 2 two way binding using ngModel is not working
EDIT:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FORM_DIRECTIVES } from 'angular2/common';
import { AppComponent } from './app.component';
#NgModule({
imports: [
BrowserModule,
FORM_DIRECTIVES
],
declarations: [
AppComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
If this doesn't work, post the console.dir(ng); again( with this code in use ).

Typescript compilation error TS2307: Cannot find module with angular2 rc5 and ngModule

I am trying to find out the reason for the following error.
app/src/app.module.ts(13,33): error TS2307: Cannot find module 'src/components/test/test.module
I am using Angular2 RC5 and created a feature module and imported it in app.module.ts file. I am using lazy loading of the module with the router.
app.module.ts looks like this
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { routing } from './app.routes';
/* App Root */
import { AppComponent } from './app.component';
/* Feature module */
import { TestModule } from 'src/components/test/test.module';
#NgModule({
imports: [ BrowserModule, routing ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
providers: []
})
export class AppModule { }
test.module.ts looks like this
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { TestComponent } from './test.component';
import { routing } from './test.routes';
#NgModule({
imports: [ CommonModule, routing ],
declarations: [ TestComponent ],
providers: []
})
export default class TestModule { }
app.routes.ts looks like
import { Routes, RouterModule } from '#angular/router';
export const routes: Routes = [
{
path: '',
redirectTo: '/test',
pathMatch: 'full'
},
{
path: 'test',
loadChildren: 'src/components/test/test.module'
}
];
export const routing = RouterModule.forRoot(routes);
test.routes.ts looks like
import { Routes, RouterModule } from '#angular/router';
import { TestComponent } from './test.component';
const routes: Routes = [
{ path: '',
component: TestComponent }
];
export const routing = RouterModule.forChild(routes);
Above error appears when I try to compile test.module.ts file with default keyword. If I remove it, error disappears. but of course, in that case, I won't be able to use lazy loading for feature module.
Does anyone come across this?
I see multiple errors in your code.
You are importing wrong module in your AppModule.
Your export class name of test.module.ts is TestModule, but you are importing DashboardModule in your AppModule.
Instead of:
import { DashboardModule } from 'src/components/test/test.module';
You should import:
import { TestModule } from 'src/components/test/test.module';
And, of course, you need to add TestModule to your AppModule imports.

Categories