如何在 Angular 18.2.6 版本中显示图像?

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

使用 Angular 版本 18.2.6(注意:有以前的 SO 线程,但没有适用于此版本)。

我有一个独立组件

NavbarComponent
,我想在其中包含图像。组件的 HTML:

<mat-toolbar class="toolbar">

    <a routerLink="/">
        <img src="" alt="IPCC Logo">
    </a>

      <a routerLink="/">
        <span style="font-size: 20px">Inpatient Psychological Care Collaborative (IPCC)</span>
      </a>
    
      <div class="spacer"></div>

      @if (true) {
        <ul id="nav-items">
          <li><a routerLink="/mission">Mission & Values</a></li>
          <li><a routerLink="/about">About Us</a></li>
          <li><a routerLink="/listserv">Listserv</a></li>
        </ul>
      }

</mat-toolbar>

...以及组件的 TS:

import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';
import { MatToolbarModule } from '@angular/material/toolbar'

@Component({
  selector: 'app-navbar',
  standalone: true,
  imports: [
    RouterModule,
    MatToolbarModule
  ],
  templateUrl: './navbar.component.html',
  styleUrl: './navbar.component.css'
})
export class NavbarComponent {

}

图像存储在

/src/assets/images/logo.png
中,但代码编译时不显示,如下所示: enter image description here

如何在 Angular V.18.2.6 应用程序中显示本地图像?

更新#1

angular.json
文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ipcc": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "outputPath": "dist/ipcc",
            "index": "src/index.html",
            "browser": "src/main.ts",
            "polyfills": [
              "zone.js"
            ],
            "tsConfig": "tsconfig.app.json",
            "assets": [
              {
                "glob": "**/*",
                "input": "public"
              }
            ],
            "styles": [
              "@angular/material/prebuilt-themes/azure-blue.css",
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kB",
                  "maximumError": "1MB"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kB",
                  "maximumError": "4kB"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "optimization": false,
              "extractLicenses": false,
              "sourceMap": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "buildTarget": "ipcc:build:production"
            },
            "development": {
              "buildTarget": "ipcc:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n"
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": [
              "zone.js",
              "zone.js/testing"
            ],
            "tsConfig": "tsconfig.spec.json",
            "assets": [
              {
                "glob": "**/*",
                "input": "public"
              }
            ],
            "styles": [
              "@angular/material/prebuilt-themes/azure-blue.css",
              "src/styles.css"
            ],
            "scripts": []
          }
        }
      }
    }
  }
}
angular angular-material
1个回答
0
投票

在 angular.json 文件中进行以下更改

"assets": [
          {
            "glob": "**/*",
            "input": "public",
            "output": "/"
          },
          {
            "glob": "**/*",
            "input": "src/assets",
            "output": "/assets"
          }
        ],
© www.soinside.com 2019 - 2024. All rights reserved.