You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue.js导入图片后在methods中调用无法显示问题求助

Fixing Background Image Not Showing in Vue.js

Hey there! I see the issue you're running into—let's break this down and get that SVG showing up as a background image.

The Main Problem: String Literal vs Variable Interpolation

Your current code uses double quotes which treat this.image as plain text instead of evaluating it as a variable:

li.style.backgroundImage = "url('this.image')"

That's why the browser is trying to load http://127.0.0.1:8080/order/detail/29/this.image—it's taking the entire string literally, not recognizing this.image as your imported path.

Replace the double quotes with backticks () and wrap your variable in ${}` to interpolate its actual value:

li.style.backgroundImage = `url(${this.image})`

This will correctly insert the image path you imported instead of the text "this.image".

Solution 2: String Concatenation

If you prefer not to use template literals, you can concatenate the string manually to inject the variable:

li.style.backgroundImage = "url('" + this.image + "')"

Just make sure you close the quotes properly around the variable to avoid syntax errors.

Quick Checks to Rule Out Other Issues

  • Double-check your SVG path: Ensure @/assets/svg/xxx.svg points to the correct file (watch for typos, capitalization, or missing files). The @ alias in Vue usually maps to the src folder, which is set up by default in Vue CLI/Vite projects.
  • Verify the imported path: Log this.image to the console to see what path is being generated—if it's empty or looks invalid, your import might be misconfigured.
  • SVG compatibility: While less likely here, some SVGs with complex features might have issues as background images, but the path issue is the clear culprit in your case.

Give those fixes a try, and your background image should load correctly!

内容的提问来源于stack exchange,提问作者Xiaozhou Song

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:54:48