admin管理员组文章数量:1336181
I am facing a weird problem with vueJs @error handler, what I want to
do is to hide the images with broken links, and display a placeholder instead, but if I have for examples two images and both of them have a broken links, only the first image of the broken link is displaying the placeholder, while the other image with a broken link is just displaying, the browser's broken image default logo
here's the code I did for testing, I know this is not a proper way of writing the code in Vue, but it was for testing purposes
<div id="app">
<img width="25%" id="img" src=".jpg" @error="handle()">
<img width="25%" id="img" src=".jpeg?auto=press&cs=tinysrgb&h=350" @error="handle()">
<img v-show="ifImageBroken" src="">
<p>{{brokenText}}</p>
<HelloWorld/>
</div>
<script>
import HelloWorld from "./ponents/HelloWorld";
export default {
name: "App",
data () {
return {
ifImageBroken: false,
brokenText: ''
}
},
ponents: {
HelloWorld
},
methods: {
handle : function() {
document.getElementById('img').style.display = 'none'
this.ifImageBroken = true;
this.brokenText = 'unable to load image'
}
}
};
</script>
I just wanted to know, if this @error
directive can handle multiple cases of broken images
I am facing a weird problem with vueJs @error handler, what I want to
do is to hide the images with broken links, and display a placeholder instead, but if I have for examples two images and both of them have a broken links, only the first image of the broken link is displaying the placeholder, while the other image with a broken link is just displaying, the browser's broken image default logo
here's the code I did for testing, I know this is not a proper way of writing the code in Vue, but it was for testing purposes
<div id="app">
<img width="25%" id="img" src="https://upload.wikimedia/wikipedia/mons/5/54/Wallpaper-img_0254.jpg" @error="handle()">
<img width="25%" id="img" src="https://images.pexels./photos/248797/pexels-photo-248797.jpeg?auto=press&cs=tinysrgb&h=350" @error="handle()">
<img v-show="ifImageBroken" src="https://via.placeholder./300">
<p>{{brokenText}}</p>
<HelloWorld/>
</div>
<script>
import HelloWorld from "./ponents/HelloWorld";
export default {
name: "App",
data () {
return {
ifImageBroken: false,
brokenText: ''
}
},
ponents: {
HelloWorld
},
methods: {
handle : function() {
document.getElementById('img').style.display = 'none'
this.ifImageBroken = true;
this.brokenText = 'unable to load image'
}
}
};
</script>
I just wanted to know, if this @error
directive can handle multiple cases of broken images
-
I think the problem may be that you have 2
img
elements with the sameid
value. Standard HTML doesn't allow this since id's need to be unique. If you have multiple elements with the sameid
, only (I believe) the first will work with thegetElementById
– T. Dirks Commented Nov 30, 2018 at 12:46
1 Answer
Reset to default 6I face the same problem and I use object
to solve it because @error depend on broken links but not broken images inside links, so I create something to switch between them
<object data="https://here the right image if not found will display the image inside img tag.jpg" type="image/png">
<img src="https://via.placeholder./300" alt="Not found image">
</object>
In the first one will check if :data
in object
found or not, if not found he will switch to <img>
tag and here you will put your placeholder image,
Update 2: I use your code and update it, I hope it work
<div id="app">
<img width="25%" id="img" src="https://upload.wikimedia/wikipedia/mons/5/54/Wallpaper-img_0254.jpg" @error="imgPlaceholder">
<img width="25%" id="img" src="https://images.pexels./photos/248797/pexels-photo-248797.jpeg?auto=press&cs=tinysrgb&h=350" @error="imgPlaceholder">
<p>{{brokenText}}</p>
<HelloWorld/>
</div>
<script>
import HelloWorld from "./ponents/HelloWorld";
export default {
name: "App",
data () {
return {
ifImageBroken: false,
brokenText: ''
}
},
ponents: {
HelloWorld
},
methods: {
imgPlaceholder(e) {
e.target.src = "https://via.placeholder./300"
}
}
};
</script>
Here I create a new method that takes an event and change current broken URL image with another one
本文标签: javascriptVueJs error for handling broken links in imagesStack Overflow
版权声明:本文标题:javascript - VueJs @error for handling broken links in images - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742401793a2468030.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论