我想在 v-for 中包装 Firebase 图像路径。

huangapple go评论63阅读模式
英文:

I want to wrap the firebase image path in v-for

问题

在这种情况下,您可以尝试以下方式:

v-img(:src="getImageSrc(item.id)")
methods: {
  async getImageSrc(id) {
    const skinId = this.$store.getters['skin/getSkinId']
    const state = this.$store.getters['skin/getSkinState']
    const storageRef = this.$fire.storage.ref()
    const imageRef = storageRef.child(`skin/${id}_${skinId}${state}.webp`)
    try {
      const url = await imageRef.getDownloadURL()
      return url
    } catch (error) {
      console.error(error)
      return '' // 或者返回一个默认图片URL或错误处理的URL
    }
  }
}

这里的主要更改是使用 async/await 来处理异步操作,并在 getImageSrc 方法中等待 getDownloadURL 的结果。如果出现错误,您可以选择返回一个默认图片URL或者一个表示错误的URL,以便在加载失败时显示备用内容。

英文:

I'm not a developer, so the question might look stupid. I ask for your understanding.

There is an existing code with v-for

v-tab-item(
  v-for="(item, index) in getCharacterSkinList"
  :key="index"
)
  v-img(:src="require('~/static/img/skin/' + item.id + '_' + getSkinId + getSkinState + '.webp')"

Current Changes

v-tab-item(
  v-for="(item, index) in getCharacterSkinList"
  :key="index"
)
  img(:src="imageSrc")


export default {
	mounted() {
		const skinId = this.$store.getters['skin/getSkinId']
		const state = this.$store.getters['skin/getSkinState']
		const storageRef = this.$fire.storage.ref()

		this.getCharacterSkinList.forEach((item, index) => {
			const imageRef = storageRef.child(
				`skin/${item.id}_${skinId}${state}.webp`
			)
			imageRef
				.getDownloadURL()
				.then((url) => {
					this.$set(this.imageSrc, index, url)
				})
				.catch((error) => {
					console.log(error, imageSrc)
				})
		})
		this.$store.dispatch('skin/asyncCharacterSkin')
	},
}

in this case, getters works successfully.
but failed to load item.id.

I tried

v-img(:src="getImageSrc(item.id)")


methods: {
	getImageSrc(id) {
		const skinId = this.$store.getters['skin/getSkinId']
		const state = this.$store.getters['skin/getSkinState']
		const storageRef = this.$fire.storage.ref()
		const imageRef = storageRef.child(`skin/${id}_${skinId}${state}.webp`)
		return imageRef.getDownloadURL()
	},
}

I tried to change it to method, but it did not work because returned prosmised.

What other ways can I try?

答案1

得分: 1

getDownloadURL()是一个异步方法,因此你应该将你的方法声明为async,并使用await,如下(未经测试):

async getImageSrc(id) {
    const skinId = this.$store.getters['skin/getSkinId']
    const state = this.$store.getters['skin/getSkinState']
    const storageRef = this.$fire.storage.ref()
    const imageRef = storageRef.child(`skin/${id}_${skinId}${state}.webp`)
    const downloadURL = await imageRef.getDownloadURL();
    return downloadURL;
}
英文:

getDownloadURL() is an asynchronous method so you should declare your method as async and use await, as follows (untested):

async getImageSrc(id) {
    const skinId = this.$store.getters['skin/getSkinId']
    const state = this.$store.getters['skin/getSkinState']
    const storageRef = this.$fire.storage.ref()
    const imageRef = storageRef.child(`skin/${id}_${skinId}${state}.webp`)
    const downloadURL = await imageRef.getDownloadURL();
    return downloadURL;
},

huangapple
  • 本文由 发表于 2023年2月19日 03:36:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定