Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rendering the Angular AppComponent as Standalone?

Trying to "Switch" the Angular AppComponent to render as standalone after generating a new project using the CLI. This attempt compiles fine, but the template does not render.

Here are the steps:

ng new project
cd project

Then change the AppComponent to look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

@Component({
  standalone: true,
  imports: [RouterModule, CommonModule],
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'b';
}
bootstrapApplication(AppComponent);

Also remove AppComponent from app.module.ts since it’s now standalone.

Finally comment out this line in main.ts since we are bootstrapping the component.

/*
platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));
*/

If I now run ng serve -o the application compiles and boots, but the app.component.html template does not render. Thoughts?

>Solution :

Assuming the version of angular that you are using supports standalone components.

You’ll have to move bootstrapApplication(AppComponent); from AppComponent to main.ts, because it searches for this function in the entry point (main property) defined in the angluar.json.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading