Encountering a challenge with Nuxt where I am unable to navigate back when utilizing the require method

After successfully viewing the image, I encountered an error when trying to navigate back using

<NuxtLink :to="{ path: 'About', hash: '#secondNav' }" class="close">
, which resulted in a message stating
Cannot find module './undefined.jpg'

"<template>
    <div class="container-fluid imgBackground vh-100">
        <div class="imgCont">
            <NuxtLink :to="{ path: 'About', hash: '#secondNav' }" class="close">
                <img
                    id="closeImg"
                    src="@/assets/images/closeHover.png"
                    src2="@/assets/images/close.png"
                />
            </NuxtLink>
            <img
                class="img-fluid"
                :src="imageUrl.myImg"
                alt="Sarah Project Image"
            />
        </div>
    </div>
</template>

<script>
export default {
    computed: {
        imageUrl() {
            return {
                myImg: require(`@/assets/images/gallery/${this.$route.params.img}.jpg`)
            };
        }
    }
};
</script> 

Answer №1

If you’re using Nuxt, there’s no requirement to use the assets folder or the require function for images.

Nuxt simplifies this process with a dedicated directory known as the static folder.

Anything placed within the static folder is directly accessible from the root of your website domain.

For instance, if your domain is example.com and inside the static folder you have logo.png and folder/image.jpg:

  • The paths would be: example.com/logo.png and example.com/folder/image.jpg

To adapt this to your needs, you can place an “images” folder inside Nuxt's static directory.

In your computed method, you can format the image URL like this:

imageUrl() {
    return `/images/gallery/${this.route.params.img}.jpg`;
}

Then, simply access it in your code like so:

<img
    class="img-fluid"
    :src="imageUrl"
    alt="Sarah Project Image"
 />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Keys in React Native's ListView are used to uniquely identify

There is a warning in my app that is causing me some concern. React keeps prompting me to add keys for each row, but no matter what I try, I can't seem to include these keys. This is how my code looks: <ListView style={styles.listView} dat ...

Arrangement of Bootstrap card components

Each card contains dynamic content fetched from the backend. <div *ngFor="let cardData of dataArray"> <div class="card-header"> <div [innerHtml]="cardData.headerContent"></div> </div> <d ...

Material Grid adamantly refused to follow the typical horizontal layout, defying its default behavior

I recently discovered Material-UI and have been trying to get two components to display side by side. However, no matter what I try, it always ends up looking like this: https://i.stack.imgur.com/w1Wyh.png Even though it is the default behavior, the Mate ...

What is the best way to retrieve two elements from the same index level that are located in separate parent DIVs?

Take a look at this HTML setup: <div id="container"> <div class="left-column"> <div class="row">Person 1:</div> <div class="row">Person 2:</div> <div class="row">Person 3:</div> </div> ...

Error: Vue js - module store has not been defined

While working on my Vue.js project, I encountered an error message stating * ./store in ./src/main.js. Despite using the store in app.vue and verifying that it's functioning correctly with this.$store.dispatch("user", response.data.response);, I kept ...

Looking for guidance on building a real-time React application with automatic data fetching linked to a nodejs backend

I have a simple question, I have developed an app that retrieves data from a backend server, Now, the app needs to be accessed and edited by multiple users simultaneously while ensuring that all changes made to the list are reflected in real-time for ever ...

retrieve only the following occurrence throughout the entire file

I am looking to target only the first occurrence of an element in the document after a clicked element. Here is my current approach: $('.class').click(function(){ var x = $(this).nextAll('.anotherclass').first(); //do something ...

Eliminate elements from an array using a specific key for filtering

My Array is called MainArray MainArray=[{ {First Name: "First Name"}, {Last Name: "Contact"}, {Last Name: "Contact"} ] I am trying to trim the key-value pair from this array like this: if (key == 'First Name') { del ...

What's the Hold-Up with IntersectionObserver in Brackets?

As a novice in the world of web development, I have been utilizing Brackets as my main tool. Recently, I've encountered a few hurdles specifically related to javascript. One issue I'm facing is trying to implement lazy loading on a div containing ...

Vue.js HTML5 Editor_vueful

I am currently utilizing browserify and attempting to incorporate the vue-html5-editor library from https://github.com/PeakTai/vue-html5-editor. However, when I attempt the following code: Vue.use(require('vue-html5-editor')); An error is thro ...

The 'classProperties' React component module is currently not enabled

Encountering an error while running my react module 'classProperties' isn't currently enabled (44:11): } 43 | // componentDidUpdate or try this > 44 | onClick = (e) => { | ^ 45 | e.preventDefault(); 4 ...

Guide on increasing a basic counter and a counter with an object component using redux

(I need help fixing my code) I am trying to write a counter increment code to better understand Redux in the following situations: Increasing a simple counter Increasing a counter with an object Currently facing an issue where counter1 is undefined on the ...

Issues arising post transitioning to 14.0.0 from 13.0.0 version of ngx-masonry library leading to failed tests

Following the update to the latest stable version of the library ngx-masonry 14.0.0, our tests started failing. The release was just yesterday (24.10.2022) and you can find the changelog here: https://github.com/wynfred/ngx-masonry/blob/master/CHANGELOG.md ...

Enhance the functionality of jqGrid by customizing key bindings for arrow and tab keys

In order to enhance my jqGrid functionality, I am seeking to implement the ability for users to press different keys for specific actions. For example, pressing Enter should save data (which is currently working fine), while pressing the left arrow key sho ...

Guide on retrieving an ArrayList() from intricate function in Angular

Simplicity is the key to my question. Let's take a look at this Angular method: getAllOrdersHeaders(){ this.getAllOrdersIds().subscribe(idList=>{ idList.forEach(id=>{ this.ordersCollection.doc(id).collection('metadata&apo ...

What is preventing the buttons from filling the entire space of the parent element in this case?

https://i.stack.imgur.com/kbiWi.png I'm trying to figure out how to make the Repos and Stars buttons fill the entire height of their parent container, similar to the Github icon. Unfortunately, the code below is not achieving this effect. I attempted ...

Is it possible to validate a template-driven form without using the model-driven approach?

Attempting to validate a template-driven form in Angular without two-way data binding has proved to be challenging. I have successfully implemented validation using [(ngModel)], but running into an error when trying to validate the form without the MODEL p ...

Tap on the HTML5 video to exit the fullscreen mode

Objective I have successfully implemented a fullscreen video setup that triggers when a link is tapped on a mobile device. To maintain a clean aesthetic, I have hidden the HTML5 video controls using CSS. The desired functionality includes closing the full ...

Insert the ng-if directive into an element using a directive

I am working on an AngularJS directive that involves looking up col-width, hide-state, and order properties for a flexbox element based on its ID. I want to dynamically add an ng-if=false attribute to the element if its hide-state is true. Is there a way ...

Navigation bar remaining fixed on mobile devices

I have been working on a bootstrap site at http://soygarota.com/blog/public After checking the code multiple times, I have added the bootstrap.min.css, bootstrap.min.js, and jQuery. However, for some reason, the header navigation is not collapsing when vi ...