I am new in Angular2. I have tried to create a component but showing an error.
This is the app.component.ts
file.
import { Component } f
Maybe This is for name of html
tag component
You use in html
something like this <mycomponent></mycomponent>
You must use this <app-mycomponent></app-mycomponent>
In your components.module.ts you should import IonicModule like this:
import { IonicModule } from '@ionic/angular';
Then import IonicModule like this:
imports: [
CommonModule,
IonicModule
],
so your components.module.ts will be like this:
import { CommonModule } from '@angular/common';
import {PostComponent} from './post/post.component'
import { IonicModule } from '@ionic/angular';
@NgModule({
declarations: [PostComponent],
imports: [
CommonModule,
IonicModule
],
exports: [PostComponent]
})
export class ComponentsModule { }```
Go to the tsconfig.json file. write this code under angularCompilerOption:
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
**"enableIvy": false**
}
I am using Angular v11 and was facing this error while trying to lazy load a component (await import('./my-component.component')
) and even if import
and export
were correctly set.
I finally figured out that the solution was deleting the separate dedicated module's file and move the module content inside the component file itself.
rm -r my-component.module.ts
and add module inside my-component.ts
(same file)
@Component({
selector: 'app-my-component',
templateUrl: './my-component.page.html',
styleUrls: ['./my-component.page.scss'],
})
export class MyComponent {
}
@NgModule({
imports: [CommonModule],
declarations: [MyComponent],
})
export class MyComponentModule {}
Check your selector in your filename.component.ts
Using the tag in various html files I would say
<my-first-component></my-first-component>
Should be
<app-my-first-component></app-my-first-component>
Example
@Component({
selector: 'app-my-first-component',
templateUrl: './my-first-component.component.html',
styleUrls: ['./my-first-component.component.scss']
})
You must declare your MyComponentComponent in the same module of your AppComponent.
import { AppComponent } from '...';
import { MyComponentComponent } from '...';
@NgModule({
declarations: [ AppComponent, MyComponentComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {}