Vue3項(xiàng)目使用指令方式修改img標(biāo)簽的src地址的幾種方法

因?yàn)轫?xiàng)目要添加一個(gè)新的功能,就是在pad上的網(wǎng)頁(yè)需要打開(kāi)攝像頭掃碼的能力,而且是在局域網(wǎng)訪問(wèn)的,所以就需要后端地址啟動(dòng)在https的地址上,這個(gè)時(shí)候所有的靜態(tài)資源都變成了https鏈接,這個(gè)時(shí)候一個(gè)unity同事在游戲中訪問(wèn)圖片地址的時(shí)候就訪問(wèn)不了,有解決方案,但是要修改的地方有點(diǎn)多,但是掃碼又必須使用https的鏈接,所以備選方案就是后端啟動(dòng)兩個(gè)服務(wù),一個(gè)http轉(zhuǎn)為給游戲提供http的圖片鏈接,https轉(zhuǎn)為pad提供掃碼能力。
但是這個(gè)時(shí)候問(wèn)題就來(lái)了:pad + server + game 三者之間的通訊是使用proto協(xié)議傳輸?shù)模渲芯桶瑘D片地址,這個(gè)圖片地址是只有http鏈接字段的,不想再添加一個(gè)https的字段,會(huì)很麻煩,添加的話大家所有人都要修改代碼,這個(gè)就只有我這個(gè)前端自己來(lái)解決了,那就是將所有圖片鏈接中http替換為https,并將端口9091替換為9090,那全局那么多img標(biāo)簽?zāi)?,怎么做?/p>
方案1:全局組件替換
創(chuàng)建一個(gè)全局的圖片組建,然后將項(xiàng)目中的所有img標(biāo)簽改成這個(gè)全局組件,不推薦:因?yàn)橐獎(jiǎng)?chuàng)建函數(shù)和全局組件,還要修改項(xiàng)目中所有的img標(biāo)簽,麻煩
<!-- components/SecureImg.vue -->
<template>
<img :src="secureSrc" v-bind="$attrs">
</template>
<script setup>
import { computed } from 'vue'
const props = defineProps({
src: String
})
const secureSrc = computed(() => {
if (!props.src) return ''
return props.src.replace(/^http:\/\//i, 'https://')
})
</script>然后在項(xiàng)目中全局注冊(cè):
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import SecureImg from './components/SecureImg.vue'
const app = createApp(App)
app.component('Img', SecureImg)
app.mount('#app')方案2:使用Vue指令
創(chuàng)建一個(gè)指令,然后將所有img標(biāo)簽的src屬性改為使用指令的方式。這個(gè)時(shí)候要改的就簡(jiǎn)單了,就是全局搜索<img src= 然后替換為 <img v-secure-src=即可實(shí)現(xiàn)。
// directives/secureSrc.js
export const secureSrc = {
beforeMount(el, binding) {
if (binding.value && typeof binding.value === 'string') {
el.src = binding.value.replace(/^http:\/\//i, 'https://')
}
},
updated(el, binding) {
if (binding.value && typeof binding.value === 'string') {
el.src = binding.value.replace(/^http:\/\//i, 'https://')
}
}
}
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import { secureSrc } from './directives/secureSrc'
const app = createApp(App)
app.directive('secure-src', secureSrc)
app.mount('#app')如果img的src跟的是變量,直接使用指令跟上變量就可以了:
<template>
<div>
<!-- 靜態(tài)字符串 -->
<img v-secure-src="'http://example.com/image1.jpg'" alt="image1">
<!-- 動(dòng)態(tài)變量 -->
<img v-secure-src="imageUrl" alt="dynamic image">
<!-- 響應(yīng)式數(shù)據(jù) -->
<img v-secure-src="user.avatar" alt="user avatar">
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
// 響應(yīng)式數(shù)據(jù)
const imageUrl = ref('http://example.com/image2.jpg')
const user = reactive({
avatar: 'http://example.com/avatar.jpg'
})
// 可以動(dòng)態(tài)更新
setTimeout(() => {
imageUrl.value = 'http://another-domain.com/new-image.jpg'
}, 3000)
</script>方案3:使用指令參數(shù)
可以通過(guò)參數(shù)的形式,將url地址傳遞到指令里面,然后在指令里面修改:
// directives/secureSrc.js
export const secureSrc = {
beforeMount(el, binding) {
if (binding.arg === 'url') {
updateSrc(el, binding.value)
}
},
updated(el, binding) {
if (binding.arg === 'url') {
updateSrc(el, binding.value)
}
}
}
function updateSrc(el, src) {
if (src && typeof src === 'string') {
el.src = src.replace(/^http:\/\//i, 'https://')
} else {
el.src = src || ''
}
}使用方式:
<template>
<div>
<!-- 使用指令參數(shù) -->
<img v-secure-src:url="dynamicImageUrl" alt="dynamic image">
<img v-secure-src:url="staticImageUrl" alt="static image">
</div>
</template>
<script setup>
import { ref } from 'vue'
const dynamicImageUrl = ref('http://example.com/dynamic.jpg')
const staticImageUrl = 'http://example.com/static.jpg'
</script>方案4:支持修飾符的指令
// directives/secureSrc.js
export const secureSrc = {
beforeMount(el, binding) {
processSrc(el, binding)
},
updated(el, binding) {
processSrc(el, binding)
}
}
function processSrc(el, binding) {
const src = binding.value
let processedSrc = src
if (src && typeof src === 'string') {
// 強(qiáng)制HTTPS修飾符
if (binding.modifiers.https) {
processedSrc = src.replace(/^http:\/\//i, 'https://')
}
// 添加時(shí)間戳修飾符(避免緩存)
if (binding.modifiers.nocache) {
const separator = processedSrc.includes('?') ? '&' : '?'
processedSrc += `${separator}_t=${Date.now()}`
}
}
el.src = processedSrc || ''
}使用方式:
<template>
<div>
<!-- 只啟用HTTPS轉(zhuǎn)換 -->
<img v-secure-src.https="imageUrl" alt="https only">
<!-- 同時(shí)啟用HTTPS和去緩存 -->
<img v-secure-src.https.nocache="dynamicImage" alt="https and nocache">
</div>
</template>
<script setup>
import { ref } from 'vue'
const imageUrl = ref('http://example.com/image.jpg')
const dynamicImage = ref('http://example.com/dynamic.jpg')
</script>推薦方案
使用方法3(指令參數(shù)) 最為清晰,因?yàn)椋?/p>
語(yǔ)義明確:v-secure-src:url 清楚地表示這是處理URL的
避免沖突:不會(huì)影響其他屬性的處理
易于擴(kuò)展:可以添加其他參數(shù)處理不同場(chǎng)景
這樣無(wú)論src是靜態(tài)字符串還是動(dòng)態(tài)變量,都能正確工作,并且在數(shù)據(jù)更新時(shí)自動(dòng)響應(yīng)變化。
以上就是Vue3項(xiàng)目使用指令方式修改img標(biāo)簽的src地址的幾種方法的詳細(xì)內(nèi)容,更多關(guān)于Vue3使用指令方式修改img的src地址的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于Vue實(shí)現(xiàn)鼠標(biāo)滾動(dòng)輪控制頁(yè)面橫向滑動(dòng)效果
這篇文章主要介紹了如何基于Vue實(shí)現(xiàn)鼠標(biāo)滾動(dòng)輪控制頁(yè)面橫向滑動(dòng)效果,文中通過(guò)代碼示例和圖文結(jié)合的方式給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-09-09
vue最強(qiáng)table vxe-table 虛擬滾動(dòng)列表 前端導(dǎo)出問(wèn)題分析
最近遇到個(gè)問(wèn)題,后臺(tái)一次性返回2萬(wàn)條列表數(shù)據(jù)并且需求要求所有數(shù)據(jù)必須全部展示,不能做假分頁(yè),怎么操作呢,下面通過(guò)本文介紹下vue最強(qiáng)table vxe-table 虛擬滾動(dòng)列表 前端導(dǎo)出問(wèn)題,感興趣的朋友一起看看吧2023-10-10
在vue中created、mounted等方法使用小結(jié)
這篇文章主要介紹了在vue中created、mounted等方法使用小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Vue+ElementUI容器無(wú)法鋪滿網(wǎng)頁(yè)的問(wèn)題解決
這篇文章主要介紹了Vue+ElementUI容器無(wú)法鋪滿網(wǎng)頁(yè)的問(wèn)題解決,文章通過(guò)圖文結(jié)合的方式給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08
Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié)
下面小編就為大家分享一篇Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

