Selecting avatars using Nuxt and Vuetify for a personalized touch!

Having trouble getting an avatar picker to function properly...

When I click to select the avatar, it's not being replaced, and I'm encountering the error TypeError: Cannot read property 'src' of undefined at VueComponent.selectAvatar

Currently, I am using Vuetify and the v-avatar component with a v-for loop to load all the images.

Any suggestions on how to resolve this issue?

HTML

<v-flex xs12 pt-0 pb-0>
  <h1 class="title mb-4">User Details</h1>
  <v-avatar
    size="100px"
  >
    <img
      :src="this.selectedAvatar"
      alt="Avatar"
    >
  </v-avatar>
</v-flex>
<v-flex x12>
  <v-btn
    color="primary"
    flat="flat"
    small
    @click="selectAvatarDialog = true"
    class="avatar-btn"
  >
    Update avatar
  </v-btn>
</v-flex>

<v-dialog
  v-model="selectAvatarDialog"
  max-width="80%"
>
  <v-card>
    <v-container fluid pa-2>
        <v-layout row wrap align-center justify-center fill-height>
          <v-flex xs6 sm4 md3 lg2 my-2 class="text-xs-center"
          v-for="(avatar,i) in avatars"
          :key="i">
            <v-img
              :src="avatar.src"
              aspect-ratio="1"
              width="100px"
              max-width="100px"
              min-width="100px"
              class="dialog-avatar-img"
              @click="selectAvatar()"
            ></v-img>
          </v-flex>
        </v-layout>
      <v-card-actions class="mt-2">
        <v-spacer></v-spacer>

        <v-btn
          color="primary"
          flat="flat"
          @click="selectAvatarDialog = false"
        >
          Cancel
        </v-btn>
      </v-card-actions>
    </v-container>
  </v-card>
</v-dialog>

JS

export default {
  layout: 'default',

  data() {
    return {
      selectAvatarDialog: false,
      avatars: [
        {src: require('@/assets/images/avatar-01.png') },
        { src: require('@/assets/images/avatar-02.png') },
        { src: require('@/assets/images/avatar-03.png') },
        { src: require('@/assets/images/avatar-04.png') },
        { src: require('@/assets/images/avatar-05.png') }
      ],
      selectedAvatar: require('@/assets/images/avatar-01.png'),
    }
  },
  methods: {
    selectAvatar(){
      this.selectedAvatar = this.avatar.src
      console.log('Avatar selected')
    }
  }
}

Thank you

Answer №1

Your selectAvatar method is causing the issue because you are trying to use 'this.avatar' when it does not actually exist. The avatar in your for loop is not being passed to your script properly. To fix this, make sure to do the following:

<v-img
  ...
  @click="selectAvatar(i)"
></v-img>

Then, update your script as follows:

methods: {
    selectAvatar(i){
      this.selectedAvatar = this.avatars[i].src
      console.log('Avatar selected')
    }
  }

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

Ways to verify if Arabic text has been submitted by the user through a form?

Is there a foolproof method for detecting Arabic input in a form before submission? Can Javascript effectively manage this task, or is it better handled by server-side scripts like .NET? I propose implementing a script to immediately block users from ente ...

Using v-for with nested objects

Have you been attempting to create a v-for loop on the child elements of the {song: "xxx"} object within the songs array? export const data = [ {id: "1", albumname: "xx", artist: "xxxx", dateadded: "xxxx", route: "xxxx", songs: [{ song : &apos ...

Vue | Passing data to parent using eventbus

Is there a way in Vue to broadcast all emitters to the parent listening component? For example, can we simplify this code: <DetailsPages v-if="$route.hash === '#details_pages'" v-on:next="$emit('next')" v-o ...

ajax-jquery request declined

I have a jquery-ajax function that is being called multiple times with different IP addresses each time. This function makes a call to an action in the mvc4 controller responsible for executing a ping and returning the results. After analyzing the request ...

Illustrate an element positioned beneath a child element within a different element

I am in the process of designing an overlay on a Google Map that resembles a real map placed on a table, using 2 pixel lines as creases above the map. This design does not significantly impact interactions with the map; only 6 out of 500 vertical lines are ...

Focus issue with MUI Custom Text Field when state changes

Currently, I've integrated the MUI library into my React Js application. In this project, I'm utilizing the controlled Text Field component to establish a basic search user interface. However, an unexpected issue has surfaced. Following a chang ...

Restrict the duplication of div elements with JQuery

Here is the structure I'm working with: <div class="checkmark-outer"> <div class="checkmark-33"> <div class="fa-stack fa-1x checkmark-icon"> <i class="fa fa-circle fa-stack-2x icon-background"></i> ...

How can I configure CRA to automatically append a slash to the end of URLs?

When I install CRA using npx create-react-app app-name and then run yarn start, the development server will by default start at http://localhost:9000. The issue arises when I try to copy the URL from the browser's address bar to my CORS middleware in ...

Sort array in ReactJS using JSON data fetched from a URL

There is a URL where I have stored some data, such as: [ {"id":1,"first_name":"add","last_name":"add"}, {"id":2,"first_name":"sfdf","last_name":"aad"} ...

Dispatching actions in `componentDidMount` is restricted in Redux

Update at the bottom of post I've created a React container component called AppContainer, which checks if the user is authenticated. If the user is authenticated, it renders the app's routes, header, and content. If not, it displays a Login com ...

Issues with clearRect() function in HTML5 canvas

Recently, I developed a function with the goal of redrawing a square line block that covers the entire page whenever the window size is adjusted. For reference, you can view my code at http://jsfiddle.net/9hVnZ/ However, I encountered an issue: bgCtx.cl ...

Access the JSON data containing sub array values and showcase them on an HTML page by utilizing ngFor

Greetings! I am currently working on a web application where I need to showcase student data that is being received in JSON format. Below is the TypeScript code snippet that outlines the structure of the student data: export interface studentData{ ...

Tips for adding a dynamic variable into a react JSX map function

I have a component that receives a props with the value of either 'A', 'B', or 'C' based on the selection made in the parent element. The challenge is to use this props to dynamically select an option list locally, instead of ...

I encountered an issue while attempting to connect to my MySQL database using my Express API endpoint: error message "connect ECONNREFUSED 127.0

I am currently in the process of developing a web application for a bootcamp using Express and MySQL. I have set up a route to handle a GET request to an endpoint which is supposed to query my MySQL database table and retrieve all records. My intention is ...

Browser locks up for a brief 4-5 seconds while waiting for the Vue component to complete loading

My Vue components include task.vue, which generates a list of tasks, and epic.vue, the parent of task.vue. Each epic.vue generates its own tasks list from task.vue. The issue arises when the page is reloaded, causing the browser to freeze for 4-5 seconds ...

Adjusting the size of images in a Bootstrap lightbox gallery

I am currently working on a website for an artist, making the galleries a key aspect. The website is built using Bootstrap, with the Lightbox for Bootstrap plugin being used for the galleries. Everything seems to be working well in terms of adjusting the i ...

Remove a key using Vue Resource

I am looking to remove a specific piece of data from my firebase database using its key: https://i.stack.imgur.com/S1zDA.png This is the approach I have tried in order to delete all data from the database: this.$http.delete('data.json', book.i ...

Transforming complex mathematical equations into executable code using the node.js platform

Looking to implement a mathematical formula found at the following link: https://en.wikipedia.org/wiki/Necklace_(combinatorics)#Number_of_bracelets into node.js for calculating the total number of distinct ring sequences that can be created with n length ...

Vue transition unexpectedly ending in the wrong position due to nested transitions

I've encountered an issue while trying to implement a transition on a child div within a 'parent' div that already has its own transition. It seems like the child transition is conflicting with the parent transition, causing it to end up in ...

avoid loading javascript in ajax layer

After struggling for days to find answers online and unsuccessfully trying different codes, I discovered a way to use ajax to dynamically change the contents of a div element and display different sliders. Here are the files if you want to take a look: Fi ...