AngularUniversal仅显示CSS导入而没有HTML内容甚至是meta标签
创始人
2024-10-29 21:00:53
0

这个问题通常是由于Angular Universal的配置错误引起的。确保正确地设置了应用程序的通用配置以确保正确渲染HTML内容和meta标签。

以下是一个可能的解决方案,使用Angular CLI中默认生成的应用程序为例:

  1. 在根目录中安装@nguniversal/express-engine和@nguniversal/builders:

    npm install --save @nguniversal/express-engine @nguniversal/builders
    
  2. 在angular.json文件中的server项目中添加Universal配置。替换项目名称和路径为你自己的。例如:

    "server": {
       "builder": "@angular-devkit/build-angular:server",
       "options": {
          "outputPath": "./dist/server",
          "main": "src/main.server.ts",
          "tsConfig": "tsconfig.server.json"
       },
       "configurations": {
          "production": {
             "fileReplacements": [
                {
                   "replace": "src/environments/environment.ts",
                   "with": "src/environments/environment.prod.ts"
                }
             ]
          },
          "universal": {
             "fileReplacements": [
                {
                   "replace": "src/environments/environment.ts",
                   "with": "src/environments/environment.universal.ts"
                }
             ],
             "optimization": true,
             "outputHashing": "all",
             "sourceMap": false,
             "extractCss": true,
             "namedChunks": false,
             "aot": true,
             "vendorChunk": false,
             "buildOptimizer": true,
             "platform": "server"
          }
       }
    }
    
  3. 在app.server.module.ts中,引入BrowserModule和ServerModule:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { ServerModule } from '@angular/platform-server';
    import { AppComponent } from './app.component';
    
    @NgModule({
       declarations: [
          AppComponent
       ],
       imports: [
          BrowserModule.withServerTransition({ appId: 'serverApp' }),
          ServerModule
       ],
       providers: [],
       bootstrap: [AppComponent]
    })
    export class AppServerModule { }
    
  4. 在environment.universal.ts中,添加以下代码:

    export const environment = {
       production: true,
       server: true
    };
    
  5. 在main.server.ts中,添加以下代码:

    import { enableProdMode } from '@angular/core';
    import { renderModuleFactory } from '@angular/platform-server';
    import { AppServerModuleNg

相关内容

热门资讯

透视游戏"广东闲来辅... 透视游戏"广东闲来辅助软件官方"开挂(工具)辅助工具有挂技巧-力荐教程;无需打开直接搜索薇:1367...
揭秘几款"wepok... wepoker高级辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,wepoker高级辅助作为一种...
盘点十款"温州茶苑手... 盘点十款"温州茶苑手机版辅助器"开挂(神器)辅助神器了解有挂-2026新版教程1、下载安装好温州茶苑...
必备科技"多乐够级辅... 必备科技"多乐够级辅助软件"开挂(透视)辅助透视新版有挂-安装教程【无需打开直接搜索加薇136704...
透视好友"中至余干六... 透视好友"中至余干六副里"开挂(软件)辅助软件详细教程-攻略方法 了解更多开挂安装加(1367043...
实测发现“好运娱乐辅助器”固有... 实测发现“好运娱乐辅助器”固有有辅助开挂平台(有挂透明挂);无需打开直接搜索加(薇:13670430...
透视线上“逗娱碰胡开挂”起初有... 透视线上“逗娱碰胡开挂”起初有开挂辅助插件(有挂实锤);无需打开直接搜索加薇136704302(咨询...
八分钟辅助“约局吧能不能开挂”... 八分钟辅助“约局吧能不能开挂”开挂(平台)辅助平台科技教程-有挂教程 【无需打开直接搜索加薇1367...
1.9分钟了解“wepoker... 1.9分钟了解“wepoker轻量版透视”原先有辅助下载(有挂详细) >>您好:软件加薇136704...
玩家必看!wejoker私人辅... 玩家必看!wejoker私人辅助软件,开心网辅助工具,AI教程(必看开挂辅助插件);打开点击测试直接...