Component imports must be standalone components, directives, pipes, or must be NgModules
Asked Answered
M

3

12

I want to use ngx-avatar in angular standalone component v14.

I use it in the template and I was imported it in the component imports.

But I got error message:

Component imports must be standalone components, directives, pipes, or must be NgModules.

stackblitz

import { AvatarModule } from 'ngx-avatar';

@Component({
  selector: 'my-app',
  standalone: true,
  imports: [AvatarModule, CommonModule],
  template: `
    app works!

    <ngx-avatar class="my-avatar" value="HM"> </ngx-avatar>
  `,
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;
}

I try to solve it using importProvidersFrom but it's not work:

bootstrapApplication(
  AppComponent, {
   providers: [importProvidersFrom(AvatarModule.forRoot())],
  }
);

Any idea how I can make it work?

Marasco answered 21/6, 2022 at 7:31 Comment(1)
If it is not a standalone component, I believe it has to be imported in a module.Autoxidation
C
3

I can solve this problem, importing a "SharedModule" with the dependencies you need, later if you need to add stuff, you can do it from this module.

The code of SharedModule, could be:

import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AvatarModule } from 'ngx-avatar';
@NgModule({
  exports: [AvatarModule],
  imports: [HttpClientModule, AvatarModule],
})
export class SharedModule {}

Finally you just have to import the SharedModule It works for me

Cocke answered 30/8, 2023 at 22:16 Comment(0)
S
0

From what I found while having a related problem, it seems like the module is malformed.

I'd would guess that the module needs to be compiled with Angular 14+ to be imported as standalone. ngx-avatar isn't since it hasn't been updated for over 2 years.

Susumu answered 8/1, 2023 at 12:14 Comment(0)
D
0

In app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template:`
    <h1>{{title}}</h1>
  `,
  styleUrls: ['./app.component.css'],
  standalone: true
})

export class AppComponent {
  title: string = 'Your title';
}
Dormancy answered 24/6, 2024 at 20:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.