【问题标题】:What is the right way to set a image route in Angular?在 Angular 中设置图像路由的正确方法是什么?
【发布时间】:2020-04-30 18:21:36
【问题描述】:

我从 API 调用数据,其中包括图像但不是全部,因此我创建了一个管道来处理没有图像的数据。


    import { Pipe, PipeTransform } from '@angular/core';
    @Pipe({
      name: 'noImg'
    })
    export class NoImgPipe implements PipeTransform {

      transform(image: any): string {

        if (image === null ) {

          return '/assets/Images/no-img.jpg' ;

          //  Regresa la imagen por defecto no images

        } else {
          return image;
        }
      }

    }

这是我的管道指向的文件

它在本地主机上运行良好,但是当我使用 ng build --prod 编译项目并托管我的应用程序时,管道不起作用。

我还在路由中激活了 useHash 并删除了 /in the baseHref=""

【问题讨论】:

  • “管道不工作”不是很具体。您的浏览器开发控制台中是否有一些错误或请求的网络问题?
  • 去掉前面的/
  • 我的意思是,在生产中,当我托管应用程序时,它不会显示 no-image.jpg 而是经典的无图像 html 图标,控制台中没有错误,它编译得很好。

标签: angular typescript localhost pipe web-hosting


【解决方案1】:

您应该使用 / 加注星标,然后使用 assets 文件夹

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 2022-06-28
    相关资源
    最近更新 更多