Categories
Vue Answers

How to add optional parameters with Vue Router?

Sometimes, we want to add optional parameters with Vue Router.

In this article, we’ll look at how to add optional parameters with Vue Router.

How to add optional parameters with Vue Router?

To add optional parameters with Vue Router, we can append a ? after the route parameter placeholder.

For instance, we write

const routes = [
  {
    path: "/offers/:member?",
    //...
  },
];

//...

to make the member URL parameter optional.

Conclusion

To add optional parameters with Vue Router, we can append a ? after the route parameter placeholder.

Categories
Vue Answers

How to use setTimeout in Vue.js?

Sometimes, we want to use setTimeout in Vue.js.

In this article, we’ll look at how to use setTimeout in Vue.js.

How to use setTimeout in Vue.js?

To use setTimeout in Vue.js, we just call it directly.

For instance, we write

<script>
export default {
  methods: {
    addToBasket() {
      const item = this.photo;
      this.$http.post("/api/buy/addToBasket", item);
      this.basketAddSuccess = true;
      setTimeout(() => (this.basketAddSuccess = false), 2000);
    },
  },
};
</script>

to call setTimeout in addToBasket to run

this.basketAddSuccess = false

after 2000 milliseconds.

Conclusion

To use setTimeout in Vue.js, we just call it directly.

Categories
Vue Answers

How to reload a page with Vue Router?

Sometimes, we want to reload a page with Vue Router

In this article, we’ll look at how to reload a page with Vue Router.

How to reload a page with Vue Router?

To reload a page with Vue Router, we can call this.$router.go().

For instance, we write

this.$router.go()

to reload the page.

this.$router.go() calls window.history.go to reload the page.

Conclusion

To reload a page with Vue Router, we can call this.$router.go().

Categories
Vue Answers

How to retry original request and access original promise with Axios request interceptors?

Sometimes, we want to retry original request and access original promise with Axios request interceptors.

In this article, we’ll look at how to retry original request and access original promise with Axios request interceptors.

How to retry original request and access original promise with Axios request interceptors?

To retry original request and access original promise with Axios request interceptors, we can call axios.request with the error.config object.

For instance, we write

axios.interceptors.response.use(
  (response) => response,
  async (error) => {
    const status = error?.response?.status;
    if (status === 401) {
      await refreshToken(store);
      error.config.headers[
        "Authorization"
      ] = `Bearer ${store.state.auth.token}`;
      error.config.baseURL = undefined;
      return axios.request(error.config);
    }

    return Promise.reject(error);
  }
);

to check if the response status is 401 with

status === 401

Then we call refreshToken and then get the token from the store and set it as the value of the Authorization request header.

And then we call axios.request with the error.config and return the returned promise to retry the request.

Conclusion

To retry original request and access original promise with Axios request interceptors, we can call axios.request with the error.config object.

Categories
Vue Answers

How to redirect to another page with Vue.js?

Sometimes, we want to redirect to another page with Vue.js.

In this article, we’ll look at how to redirect to another page with Vue.js.

How to redirect to another page with Vue.js?

To redirect to another page with Vue.js, we can use the this.$router.push method.

For instance, we write

this.$router.push('home') 

to call push with the 'home' route path to go to /home route.

Conclusion

To redirect to another page with Vue.js, we can use the this.$router.push method.