My Vue.js web application is unable to find files with correct paths. I have checked the paths like 10 times, they are all correct and all give the 404 error. I tried relative path, absolute path and whatnot. There must be some Vue peculiarity which makes it unable to find the images.
The browser gives the following request summary (the path is given as '../assets/images/user.png'):
URL: http://localhost:8080/assets/images/user.png
Status: 404 Not Found
Source: Network
Address: 127.0.0.1:8080
A screenshot from VSCode:
MainPageBody.vue is the component that is supposed to show the images.
Been stuck at this for a while now.
Update: Followed @kissu's advice and used v-for
, also created a brand new Vue project and copied my files there. However, the images are still throwing 404s. I must be doing something wrong. Tried using require()
, that didn't help either.
The .json file on the URL may look something like:
{
"posts": [
{
"author": "Samuel",
"profilePicName": "../assets/images/user.png",
"text": "Look at my dog!",
"picName": "../assets/images/dog.jpg",
"date": "29. Sept 2022"
},
{
"author": "Bob",
"profilePicName": "../assets/images/bob.jpg",
"text": "Hello, I am Bob",
"picName": "../assets/images/bob.jpg",
"date": "7. Sept 2022"
}
]
}
Reproducible example: I tried my best to make a reproducible example. I assume you still need to create a Vue project to reproduce my errors? If there is anything missing you need me to add, let me know.
<!-- MainPageBody.vue -->
<template>
<div v-for="post in posts" :key="post.id">
<!-- text comes through fine, image is not found -->
<img :src="post.picName" />
<p>{{ post.text }}</p>
</div>
</template>
<script>
import { nanoid } from 'nanoid'
export default {
data() {
return {
posts: null
}
},
async created() {
const response = await fetch('https://api.npoint.io/21b1db5b0e48a83466da') // working URL for json file
const { posts } = await response.json()
this.posts = posts.map((post) => {
return { ...post, isHeld: false, id: nanoid() };
})
}
}
</script>
<!-- App.vue -->
<template>
<MainPageBody/>
</template>
<script>
import MainPageBody from './components/MainPageBody.vue';
export default {
name: 'App',
components: {
MainPageBody
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>