Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

向 Vue 中的 img 元素动态传入 src 值 #161

Closed
Dream4ever opened this issue Jul 1, 2021 · 0 comments
Closed

向 Vue 中的 img 元素动态传入 src 值 #161

Dream4ever opened this issue Jul 1, 2021 · 0 comments
Labels
Vue Powerful framework

Comments

@Dream4ever
Copy link
Owner

需求描述

如标题

解决过程

Google 关键字 vue dynamic bind image src,第一个 Stack Overflow 的链接中就有答案:Vue.js dynamic images not working

具体代码很简单,用下面的方法来动态生成图片的地址,关键是返回的图片地址要用 require() 给包起来。

topThree(index) {
    return require(`@/assets/game1/img/rank/rank-${index}.png`)
}

然后在 Vue 的 template 中动态绑定到 img 元素的 src 属性上即可:

<img :src="topThree(index + 1)">
@Dream4ever Dream4ever added the Vue Powerful framework label Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Vue Powerful framework
Projects
None yet
Development

No branches or pull requests

1 participant