Categories
Vue Answers

How to get route URL parameters in a page with Nuxt.js?

Sometimes, we want to get route URL parameters in a page with Nuxt.js.

In this article, we’ll look at how to get route URL parameters in a page with Nuxt.js.

How to get route URL parameters in a page with Nuxt.js?

To get route URL parameters in a page with Nuxt.js, we can get them from the this.$route.params object.

For instance, we write

<script>
//...
export default {
  //...
  mounted() {
    console.log(this.$route.params.slug);
  },
  //...
};
</script>

to get the value of the slug URL parameter with this.$route.params.slug).

Conclusion

To get route URL parameters in a page with Nuxt.js, we can get them from the this.$route.params object.

Categories
Vue Answers

How to scroll to top of new page route with Vue.js?

Sometimes, we want to scroll to top of new page route with Vue.js.

In this article, we’ll look at how to scroll to top of new page route with Vue.js.

How to scroll to top of new page route with Vue.js?

To scroll to top of new page route with Vue.js, we can add the scrollBehavior method when we’re creating the Vue Router instance.

For instance, we write

export default new Router({
  scrollBehavior() {
    return { x: 0, y: 0 };
  },
  routes: [
    {
      path: "/",
      name: "Home",
      component: Home,
    },
  ],
  mode: "history",
});

to add the scrollBehavior method that returns an object with the x and y coordinates to scroll to when we change routes.

Conclusion

To scroll to top of new page route with Vue.js, we can add the scrollBehavior method when we’re creating the Vue Router instance.

Categories
Vue Answers

How to submit a form, redirect to a new route and pass the parameters in Vue.js?

Sometimes, we want to submit a form, redirect to a new route and pass the parameters in Vue.js.

In this article, we’ll look at how to submit a form, redirect to a new route and pass the parameters in Vue.js.

How to submit a form, redirect to a new route and pass the parameters in Vue.js?

To submit a form, redirect to a new route and pass the parameters in Vue.js, we can call this.$router.push in the submit event handler.

For instance, we write

<template>
  <div id="app">
    <form>
      <input type="text" name="foobar" v-model="foobar" />
      <button type="submit" @click.stop.prevent="submit()">Submit</button>
    </form>
  </div>
</template>

<script>
//...
export default {
  //...
  data() {
    return {
      foobar: null,
    };
  },
  methods: {
    submit() {
      const { foobar } = this;
      this.$router.push({ path: "/search", query: { foobar } });
    },
  },
  //...
};
</script>

to add a form with a submit button.

We call submit when we submit the form.

In it, we call this.$router.push with an object with the route path and the query string parameters in the query property.

We add the prevent to the @click directive to stop the default server side form submission behavior.

Conclusion

To submit a form, redirect to a new route and pass the parameters in Vue.js, we can call this.$router.push in the submit event handler.

Categories
Vue Answers

How to get Vuex state from a JavaScript file with Vue.js?

Sometimes, we want to get Vuex state from a JavaScript file with Vue.js.

In this article, we’ll look at how to get Vuex state from a JavaScript file with Vue.js.

How to get Vuex state from a JavaScript file with Vue.js?

To get Vuex state from a JavaScript file with Vue.js, we can import the Vuex store and use it directly.

For instance, we write

import { store } from "../store";

export function getAuth() {
  return store.state.authorization.AUTH_STATE;
}

to import the store from a module.

Then we get a state value from the store with store.state.authorization.AUTH_STATE.

Conclusion

To get Vuex state from a JavaScript file with Vue.js, we can import the Vuex store and use it directly.

Categories
Vue Answers

How to add a class conditionally in Vue.js?

Sometimes, we want to add a class conditionally in Vue.js.

In this article, we’ll look at how to add a class conditionally in Vue.js.

How to add a class conditionally in Vue.js?

To add a class conditionally in Vue.js, we can add the :class directive.

For instance, we write

<template>
  <div id="app">
    <div :class="condition ? 'class-if-is-true' : 'else-class'"></div>
  </div>
</template>

to set the class prop of the div to 'class-if-is-true' if condition is true.

Otherwise, we set class to 'else-class'.

Conclusion

To add a class conditionally in Vue.js, we can add the :class directive.