@eywsf

Как получить доступ к полям data в Vue из асинхронного метода?

Есть код:

export default {
   data() {
        return {
            nameCity: '',
        }
    },
    methods: {
        findCity(event){
            event.preventDefault()
            findCityCust().then(function(response) { 
                console.log(response)
               this.nameCity = response;
            })
        }, 
    },
}

И вот здесь - this.nameCity = response; - выдает ошибку Uncaught (in promise) TypeError: Cannot read properties of undefined

Как в Vue 3 из асинхронных методов работать с полями?
  • Вопрос задан
  • 207 просмотров
Решения вопроса 1
@AndromedaStar
.Net - monkey
export default {
   data() {
        return {
            nameCity: '',
        }
    },
    methods: {
        findCity(event){
            event.preventDefault()
            findCityCust().then((response) => { 
                console.log(response)
               this.nameCity = response;
            })
        }, 
    },
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

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

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