N
N
nurdus2017-09-04 22:11:58
Vue.js
nurdus, 2017-09-04 22:11:58

Why isn't a method called from another method in vue?

It works, but somehow I don't want to duplicate the code...

let data = function() {
    return { user: {}};
};
vm = new Vue({
    el: '#navbar',
    data: data,
    methods: {
        getUserInfo: function() {
            let vm = this;
            $.ajax({
                type: 'GET',
                url: config.apiHost + apiRoutes.getUserInfo,
                dataType: 'json',
            })
            .done(function(res) {
                if (!res.error) {
                    vm.user = res.data;
                }
            });
        },
        loginLocal: function() {
            let vm = this;
            let res = this.sendForm('loginLocal') /*для простоты убрал код функции sendForm*/
            .done(function(res){
                if (!res.error) {
                    vm.user = res.data;
                }
            });
        },
        logout: function(e) {
            let vm = this;
            let res = this.sendForm('logout')
            .done(function(res){
                if (!res.error) {
                    vm.user = res.data; // /*тоже самое, что */ vm.user = {};
                }
            });
        },
    }
});

This does NOT work... to update the values, you need to refresh the page manually
let data = function() {
    return { user: {}};
};
vm = new Vue({
    el: '#navbar',
    data: data,
    methods: {
        getUserInfo: function() {
            let vm = this;
            $.ajax({
                type: 'GET',
                url: config.apiHost + apiRoutes.getUserInfo,
                dataType: 'json',
            })
            .done(function(res) {
                if (!res.error) {
                    vm.user = res.data;
                }
            });
        },
        loginLocal: function() {
            let vm = this;
            let res = this.sendForm('loginLocal') /*для простоты убрал код функции sendForm*/
            .done(function(){ // пробовал просто "getUserInfo() или getUserInfo"
                getUserInfo();
            });
        },
        logout: function(e) {
            let vm = this;
            let res = this.sendForm('logout')
            .done(function(){
                getUserInfo();
            });
        },
    }
});

Thank you very much in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Shashenkov, 2017-09-04
@nurdus

vm.$set(vm,'user',res.data)
Why jq for ajax? fetch, axios - for wimps?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question