Categories
Vue Answers

How to hide div on click in Vue.js?

Sometimes, we want to hide div on click in Vue.js.

In this article, we’ll look at how to hide div on click in Vue.js.

How to hide div on click in Vue.js?

To hide div on click in Vue.js, we can use the v-if directive.

For instance, we write

<template>
  <div id="app">
    <button @click="isHidden = true">Hide</button>
    <button @click="isHidden = !isHidden">Toggle hide and show</button>

    <h1 v-if="!isHidden">Hide me on click event!</h1>
  </div>
</template>

<script>
//...
export default {
  //...
  data() {
    return {
      isHidden: false,
    };
  },
  //...
};
</script>

to add the isHidden reactive property by returning it in the object in the data method.

And then we add 2 buttons.

The first set isHidden to true when we click it.

And then 2nd toggles the value of isHidden between false and true when we click it.

We then add v-if to show the h1 element when isHidden is false.

Conclusion

To hide div on click in Vue.js, we can use the v-if directive.

Categories
Vue Answers

How to access props in a Vue.js component data function?

Sometimes, we want to access props in a Vue.js component data function.

In this article, we’ll look at how to access props in a Vue.js component data function.

How to access props in a Vue.js component data function?

To access props in a Vue.js component data function, we can get them from this.

For instance, we write

<script>
//...
export default {
  //...
  props: ["messageId"],
  data() {
    const theData = {
      someVar: this.messageId,
    };
    return theData;
  },
  //...
};
</script>

to register the messageId prop.

Then we get the initial value of the messageId prop with this.messageId in the data method.

Conclusion

To access props in a Vue.js component data function, we can get them from this.

Categories
Vue Answers

How to show loading when making ajax request with Axios in Vue.js?

Sometimes, we want to show loading when making ajax request with Axios in Vue.js.

In this article, we’ll look at how to show loading when making ajax request with Axios in Vue.js.

How to show loading when making ajax request with Axios in Vue.js?

To show loading when making ajax request with Axios in Vue.js, we can add Axios request and response interceptors and set the loading state in the interceptor methods.

For instance, we write

<script>
//...
export default {
  //...
  created() {
    axios.interceptors.request.use(
      (config) => {
        this.isLoading = true;
        return config;
      },
      (error) => {
        this.isLoading = false;
        return Promise.reject(error);
      }
    );

    axios.interceptors.response.use(
      (response) => {
        this.isLoading = false;
        return response;
      },
      (error) => {
        this.isLoading = false;
        return Promise.reject(error);
      }
    );
  },
  //...
};
</script>

to call axios.interceptors.request.use to add a request interceptor and call axios.interceptors.response.use to add a response interceptor in the component’s created hook to add the interceptors before the component is mounted.

We call them with methods that sets isLoading to differnt values.

We have this.isLoading = true; when we’re making the request

And in the other functions, we set isLoading to false since the request is finished.

Conclusion

To show loading when making ajax request with Axios in Vue.js, we can add Axios request and response interceptors and set the loading state in the interceptor methods.

Categories
Vue Answers

How to add routes with parameters in Vue.js using Vue Router router-link?

Sometimes, we want to add routes with parameters in Vue.js using Vue Router router-link.

In this article, we’ll look at how to add routes with parameters in Vue.js using Vue Router router-link.

How to add routes with parameters in Vue.js using Vue Router router-link?

To add routes with parameters in Vue.js using Vue Router router-link, we can set the to prop to an object with the params property set to an object with the URL parameters.

For instance, we write

<template>
  <div>
    <router-link :to="{ name: 'user', params: { userId: 123 } }">
      User
    </router-link>
  </div>
</template>

to add router-link with the to prop set to an object with the params property set to { userId: 123 }

to replace the userId URL parameter placeholder with 123.

name is the name property value that we set when we define the route.

Conclusion

To add routes with parameters in Vue.js using Vue Router router-link, we can set the to prop to an object with the params property set to an object with the URL parameters.

Categories
Vue Answers

How to use async and await with Vuex dispatch?

Sometimes, we want to use async and await with Vuex dispatch.

In this article, we’ll look at how to use async and await with Vuex dispatch.

How to use async and await with Vuex dispatch?

To use async and await with Vuex dispatch, we can add an action method that’s an async function.

For instance, we write

const store = new Vuex.Store({
  //...
  actions: {
    getProducts: async ({ commit }, type) => {
      try {
        const { data: products } = await axios.get(`/api/products/${type}`);
        commit("SET_PRODUCTS", { products, type });
      } catch (err) {
        console.log(err);
      }
    },
  },
  //...
});

to add the getProducts action that makes an async request with axios.get.

And then we call commit to commit the SET_PRODUCTS mutation to store the data.

Conclusion

To use async and await with Vuex dispatch, we can add an action method that’s an async function.