5

Thanks for reading my question.

I'm trying to get the new <script setup> syntax (Composition API) with Vue.js 3.2 and axios running. With the normal syntax my code looks something like:

<script>
import axios from 'axios'

export default {
  name: 'GetRequest',
  data () {
    return {
      infos: null
    }
  },
  mounted () {
    axios
      .get('https://api.predic8.de/shop/products/')
      .then(response => (this.infos = response.data))
  }
}
</script>
<template>
  <div id="app">
    {{ infos }}
  </div>
</template>

This works just fine, but I use a template (https://github.com/justboil/admin-one-vue-tailwind) for my projekt which works with the new <script setup>.

I already found some solutions like:

<script setup>
 
import {onMounted} from "vue";
 
const {ref} = require("vue");
const axios = require("axios");
const info = ref([])
onMounted(async () => {
  await axios
      .get('https://api.predic8.de/shop/products/')
      .then(response => {
        this.info = response.data
 
      })
})
</script>
<template>
  <div id="app">
    {{ infos }}
  </div>
</template>

but it gives me 'this.infos' is assigned a value but never used. Does anyone know how I can assigne the value to the variabel and call it in the <template>?

Update:

I found the solution by using infos.value instead of this.infos

<script setup>
import {onMounted} from "vue"
 
const {ref} = require("vue")
const axios = require("axios")
const infos = ref([])
onMounted(async () => {
  await axios
    .get('https://api.predic8.de/shop/products/')
    .then(response => {
      infos.value = response.data
    })
})
</script>
<template>
  <div id="app">
    {{ infos }}
  </div>
</template>
``` 

2 Answers 2

5

It's better to use inject for importing axios in each component. This way you can create some interceptors if they needed as well...

First you should install the axios plugin for vue.js.

> npm install --save axios vue-axios

When the installation finished just import the axios like below example:

main.js

import { createApp } from "vue";
import axios from "./plugins/axios";
import VueAxios from "vue-axios";

const app = createApp(App);

// Axios Plugin
app.use(VueAxios, axios);
app.provide("axios", app.config.globalProperties.axios);

inside any component

import { inject } from "vue";

const axios = inject('axios');
// using axios as usual

Note: I used the <script setup> inside the component example.

PS: You can create an axios instance inside the ./plugins/axios.js file if you need to use interceptors otherwise just import the axios as always inside main.js file!

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

1 Comment

Thanks for your help! I consider using the injection for this project.
0
  1. In your template you are accessing "infos" but the declared variable is "info"
  2. In your onMounted callback your assignment should be without "this" just "info = response.data"

Any variable declared at the top level script would be accesible from the template.

More info here https://v3.vuejs.org/api/sfc-script-setup.htm and here https://v3.vuejs.org/guide/composition-api-setup.html

2 Comments

Thank you as well for your help. The missing information was using .value
@Taraman You can click on the check mark on the left of the answer to say that it's the accepted answer

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.