gpt4 book ai didi

javascript - 如何替换 vue.js 中的斜杠

转载 作者:行者123 更新时间:2023-12-02 01:51:21 24 4
gpt4 key购买 nike

我的代码有一点问题:

<template slot="popover">
<img :src="'img/articles/' + item.id + '_1.jpg'">
</template>

我的一些 item.id 编号中带有斜线。因此,某些图像无法显示。现在,如果 item.id 数字中出现斜杠,我想用下划线替换斜杠或将其删除。有没有简单的解决方案?

斜杠只能在代码中的此时替换,而不能在使用 item.id 的其他地方替换。

最佳答案

您可以使用计算属性,并使用 replace 和正则表达式将斜杠替换为破折号:

<template slot="popover">
<img :src="`img/articles/${itemId}_1.jpg`">
</template>

<script>
...
computed: {
itemId: function(){
return this.item.replace(/\//g, '-');
}
}
...
</script>

这是一个测试 fiddle :https://jsfiddle.net/pqfvba6n/

关于javascript - 如何替换 vue.js 中的斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70303973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com