M
M
Maxim2021-03-24 15:55:06
typescript
Maxim, 2021-03-24 15:55:06

How to solve Nuxt + TypeScript problem?

How to solve the problem with axios correctly?

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
  data() {
    return {
      form: {
        customer_name: '',
        customer_phone: '',
        customer_message: '',
      },
    }
  },
  methods: {
    sendOrder() {
      this.$axios
        .$post('/v1/order', this.form)
        .then((response) => {
          if (response.error) {
            this.$swal({
              icon: 'error',
              title: response.message,
              timerProgressBar: true,
            })
          }
        })
        .catch((error) => {
          this.$swal({
            icon: 'error',
            title: Object.values(error.response.data.errors).flat().join(''),
            timerProgressBar: true,
          })
        })
    },
  },
})
</script>


issues

Property '$axios' does not exist on type 'CombinedVueInstance<Vue, { form: { customer_name: string; customer_phone: string; customer_message: string; }; page: { title: string; description: string; }; }, { sendOrder(): any; }, unknown, Readonly<Record<never, any>>>'.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eternalfire, 2021-03-24
@eternalfire

https://github.com/nuxt/typescript/issues/335#issu...
needs to be added to tsconfig.json @nuxt/types

{
  "compilerOptions": {
    ...
    "types": [
      "@types/node",
      "@nuxt/types"
    ]
  },
  ...
}

K
Konstantin Velichko, 2021-04-01
@Zoxon

You need to extend the Vue, Vuex, Nuxt type (depending on where you use this.$axios)
There are examples of how to do this https://typescript.nuxtjs.org/cookbook/plugins

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question