Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
404 Error on loading local image using vue

I'm trying to make a simple dice roll game in VueJs but i'm having problems to use the dice images, it always gives 404 error and when i try to use require() it says it's not defined

Got a solution by using this method instead of using require() new URL('./components/icons/'+ this.img[this.dice-1], import.meta.url).href;

code:

App.vue

<template>
  <div>
    <button @click="roll">roll</button>
    <p> Resultado: {{dice}} </p>
    <div v-if="dice != 0">
      <img :src="selected"/>
    </div>
  </div>
</template>

<script>
  export default {
    data(){
      return{
        img: [
          '@/components/icons/dice1.png',
          '@/components/icons/dice2.png',
          '@/components/icons/dice3.png',
          '@/components/icons/dice4.png',
          '@/components/icons/dice5.png',
          '@/components/icons/dice6.png',
        ],
        selected: '',
        dice: 0,
      }
    },
    created(){
      this.selected = this.img[1];
    },methods:{
      roll: function(){
      this.dice = Math.ceil(Math.random() *6);
      this.select = this.img[this.dice];
      console.log(this.img[this.dice]);
      }
    }
  }
</script>

Main.js

import { createApp } from 'vue'
import App from './App.vue'


import './assets/main.css'

createApp(App).mount('#app')
almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I can't find require() in your code, but you could try the import statement:

import { ModuleName } from 'pathname'
almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!