0

I'm making app in Vue.js 2.6.14.

I use vue-query to fetch data from external API. I wanted to pass an argument - this.form.style to the query function, inside setup() but it seems to does not work. How should i do it properly?

<script lang="ts">
  const fetchRandomImg = async (style: FormType["style"]) => {
  const params = new URLSearchParams();

  if (style.type === "grayscale") {
    params.append("grayscale", "");
  } else if (style.type === "blur") {
    params.append("blur", style.blurValue.toString());
  }

  const response = await fetch(
    `https://picsum.photos/150?${params.toString()}`
  );

  const blob = await response.blob();

  return URL.createObjectURL(blob);
};


export default Vue.extend({
  data(){
    return{
     form:{
       style: {
        type:"normal",
        blurValue: 1
       },
      ...
     }
    }
  }
})

setup() {
 const { data: randomImg, refetch: refetchRandomImg } = useQuery(
  ["randomImage", this.form.style],
  () => fetchRandomImg(this.form.style),
  {
    refetchOnWindowFocus: false,
  }
);

return { randomImg, refetchRandomImg };
},

})
</script>
1
  • You preferably shouldn't. Just use composition api. Commented Apr 30, 2023 at 16:50

1 Answer 1

0

Put your JS code inside a created(){} function, inside the Vue.extend({}).

That should allow it access to this.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.