Как решить проблему Nuxt + TypeScript?

Как правильно решить проблему с axios?
<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>


выдает

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>>>'.
  • Вопрос задан
  • 1170 просмотров
Пригласить эксперта
Ответы на вопрос 2
@eternalfire
https://github.com/nuxt/typescript/issues/335#issu...

нужно добавить в tsconfig.json @nuxt/types
{
  "compilerOptions": {
    ...
    "types": [
      "@types/node",
      "@nuxt/types"
    ]
  },
  ...
}
Ответ написан
Zoxon
@Zoxon
Веб-разработчик
Вам нужно расширить тип Vue, Vuex, Nuxt (смотря где будете использовать this.$axios)
Тут есть примеры как это делать https://typescript.nuxtjs.org/cookbook/plugins
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы