Angularngx-translate图片无法显示
创始人
2024-10-28 18:01:00
0
  1. 确认图片路径是否正确,可以尝试直接访问图片路径查看是否能够正常加载图片。
  2. 在ngx-translate中加载图片时,需要使用translateService.get()方法,如下所示:

HTML模板代码: My Image

组件代码: import { Component, OnInit } from '@angular/core'; import { TranslateService } from '@ngx-translate/core';

@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'] }) export class MyComponent implements OnInit {

constructor(private translateService: TranslateService) { }

ngOnInit() { this.translateService.setTranslation('en', { MY_IMAGE: '~/assets/images/my-image.png' }); } }

  1. 如果图片仍然无法显示,可以尝试使用绝对路径来加载图片。

HTML模板代码:

组件代码: import { Component, OnInit } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { DomSanitizer } from '@angular/platform-browser';

@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'] }) export class MyComponent implements OnInit {

constructor(private translateService: TranslateService, private sanitizer: DomSanitizer) { }

ngOnInit() { this.translateService.setTranslation('en', { MY_IMAGE: '/assets/images/my-image.png' }); }

getImageUrl(): any { const imageUrl = this.translateService.instant('MY_IMAGE'); return this.sanitizer.bypassSecurityTrustUrl(imageUrl); }

} 注意:这种做法需要使用DomSanitizer来避免XSS攻击。

相关内容

热门资讯

我来分享!掌中乐游戏中心吗(辅... 我来分享!掌中乐游戏中心吗(辅助挂)一贯存在有挂(2024已更新)(哔哩哔哩);1、掌中乐游戏中心吗...
wepok软件透明挂!wepo... wepok软件透明挂!wepoke可以来好牌(透明挂)wepoKe(原来真的有挂)准备好在wepoK...
微扑克wpk透视辅助!微扑克职... 微扑克wpk透视辅助!微扑克职业代打,微扑克app(确实有挂辅助挂)1、快速入门:当你通过点击开始按...
重磅来袭!心动麻将有挂么(透视... 重磅来袭!心动麻将有挂么(透视)一贯真的是有挂(2022已更新)(哔哩哔哩)1、起透看视 心动麻将有...
微扑克ai辅助工具!微扑克线上... 微扑克ai辅助工具!微扑克线上代打,WPK果然存在有挂,教你教程(有挂软件)是一款可以让一直输的玩家...
wepokeai代打!wepo... wepokeai代打!wepower插件(辅助透视)Wepoke(都是真的有挂);是一款可以让一直输...
教程辅助!越乡游斗地主有挂吗(... 教程辅助!越乡游斗地主有挂吗(辅助)原来有挂(2026已更新)(哔哩哔哩)1、很好的工具软件,可以解...
微扑克有辅助挂!微扑克有脚本,... 微扑克有辅助挂!微扑克有脚本,微扑克有辅助挂,解密教程(有挂方法)1、每一步都需要思考,不同水平的挑...
aapoker透明挂!wepo... aapoker透明挂!wepoke是机器发牌,wepoKe果然真的有挂,规律教程(有挂详情)这是由厦...
玩家科普!开心十三张辅助ios... 玩家科普!开心十三张辅助ios版(辅助挂)其实是真的有挂(2026已更新)(哔哩哔哩)1、开心十三张...