Categories
Vue Answers

How to change the default base URL for Axios?

Sometimes, we want to change the default base URL for Axios.

In this article, we’ll look at how to change the default base URL for Axios.

How to change the default base URL for Axios?

To change the default base URL for Axios, we can set the baseUrl option when we’re calling our axios instance.

For instance, we write

this.$axios({ url: "items", baseURL: "http://new-url.com" });

to call this.$axios with an object with the baseURL to a different URL than the one that’s set when we’re creating the axios instance.

Conclusion

To change the default base URL for Axios, we can set the baseUrl option when we’re calling our axios instance.

Categories
Vue Answers

How to delete property from a data object with Vue.js?

Sometimes, we want to delete property from a data object with Vue.js.

In this article, we’ll look at how to delete property from a data object with Vue.js.

How to delete property from a data object with Vue.js?

To delete property from a data object with Vue.js, we can use the this.$delete method.

For instance, we write

this.$delete(this.users, "foo");

in our component to remove the foo property from the this.users object.

Conclusion

To delete property from a data object with Vue.js, we can use the this.$delete method.

Categories
Vue Answers

How to set background color with Vuetify?

Sometimes, we want to set background color with Vuetify

In this article, we’ll look at how to set background color with Vuetify.

How to set background color with Vuetify?

To set background color with Vuetify, we can add our own theme colors.

For instance, we write

import Vue from "vue";
import Vuetify from "vuetify/lib";
import colors from "vuetify/lib/util/colors";

const vuetify = new Vuetify({
  theme: {
    themes: {
      light: {
        primary: colors.purple,
        secondary: colors.grey.darken1,
        accent: colors.shades.black,
        error: colors.red.accent3,
        background: colors.indigo.lighten5,
        //...
      },
      dark: {
        primary: colors.blue.lighten3,
        background: colors.indigo.base,
        //...
      },
    },
  },
});
//...

to add the background color in addition to the existing theme colors.

Then we can apply the background color by writing

<template>
  <v-app
    id="main"
    :style="{ background: $vuetify.theme.themes[theme].background }"
  >
    <v-content> ... </v-content>
  </v-app>
</template>

where theme is the property key that we have in themes.

Conclusion

To set background color with Vuetify, we can add our own theme colors.

Categories
Vue Answers

How to handle errors in Vue.js with Vuex and Axios?

Sometimes, we want to handle errors in Vue.js with Vuex and Axios.

In this article, we’ll look at how to handle errors in Vue.js with Vuex and Axios.

How to handle errors in Vue.js with Vuex and Axios?

To handle errors in Vue.js with Vuex and Axios, we can add a response interceptor.

For instance, we write

axios.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    store.commit("ERROR", error);
    return Promise.reject(error);
  }
);

to call axios.interceptors.response.use with the error handler in the 2nd argument.

In it, we call store.commit to put the error data in the Vuex store by committing the ERROR mutation.

And then we return a rejected promise with the error as the rejection reason.

Then we can catch any request errors with a catch block:

try {
  await axios.get("...");
} catch (e) {
  console.log(e);
}

Conclusion

To handle errors in Vue.js with Vuex and Axios, we can add a response interceptor.

Categories
Vue Answers

How to include global functions in Vue.js?

Sometimes, we want to include global functions in Vue.js.

In this article, we’ll look at how to include global functions in Vue.js.

How to include global functions in Vue.js?

To include global functions in Vue.js, we can create our own plugin.

For instance, we write

MyPlugin.install = (Vue, options) => {
  Vue.myGlobalMethod = () => {
    //...
  };

  Vue.directive("my-directive", {});

  Vue.prototype.$myMethod = function () {
    //...
  };
};

to create the MyPlugin plugin.

We set its install method to a function that adds a global static method with

Vue.myGlobalMethod = () => {
  //...
};

We add a directive with

Vue.directive("my-directive", {});

And we add an instance method with

Vue.prototype.$myMethod = function () {
  //...
};

Then we register the plugin with Vue.use by writing

Vue.use(MyPlugin)

We can call global static methods with

Vue.myGlobalMethod(parameters);

And we call component instance methods with

this.$myMethod 

in a component.

Conclusion

To include global functions in Vue.js, we can create our own plugin.