[英]Typescript (Ionic 3/Angular) string replace url
Trying to get this easy thing working but can't find it out... 试图让这件简单的事情起作用,但找不到它...
I've got this url that looks like this: 我有这个看起来像这样的网址:
https://website.com/image{width}x{height}.jpg
Now I need to replace the {width}
and the {height}
values. 现在,我需要替换{width}
和{height}
值。
I tried this function but I get the following error message all the time: 我尝试了此功能,但始终收到以下错误消息:
placeThumb(thumb:String){
thumb = thumb.replace('{width}', '300');
thumb = thumb.replace('{height}', '150');
return thumb;
}
The error code I'm getting is: 我得到的错误代码是:
Uncaught (in promise): TypeError: Cannot read property 'replace' of undefined 未捕获(承诺):TypeError:无法读取未定义的属性“替换”
TypeError: Cannot read property 'replace' of undefined at PlayerPage.webpackJsonp.109.PlayerPage.placeThumb ( http://localhost:8100/build/main.js:202:23 ) TypeError:无法读取PlayerPage.webpackJsonp.109.PlayerPage.placeThumb( http:// localhost:8100 / build / main.js:202:23 )上未定义的属性“替换”
at Object.eval [as updateRenderer] (ng:///AppModule/PlayerPage.ngfactory.js:166:26) 在Object.eval [作为updateRenderer](ng:///AppModule/PlayerPage.ngfactory.js:166:26)
at Object.debugUpdateRenderer [as updateRenderer] ( http://localhost:8100/build/vendor.js:15109:21 ) 在Object.debugUpdateRenderer [作为updateRenderer]( http:// localhost:8100 / build / vendor.js:15109:21 )
at checkAndUpdateView ( http://localhost:8100/build/vendor.js:14223:14 ) 在checkAndUpdateView( http:// localhost:8100 / build / vendor.js:14223:14 )
at callViewAction ( http://localhost:8100/build/vendor.js:14569:21 ) 在callViewAction( http:// localhost:8100 / build / vendor.js:14569:21 )
at execComponentViewsAction ( http://localhost:8100/build/vendor.js:14501:13 ) 在execComponentViewsAction( http:// localhost:8100 / build / vendor.js:14501:13 )
at checkAndUpdateView ( http://localhost:8100/build/vendor.js:14224:5 ) 在checkAndUpdateView( http:// localhost:8100 / build / vendor.js:14224:5 )
at callWithDebugContext ( http://localhost:8100/build/vendor.js:15472:42 ) 在callWithDebugContext( http:// localhost:8100 / build / vendor.js:15472:42 )
at Object.debugCheckAndUpdateView [as checkAndUpdateView] ( http://localhost:8100/build/vendor.js:15009:12 ) 在Object.debugCheckAndUpdateView [作为checkAndUpdateView]( http:// localhost:8100 / build / vendor.js:15009:12 )
at ViewRef_.detectChanges ( http://localhost:8100/build/vendor.js:11993:22 ) 在ViewRef_.detectChanges( http:// localhost:8100 / build / vendor.js:11993:22 )
I think it should be really simple but maybe I am missing something. 我认为这应该非常简单,但也许我缺少了一些东西。
HTML is looking as follows: HTML看起来如下:
<ion-card>
<img [src]="placeThumb(playerData.twitch_thumb)"/>
<ion-card-content>
<ion-card-title>
<ion-icon name="logo-twitch" class="iconblink"></ion-icon> Streaming: {{playerData.twitch_title}}
</ion-card-title>
<p>
{{ playerData.twitch_thumb }}
{{ placeThumb(playerData.twitch_thumb) }}
</p>
</ion-card-content>
</ion-card>
Not really sure what the issue was. 不太确定问题是什么。 Because this code only have to load once on page load I managed to run the function on the ts file and assign it to a variable. 因为此代码仅需在页面加载时加载一次,所以我设法在ts文件上运行该函数并将其分配给变量。
So from .TS i'm running it like: 所以从.TS我运行它像:
twitchThumb:any;
getPlayerInfo(){
let twthumb = 'https://website.com/image{width}x{height}.jpg';
this.twitchThumb = this.placeThumb(twthumb);
}
placeThumb(thumb){
thumb = thumb.replace('{width}', '300');
thumb = thumb.replace('{height}', '150');
return thumb;
}
And on the HTML I simply call {{ twitchThumb }}
在HTML上,我只需调用{{ twitchThumb }}
Thanks all for the feedback! 感谢大家的反馈!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.