It is recommended to include the require statement in the data instead of directly in the html template.
This approach is beneficial because Webpack scans all require statements and resolves them during compilation, rather than at runtime. This enables webpack to eliminate unused images from your build or even convert them into data urls.
<div class="card" :style="{ background: 'url(' + image + ')',}">
export default {
data() {
return {
image: require("./assets/dance.jpg"),
};
}
};
Note that requires are resolved based on the path of your .vue
file, not the project root. If you begin the path with @/
, you can reference files from the src
directory.