在 TypeScript 中设置 Image Src
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40359561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Image Src in TypeScript
提问by Luca Trembon
I'm using ionic 2 and want to load a picture during runtime. In the past projects I always did it like this:
我正在使用 ionic 2 并希望在运行时加载图片。在过去的项目中,我总是这样做:
bikeImage.src = "data:image/jpeg;base64," + bikeResult.image;
But it doesn't work in TypeScript.
但它在 TypeScript 中不起作用。
My HTML looks like this.
我的 HTML 看起来像这样。
<img id='bikeImage'/>
It says, that src is a unresolved variable.
它说, src 是一个未解析的变量。
I thought that every JavaScript code works in TypeScript as well. Am I wrong?
我认为每个 JavaScript 代码也适用于 TypeScript。我错了吗?
Does anybody have an idea how to fix this?
有人知道如何解决这个问题吗?
Thanks in advance.
提前致谢。
回答by Nitzan Tomer
You haven't posted how you get element into bikeImage
, but it should be:
您还没有发布如何将元素放入bikeImage
,但它应该是:
bikeImage = document.getElementById("bikeImage") as HTMLImageElement;
If you don't type assert it to HTMLImageElementthen it will be just an HTMLElementwhich does not have the src
property.
如果你不输入 assert 到HTMLImageElement那么它将只是一个没有属性的HTMLElementsrc
。