0

I am using regular Vue.js in my project. I store data in a store made from scratch and use it in a template:

<template>
  <div>
    <div class="row">
      <div v-for="(picture, index) in storeState.pictures"
          :key="index"
          class="col-md-2 my-auto">   
          <div >                 
            <img class="img-thumbnail img-responsive" 
                :src="picture.url"
                @click="deleteMe">            
          </div>              
      </div>      
    </div>    
    </div>
</template>

<script>
import { store } from "../common/store.js"

export default {
  name:"PictureUpload",
    data() {
        return {
  storeState: store.state,           
        };
  },
  methods: {
    deleteMe() {
      let apiUrl = this.picture.url
      console.log(apiUrl)
    }
  }
}
</script>

My pictures are rendering well but now I want to add a delete() function to the picture @click and whenever I click on the button I get:

TypeError: Cannot read property 'url' of undefined

So how can I access my picture data inside my method?

5
  • where do you define picture? Commented Aug 23, 2020 at 8:18
  • in the v-for loop Commented Aug 23, 2020 at 8:19
  • is my answer helpful? Commented Aug 23, 2020 at 9:05
  • Yes thank you! I voted for it. Commented Aug 23, 2020 at 10:01
  • you're welcome, there's something went wrong, i received only the upvote score +10 and i didn't receive the accept vote +15 however the answer has the green check mark, please try to unaccept it and accept again it Commented Aug 23, 2020 at 10:04

1 Answer 1

1

You should pass picture as parameter in the click handler :

  @click="deleteMe(picture)">

and refer to it in the method like :

 methods: {
   deleteMe(picture) {
     let apiUrl = picture.url //omit this
     console.log(apiUrl)
    }
   }

the storeState should be a computed property :


export default {
  name:"PictureUpload",
    data() {
        return {
           
        };
  },
  computed:{
     storeState(){
    return store.state;
   }
  },
  methods: {
    deleteMe(picture) {
      let apiUrl = picture.url
      console.log(apiUrl)
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.