Ashraam
2 years ago
1
0
Vue

Reusable functions (mixins ?)

Posted 2 years ago by Ashraam

Hi everyone,

I'm learning how to use VueJS (it's kinda awesome) and I'd like to know I could re-use some functions like this:

this.$http.post('/api/servers', this.server)
                .then(response => {
                    return response.json();
                }, error => {
                    const errors = error.body;
                    if(typeof errors === 'object') {
                        for(let key in errors) {
                            this.errors.push(errors[key][0]);
                        }
                    } else {
                        swal({
                            title: "Oops",
                            type: 'error',
                            text: `${error.status}: ${error.statusText}`
                        });
                    }
                })
                .then(data => {
                    this.isLoading = false;
                    
                    if(data) {
                        // Something
                    }
                });

In this example I'm using Vue-Resource and Vue-Router and each time I'm using Vue-Resource to fetch some data I want to use this error function (now I'm writing this piece of code every time :/)

How should I do that ?

Thank you

Please sign in or create an account to participate in this conversation.