Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

40
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')
4 months 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'
4 months ago · Santiago Trujillo Report
Answer question
Find remote jobs