Categories
Vue Answers

How to access props in mounted function with Vue.js?

Sometimes, we want to access props in mounted function with Vue.js.

In this article, we’ll look at how to access props in mounted function with Vue.js.

How to access props in mounted function with Vue.js?

To access props in mounted function with Vue.js, we add v-if to our component to show the component only when the prop is set.

For instance, we write

<template>
  <div>
    <ChildComponent v-if="testData" :data="testData" />
  </div>
</template>

to render ChildComponent only if the testData reactive property is set.

We set the data prop to testData this means the data prop would be set to testData in the mounted hook since we render the component only when testData is set.

Conclusion

To access props in mounted function with Vue.js, we add v-if to our component to show the component only when the prop is set.

Categories
Vue Answers

How to close dialog when ESC key is pressed with Vuetify and Vue.js?

Sometimes, we want to close dialog when ESC key is pressed with Vuetify and Vue.js.

In this article, we’ll look at how to close dialog when ESC key is pressed with Vuetify and Vue.js.

How to close dialog when ESC key is pressed with Vuetify and Vue.js?

To close dialog when ESC key is pressed with Vuetify and Vue.js, we can add a keydown event listener to the dialog.

For instance, we write

<template>
  <div>
    <v-dialog v-model="dialog" @keydown.esc="dialog = false"></v-dialog>
  </div>
</template>

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

to listen for esc key presses with the @keydown.esc directive.

And if the event is triggered, we set dialog to false to close the dialog.

We set v-model to dialog so that the dialog is open only when dialog is true.

Conclusion

To close dialog when ESC key is pressed with Vuetify and Vue.js, we can add a keydown event listener to the dialog.

Categories
Vue Answers

How to create a simple 10 seconds countdown in Vue.js?

Sometimes, we want to create a simple 10 seconds countdown in Vue.js.

In this article, we’ll look at how to create a simple 10 seconds countdown in Vue.js.

How to create a simple 10 seconds countdown in Vue.js?

To create a simple 10 seconds countdown in Vue.js, we can use the setInterval.

For instance, we write

<template>
  <div>
    {{ countDown }}
  </div>
</template>

<script>
export default {
  data() {
    return {
      countDown: 10,
    };
  },
  method: {
    countDownTimer() {
      const timer = setInterval(() => {
        if (this.countDown > 0) {
          this.countDown--;
        } else {
          clearInterval(timer);
        }
      }, 1000);
    },
  },
  mounted() {
    this.countDownTimer();
  },
};
</script>

to create the countDownTimer method.

In it, we call setInterval with a callback that checks if this.countDown is bigger than 0.

If it is, we decrement this.countDown by 1.

Otherwise, we call clearInterval to clear the timer.

The callback runs every 1000 milliseconds which is 1 second.

We call countDownTimer in the mounted hook to run the code when the component is mounted.

The countDown value is displayed in the template.

Conclusion

To create a simple 10 seconds countdown in Vue.js, we can use the setInterval.

Categories
Vue Answers

How to make the v-container full width with Vuetify?

Sometimes, we want to make the v-container full width with Vuetify.

In this article, we’ll look at how to make the v-container full width with Vuetify.

How to make the v-container full width with Vuetify?

To make the v-container full width with Vuetify, we just add the fluid prop.

For instance, we write

<template>
  <v-content class="yellow">
    <v-container fluid>
      <v-layout>
        <router-view></router-view>
      </v-layout>
    </v-container>
  </v-content>
</template>

to add the fluid prop to the v-container to make it fill the width of the v-content component.

Conclusion

To make the v-container full width with Vuetify, we just add the fluid prop.

Categories
Vue Answers

How to access props in mounted function with Vue.js?

Sometimes, we want to access props in mounted function with Vue.js.

In this article, we’ll look at how to access props in mounted function with Vue.js.

How to access props in mounted function with Vue.js?

To access props in mounted function with Vue.js, we can get the value from this.

For instance, we write

<script>
//...
export default {
  //...
  props: ["subscriptions"],
  async mounted() {
    await this.$nextTick();
    console.log(this.subscriptions);
  },
  //...
};
</script>

to register the subscriptions prop with

props: ["subscriptions"]

Then we get the subscriptions prop value in the mounted hook with

this.subscriptions