I'm trying to create a hybrid Angular JS Angular 18 application, and at first the application was running fine with this setup:
upgrade-module.ts
import { DoBootstrap, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UpgradeModule } from '@angular/upgrade/static';
@NgModule({
imports: [
BrowserModule,
UpgradeModule
]
})
export class AppModule implements DoBootstrap {
constructor(private upgrade: UpgradeModule) { }
ngDoBootstrap() {
this.upgrade.bootstrap(document.body, ['myapp'], { strictDi: false });
}
}
main.ts file:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/upgrade-module';
platformBrowserDynamic().bootstrapModule(AppModule)
.catch((err) => console.error(err));
and this in my index.html:
<body>
<div ui-view></div>
</body>
However, I tried added angular 18 components to test if they could also work in the hybrid application like this:
import { DoBootstrap, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UpgradeModule } from '@angular/upgrade/static';
import { routes } from './app.routes'; // Ensure the correct path to your routes
import { RouterModule } from '@angular/router';
import { LocationUpgradeModule } from '@angular/common/upgrade';
// import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
UpgradeModule,
RouterModule.forRoot(routes),
LocationUpgradeModule.config({
useHash: true,
hashPrefix: '!'
})
]
})
export class AppModule implements DoBootstrap {
constructor(private upgrade: UpgradeModule) { }
ngDoBootstrap() {
this.upgrade.bootstrap(document.body, ['myapp'], { strictDi: false });
}
}
routes:
import { Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { AngularJsComponentComponent } from './angular-js-component/angular-js-component.component';
export const routes: Routes = [
{ path: '', redirectTo: '/hello', pathMatch: 'full' },
{ path: 'hello', component: AppComponent },
{ path: '**', component: AngularJsComponentComponent }
];
index.html:
<body>
<router-outlet></router-outlet>
</body>
the template of the angularjs component is:
<div ui-view></div>
but now nothing is being rendered
Shouldn't it be.
<body>
<app-root></app-root>
</body>
This will render the angular components inside the body tag.
Also we need to import the app.component
inside the module and bootstrap it.
import { DoBootstrap, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UpgradeModule } from '@angular/upgrade/static';
import { routes } from './app.routes'; // Ensure the correct path to your routes
import { RouterModule } from '@angular/router';
import { LocationUpgradeModule } from '@angular/common/upgrade';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
UpgradeModule,
RouterModule.forRoot(routes),
LocationUpgradeModule.config({
useHash: true,
hashPrefix: '!'
})
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule implements DoBootstrap {
constructor(private upgrade: UpgradeModule) { }
ngDoBootstrap() {
this.upgrade.bootstrap(document.body, ['myapp'], { strictDi: false });
}
}
Finally in the app.component.html it should contain the router-outlet.
You have to create another component that will serve as a replacement for the component you defined on route hello
.
...
export const routes: Routes = [
{ path: '', redirectTo: '/hello', pathMatch: 'full' },
{ path: 'hello', component: SomeComponent },
{ path: '**', component: AngularJsComponentComponent }
];