@mazahaler

Как использовать $this там, где он undefined?

Здравствуйте, как можно использовать $this, не прибегая к объявлению переменной через var в данном случае?
methods: {
            login() {
                var redirect = this.$auth.redirect() // ВОТ ЭТО МОЗОЛИТ ГЛАЗА ОЧЕНЬ СИЛЬНО
                var app = this // И ВОТ ЭТО
                this.$auth.login({
                    params: {
                        email: app.email,
                        password: app.password
                    },
                    success: function () {
                        // handle redirection
                        const redirectTo = redirect ? redirect.from.name : this.$auth.user().role === 2 ? 'admin.dashboard' : 'dashboard'
                        this.$router.push({name: redirectTo})
                    },
                    error: function (error) {
                        app.has_error = true
                        app.errors = error.response.data.errors || {}
                        app.error = error.response.data.error
                    },
                    rememberMe: app.remember_me,
                    fetchUser: true
                })
            },
         ........
        }

Есть ли способ передать $this внутрь this.$auth.login без использования var?
  • Вопрос задан
  • 157 просмотров
Решения вопроса 1
delphinpro
@delphinpro Куратор тега JavaScript
frontend developer
Использовать стрелочные функции
methods: {
    login() {
        this.$auth.login({
            params: {
                email: this.email,
                password: this.password
            },
            success: () => {
                // handle redirection
                const redirectTo = this.$auth.redirect()
                    ? this.$auth.redirect().from.name
                    : this.$auth.user().role === 2
                        ? 'admin.dashboard'
                        : 'dashboard'
                this.$router.push({name: redirectTo})
            },
            error: error => {
                this.has_error = true
                this.errors = error.response.data.errors || {}
                this.error = error.response.data.error
            },
            rememberMe: this.remember_me,
            fetchUser: true
        })
    },
    ........
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

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

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