Complete Guide to Static Asset References in Vue.js: From JavaScript to Templates

Nov 21, 2025 · Programming · 10 views · 7.8

Keywords: Vue.js | Static Assets | require function | webpack | Path Resolution

Abstract: This article provides a comprehensive analysis of correctly referencing static assets in Vue.js projects, focusing on using require() function in JavaScript code and @ alias in templates. Through practical code examples, it demonstrates how to solve 404 errors with Leaflet custom icons, and delves into Vue CLI's static asset handling mechanism, webpack configuration principles, and usage scenarios for the public folder.

Overview of Static Asset References in Vue.js

Correctly referencing static assets is a common but error-prone issue in Vue.js development. Vue CLI processes static assets through webpack build tool, and understanding its working mechanism is crucial for avoiding 404 errors.

Static Asset References in JavaScript

Referencing static assets in JavaScript code requires using the require() function, which is webpack's standard approach for handling module dependencies. Taking custom icons in Leaflet mapping library as an example, incorrect referencing methods will lead to 404 errors:

// Incorrect example - using relative path directly
var icon = L.icon({
    iconUrl: './assets/img.png',
    iconSize: [25, 25],
    iconAnchor: [12, 12]
});

The correct approach is to use the require() function:

// Correct example - using require function
var icon = L.icon({
    iconUrl: require('./assets/img.png'),
    iconSize: [25, 25],
    iconAnchor: [12, 12]
});

Path Resolution Rules

Path resolution in Vue CLI follows specific rules:

Static Asset References in Templates

In Vue templates, you can use the @ alias to directly reference resources in the assets directory:

<!-- Using @ alias in templates -->
<img src="@/assets/images/home.png"/>

This syntax is properly handled by webpack during compilation, generating optimized resource references.

Webpack Asset Handling Mechanism

When static assets are referenced via relative paths in JavaScript, CSS, or *.vue files, they are incorporated into webpack's dependency graph. During compilation:

Webpack Assets Modules are responsible for determining the final file location, adding version hashes, and correctly handling public base paths. Resources smaller than 8KiB are inlined to reduce HTTP requests.

Using the Public Directory

Static assets placed in the public directory are not processed by webpack but are directly copied to the output directory. Referencing these resources requires using absolute paths:

// Referencing public directory resources in components
data() {
    return {
        publicPath: process.env.BASE_URL
    }
}

// Using in templates
<img :src="`${publicPath}my-image.png`">

Best Practice Recommendations

Based on Vue CLI's static asset handling mechanism, the following best practices are recommended:

  1. Prefer Relative Path Imports: Let resources be processed through webpack to benefit from bundling optimization, error detection, and cache control
  2. Use Public Directory Judiciously: Only use when specific filenames are needed, dealing with large numbers of dynamic images, or handling libraries incompatible with webpack
  3. Pay Attention to Path Context: When using require in JavaScript, paths are relative to the current file location
  4. Leverage Aliases for Simplicity: Use @ alias in templates to improve code readability

Common Issue Resolution

For common static asset referencing issues in development:

By understanding Vue CLI's static asset processing principles, developers can avoid common referencing errors and build more stable and efficient Vue.js applications.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.