Angular JS 到 Angular 18 的迁移

问题描述 投票:0回答:1

我正在尝试创建一个混合 Angular JS Angular 18 应用程序,起初该应用程序使用此设置运行良好:

升级模块.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 文件:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/upgrade-module';

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

这在我的index.html中:

<body>
  <div ui-view></div>
</body>

但是,我尝试添加 Angular 18 组件来测试它们是否也可以在混合应用程序中工作,如下所示:

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 });
  }
}

路线:

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>

angularjs组件的模板是:

<div ui-view></div>

但现在什么也没有渲染

javascript angular angularjs typescript routes
1个回答
0
投票

难道不应该吗

<body>
  <app-root></app-root>
</body>

这将渲染 body 标签内的角度分量。

我们还需要在模块中导入

app.component
并引导它。

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 });
  }
}

最后在 app.component.html 中它应该包含路由器出口。

您必须创建另一个组件来替换您在路线上定义的组件

hello

...
export const routes: Routes = [
    { path: '', redirectTo: '/hello', pathMatch: 'full' },
    { path: 'hello', component: SomeComponent },
    { path: '**', component: AngularJsComponentComponent }
];
© www.soinside.com 2019 - 2024. All rights reserved.