skip to Main Content

Angular Material’s documentation says:

import { MatSlideToggleModule } from '@angular/material/slide-toggle';

@NgModule ({
  imports: [
    MatSlideToggleModule,
  ]
})
class AppModule {}

But Angular 17 doesn’t create app.module file by default. Is there any way to do that without app.module?

2

Answers


  1. Angular v17 onwards, standalone is default via CLI

    Which means when you create new project it wont have any modules in it

    As a workaround it is possible to create a module-based app by using the --no-standalone flag : ng new --no-standalone which will create a app.modules.ts in your project

    use this command :

    ng new myNewApp --no-standalone
    

    Follow it up with :

    ng add @angular/material 
    

    Then import relevent modules from angular material to your project app.module.ts :

    import { MatSlideToggleModule } from '@angular/material/slide-toggle';
    
    @NgModule ({
      imports: [
        MatSlideToggleModule,
      ]
    })
    class AppModule {}
    
    Login or Signup to reply.
  2. import dependencies into the component instead of app.modules, add attribute standalone:true , like the code below:

    import { MatSlideToggleModule } from '@angular/material/slide-toggle';
    @Component({
      standalone:true,
      imports: [
         MatSlideToggleModule,
       ],
       selector: 'your-component',
       templateUrl: './your.component.html',
       styleUrls: ['./your.component.scss']
     })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search