我的代碼有一個小問題:
<template slot="popover">
<img :src="'img/articles/' item.id '_1.jpg'">
</template>
我的一些 item.id 編號中有斜杠。因此,某些影像無法顯示。現在,如果 item.id 編號中出現斜線,我想用下劃線替換斜線或將其洗掉。有沒有一個簡單的解決方案?
斜杠應該只在代碼中的這一點被替換,而不是在另一個使用 item.id 的地方。
uj5u.com熱心網友回復:
您可以使用計算屬性并用破折號replace和正則運算式替換斜線:
<template slot="popover">
<img :src="`img/articles/${itemId}_1.jpg`">
</template>
<script>
...
computed: {
itemId: function(){
return this.item.replace(/\//g, '-');
}
}
...
</script>
這是一個測驗小提琴:https : //jsfiddle.net/pqfvba6n/
uj5u.com熱心網友回復:
使用replace(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)
<template slot="popover">
<img :src="'img/articles/' item.id.replace(/\//g, '_') '_1.jpg'">
</template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/379070.html
標籤:javascript Vue.js
