Categories
Vue Answers

How to open a Vuetify dialog from a component template in Vue.js?

Sometimes, we want to open a Vuetify dialog from a component template in Vue.js.

In this article, we’ll look at how to open a Vuetify dialog from a component template in Vue.js.

How to open a Vuetify dialog from a component template in Vue.js?

To open a Vuetify dialog from a component template in Vue.js, we can use v-model.

For instance, we write

<template>
  <v-dialog v-model="show" max-width="500px">
    <v-card>
      <v-card-actions>
        <v-btn color="primary" flat @click.stop="show = false">Close</v-btn>
      </v-card-actions>
    </v-card>
  </v-dialog>
</template>

<script>
export default {
  props: {
    value: Boolean,
  },
  computed: {
    show: {
      get() {
        return this.value;
      },
      set(value) {
        this.$emit("input", value);
      },
    },
  },
};
</script>

to add the v-dialog into the ScheduleForm.vue file.

We emit the show value to the parent when we open or close the dialog.

And we get the show value from the parent via the value prop.

If show is true then the dialog is shown.

Then we write

<template>
  <v-btn color="accent" large @click.stop="showScheduleForm = true" />
  <ScheduleForm v-model="showScheduleForm" />
</template>

<script>
import ScheduleForm from "~/components/ScheduleForm";

export default {
  data() {
    return {
      showScheduleForm: false,
    };
  },
  components: {
    ScheduleForm,
  },
};
</script>

to import and register it in the parent component.

Then we set v-model to showScheduleForm to show the dialog if it’s set to true.

Conclusion

To open a Vuetify dialog from a component template in Vue.js, we can use v-model.

Categories
JavaScript Answers

How to use JavaScript execCommand to copy hidden text to clipboard?

Sometimes, we want to use JavaScript execCommand to copy hidden text to clipboard.

In this article, we’ll look at how to use JavaScript execCommand to copy hidden text to clipboard.

How to use JavaScript execCommand to copy hidden text to clipboard?

To use JavaScript execCommand to copy hidden text to clipboard, we convert it to a text input first.

For instance, we write

<input id="dummy" name="dummy" type="hidden" />

to add a hidden input.

Then we write

const copyText = document.getElementById("dummy");
copyText.type = "text";
copyText.select();
document.execCommand("copy");
copyText.type = "hidden";

to get the hidden input with getElementById.

We then set the type of the input to 'text'.

Next we call select to select the text inside.

Then we call execCommand with 'copy' to copy the selected text into the clipboard.

And finally we set its type back to 'hidden'.

Conclusion

To use JavaScript execCommand to copy hidden text to clipboard, we convert it to a text input first.

Categories
JavaScript Answers

How to convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript?

Sometimes, we want to convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript.

In this article, we’ll look at how to convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript.

How to convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript?

To convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript, we use the toLoclaeTimeString method.

For instance, we write

const timeString = "18:00:00";

const timeString12hr = new Date(
  "1970-01-01T" + timeString + "Z"
).toLocaleTimeString("en-US", {
  timeZone: "UTC",
  hour12: true,
  hour: "numeric",
  minute: "numeric",
});

to create a date from timeString.

Then we call toLocaleTimeString with the locale and an object with the options.

We set hour12 to true to make it return a 12 hour time string.

Conclusion

To convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript, we use the toLoclaeTimeString method.

Categories
JavaScript Answers

How to assign value from successful promise resolve to external variable with JavaScript?

Sometimes, we want to assign value from successful promise resolve to external variable with JavaScript.

In this article, we’ll look at how to assign value from successful promise resolve to external variable with JavaScript.

How to assign value from successful promise resolve to external variable with JavaScript?

To assign value from successful promise resolve to external variable with JavaScript, we use async and await.

For instance, we write

const myFunction = async () => {
  vm.feed = await getFeed();
  // ...
};

to get the resolve value of the promise returned by getFeed with await.

Then we assign the returned value to vm.feed.

We have to use await inside async functions.

Conclusion

To assign value from successful promise resolve to external variable with JavaScript, we use async and await.

Categories
JavaScript Answers

How to prevent HTML and script injections in JavaScript?

Sometimes, we want to prevent HTML and script injections in JavaScript.

In this article, we’ll look at how to prevent HTML and script injections in JavaScript.

How to prevent HTML and script injections in JavaScript?

To prevent HTML and script injections in JavaScript, we escape the string with the JavaScript code.

For instance, we write

const escapedHtml = html.replace(/</g, "&lt;").replace(/>/g, "&gt;");

to call replace to replace the opening and closing brackets with "&lt;" and "&gt;" respectively with replace.

The g flag will make replace replace all matches.

The escaped string is returned and the string won’t have valid code so it can’t be run.

Conclusion

To prevent HTML and script injections in JavaScript, we escape the string with the JavaScript code.