Angular Universal BrowserModule.withServerTransition已被弃用,有什么替代品?

huangapple go评论69阅读模式
英文:

Angular Universal BrowserModule.withServerTransition is deprecated, what is the replacement for this?

问题

我正在使用 Angular 16.0.0,并且使用 Angular Universal 服务器端渲染,但当我在我的应用模块中导入 BrowserModule.withServerTransition 时,它被标记为已弃用,有什么替代方法?

我的 app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { MatMenuModule } from '@angular/material/menu';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatCardModule } from '@angular/material/card';
import { HomeComponent } from './home/home.component';
import { MatTabsModule } from '@angular/material/tabs';
import { CoursesCardListComponent } from './courses-card-list/courses-card-list.component';
import { CourseComponent } from "./course/course.component";
import { MatDatepickerModule } from "@angular/material/datepicker";
import { MatDialogModule } from "@angular/material/dialog";
import { MatInputModule } from "@angular/material/input";
import { MatListModule } from "@angular/material/list";
import { MatPaginatorModule } from "@angular/material/paginator";
import { MatProgressSpinnerModule } from "@angular/material/progress-spinner";
import { MatSelectModule } from "@angular/material/select";
import { MatSidenavModule } from "@angular/material/sidenav";
import { MatSortModule } from "@angular/material/sort";
import { MatTableModule } from "@angular/material/table";
import { MatToolbarModule } from "@angular/material/toolbar";
import { CoursesService } from "./services/courses.service";
import { CourseResolver } from "./services/course.resolver";
import { CourseDialogComponent } from './course-dialog/course-dialog.component';
import { ReactiveFormsModule } from "@angular/forms";
import { HttpClientModule } from '@angular/common/http';
import { AboutComponent } from './about/about.component';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    CourseComponent,
    CoursesCardListComponent,
    CourseDialogComponent,
    AboutComponent,
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'serverApp' }),
    BrowserAnimationsModule,
    MatMenuModule,
    MatButtonModule,
    MatIconModule,
    MatCardModule,
    MatTabsModule,
    MatSidenavModule,
    MatListModule,
    MatToolbarModule,
    MatInputModule,
    MatTableModule,
    MatPaginatorModule,
    MatSortModule,
    MatProgressSpinnerModule,
    MatDialogModule,
    AppRoutingModule,
    MatSelectModule,
    MatDatepickerModule,
    ReactiveFormsModule,
    HttpClientModule
  ],
  providers: [
    CoursesService,
    CourseResolver
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

请注意,BrowserModule.withServerTransition 已经被弃用,可以替换为 BrowserModule 并在 AppModuleimports 数组中使用 BrowserTransferStateModule。希望这对你有所帮助。

package.json 文件部分未提供翻译,因为它包含了一些具体的依赖和脚本信息。

英文:

I am using Angular 16.0.0 and with Angular Universal server side rendering, but when I
Import BrowserModule.withServerTransition in my app module its marked as deprecated, what is the replacement for it ?

Angular Universal BrowserModule.withServerTransition已被弃用,有什么替代品?

my app.module.ts

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
import {MatMenuModule} from '@angular/material/menu';
import {MatButtonModule} from '@angular/material/button'
import {MatIconModule} from '@angular/material/icon';
import {MatCardModule} from '@angular/material/card';
import { HomeComponent } from './home/home.component';
import {MatTabsModule} from '@angular/material/tabs';
import { CoursesCardListComponent } from './courses-card-list/courses-card-list.component';
import {CourseComponent} from "./course/course.component";
import { MatDatepickerModule } from "@angular/material/datepicker";
import { MatDialogModule } from "@angular/material/dialog";
import { MatInputModule } from "@angular/material/input";
import { MatListModule } from "@angular/material/list";
import { MatPaginatorModule } from "@angular/material/paginator";
import { MatProgressSpinnerModule } from "@angular/material/progress-spinner";
import { MatSelectModule } from "@angular/material/select";
import { MatSidenavModule } from "@angular/material/sidenav";
import { MatSortModule } from "@angular/material/sort";
import { MatTableModule } from "@angular/material/table";
import { MatToolbarModule } from "@angular/material/toolbar";
import {CoursesService} from "./services/courses.service";
import {CourseResolver} from "./services/course.resolver";
import { CourseDialogComponent } from './course-dialog/course-dialog.component';
import {ReactiveFormsModule} from "@angular/forms";
import { HttpClientModule} from '@angular/common/http';
import {AboutComponent} from './about/about.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
CourseComponent,
CoursesCardListComponent,
CourseDialogComponent,
AboutComponent,
],
imports: [
BrowserModule.withServerTransition({ appId: 'serverApp' }),
//BrowserTransferStateModule,
BrowserAnimationsModule,
MatMenuModule,
MatButtonModule,
MatIconModule,
MatCardModule,
MatTabsModule,
MatSidenavModule,
MatListModule,
MatToolbarModule,
MatInputModule,
MatTableModule,
MatPaginatorModule,
MatSortModule,
MatProgressSpinnerModule,
MatDialogModule,
AppRoutingModule,
MatSelectModule,
MatDatepickerModule,
ReactiveFormsModule,
HttpClientModule
],
providers: [
CoursesService,
CourseResolver
],
bootstrap: [AppComponent]
})
export class AppModule {
}

package.json

{
"name": "angular-universal-course",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"serve:prerender": "http-server -c-1 dist/angular-universal-course/browser",
"dev:ssr": "ng run angular-universal-course:serve-ssr",
"serve:ssr": "node dist/angular-universal-course/server/main.js",
"build:ssr": "ng build --configuration production && ng run angular-universal-course:server:production",
"prerender": "ng run angular-universal-course:prerender  --routes routes.txt"
},
"private": true,
"dependencies": {
"@angular/animations": "^16.0.0",
"@angular/cdk": "^16.0.0",
"@angular/common": "^16.0.0",
"@angular/compiler": "^16.0.0",
"@angular/core": "^16.0.0",
"@angular/forms": "^16.0.0",
"@angular/material": "^16.0.0",
"@angular/platform-browser": "^16.0.0",
"@angular/platform-browser-dynamic": "^16.0.0",
"@angular/platform-server": "^16.0.0",
"@angular/router": "^16.0.0",
"@nguniversal/express-engine": "^16.0.0",
"@types/express": "^4.17.8",
"express": "^4.15.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.13.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^16.0.0",
"@angular/cli": "^16.0.0",
"@angular/compiler-cli": "^16.0.0",
"@nguniversal/builders": "^16.0.0",
"@types/jasmine": "~3.8.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^14.15.0",
"http-server": "^14.0.0",
"jasmine-core": "~3.8.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~6.3.2",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"ts-node": "~8.3.0",
"typescript": "~5.0.4"
}
}

答案1

得分: 5

如果您需要设置一个ID,请使用APP_ID InjectionToken

{ provide: APP_ID, useValue: 'serverApp' }

注意:如果您在页面上只引导一个Angular应用程序,您可以将其删除。只有在您有多个Angular应用程序时才需要指定它。

英文:

If you need to set an ID use the APP_ID InjectionToken :

{ provide: APP_ID, useValue: 'serverApp' }.

NB: If you bootstrap only one Angular app on your page, you can just remove it. Specifying is only necessary when you have multiple angular apps.

huangapple
  • 本文由 发表于 2023年6月12日 07:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定