Shadow
Shadow

Reputation: 11

Update data on page without refreshing on with Vue?

When I try to add a friend, button is loading and show up again instead of Waiting for response, but after refresh the page it works.

   methods: {

        add_friend(){


            this.loading = true

            this.$http.get('/add_friend/' + this.profile_user_id)

                .then( (r) => {

                    if(r.body == 1)

                        this.status = 'waiting'
                        this.loading = false

                })

Upvotes: 0

Views: 1299

Answers (2)

Taki
Taki

Reputation: 68

If you are returning body from your laravel controller, then your http block in vue, should look like this:

this.$http.get('/add_friend/' + this.profile_user_id)

            .then( (r) => { // you can also use destructing
                const data = r.data;

                if(data.body == 1)

                    this.status = 'waiting'
                    this.loading = false

            })

Upvotes: 0

common sense
common sense

Reputation: 3898

Your if condition misses curly braces. This is only valid for one-line statements but not for multi-line.

if(r.body == 1) {
    this.status = 'waiting';
    this.loading = false;
}

Check also if r.body contains 1.

Upvotes: 2

Related Questions