Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why isn't the prop defined?

I have this code for my component:

<template>
    <h2>{{ name }}</h2>
    <img :src="imgLink"/>

</template>

<script>
import { ref } from 'vue';


let imgLink = ref()

export default {
    props: {
        'name': String,
        'imgurl': String
    }
}

fetch(imgurl).then(res => res.json()).then(json => imgLink.value = json.sprites.front_default)



</script>

But i get the error "’imgurl’ is not defined" at the line with the fetch-function. I don’t understand why it isn’t defined since i define it as a String in the props.

Any help is appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have only tried too make a seperate variabel and setting its value to the props value becuase i thought the problem might be using the prop as a link directly.

>Solution :

I think you are mixin the <script setup> notation (using composition API) with the option API. You can either do :

<script setup>
import { ref, defineProps } from 'vue';

let imgLink = ref()

const props = defineProps({
  name: String,
  imgurl: String
})

fetch(props.imgurl).then(res => res.json()).then(json => imgLink.value = json.sprites.front_default)
</script>

See : https://vuejs.org/api/sfc-script-setup.html#defineprops-defineemits

Or with the options API :

<script>
export default {
    props: {
        'name': String,
        'imgurl': String
    },
    data: () => ({
       imgLink: null,
    }),
    created() {
        fetch(this.imgurl).then(res => res.json()).then(json => this.imgLink = json.sprites.front_default)
    }
}
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading