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.

Categories
Vue Answers

How to reference the form element when we submit a form in Vue.js?

Sometimes, we want to reference the form element when we submit a form in Vue.js.

In this article, we’ll look at how to reference the form element when we submit a form in Vue.js.

How to reference the form element when we submit a form in Vue.js?

To reference the form element when we submit a form in Vue.js, we can use a ref.

For instance, we write

<template>
  <div>
    <form ref="form" @submit.prevent="submit">
      <input v-model="store.vital" />
      <button type="submit">Submit</button>
    </form>
  </div>
</template>

<script>
//...
export default {
  //...
  methods: {
    submit() {
      this.$refs.form.$el.submit();
    },
  },
  //...
};
</script>

to add a form and we assign the form ref to it.

And we make the submit method run when we submit the form with @submit.prevent="submit".

The prevent modifier calls event.preventDefault to prevent the default server side submission behavior.

In submit, we get the form element with this.$refs.form.$el.

Conclusion

To reference the form element when we submit a form in Vue.js, we can use a ref.