Categories
Vue Answers

How to upload image in Vue.js?

Sometimes, we want to upload image in Vue.js.

In this article, we’ll look at how to upload image in Vue.js.

How to upload image in Vue.js?

To upload image in Vue.js, we can add a file input, watch for file selection, and upload the file when a file is selected.

For instance, we write

<template>
  <input type="file" accept="image/*" @change="uploadImage" />
</template>

<script>
//...
export default {
  //...
  methods: {
    async uploadImage(event) {
      const URL = "http://foobar.com/upload";
      const data = new FormData();
      data.append("name", "my-picture");
      data.append("file", event.target.files[0]);
      const config = {
        header: {
          "Content-Type": "image/png",
        },
      };

      await axios.put(URL, data, config);
    },
  },
  //...
};
</script>

to set the change event handler to the uploadImage method on the file input.

In uploadImage, we put the image in the form data body with

data.append("file", event.target.files[0]);

And then we call axios.put to make a PUT request with the URL, form data data and the config for the request.

Conclusion

To upload image in Vue.js, we can add a file input, watch for file selection, and upload the file when a file is selected.

Categories
Vue Answers

How to set the selected option of a drop down in Vue.js?

Sometimes, we want to set the selected option of a drop down in Vue.js.

In this article, we’ll look at how to set the selected option of a drop down in Vue.js.

How to set the selected option of a drop down in Vue.js?

To set the selected option of a drop down in Vue.js, we can add the selected attribute directly to the selected option.

For instance, we write

<template>
  <select v-model="selected" required @change="changeLocation">
    <option selected>Choose Province</option>
    <option v-for="option in options" :value="option.id">
      {{ option.name }}
    </option>
  </select>
</template>

to add the selected attribute to the first option.

Conclusion

To set the selected option of a drop down in Vue.js, we can add the selected attribute directly to the selected option.

Categories
Vue Answers

How to use the custom filter prop in data tables in Vuetify?

Sometimes, we want to use the custom filter prop in data tables in Vuetify.

In this article, we’ll look at how to use the custom filter prop in data tables in Vuetify.

How to use the custom filter prop in data tables in Vuetify?

To use the custom filter prop in data tables in Vuetify, we can return an array with the filtered items in a computed property.

For instance, we write

<template>
  <div>
    <v-data-table :headers="headers" :items="filteredItems" hide-actions>
    </v-data-table>
  </div>
</template>

<script>
//...
export default {
  //...
  data() {
    return {
      food: [
        { name: "Bakchoi", type: "vegetable", calories: 100 },
        { name: "Pork", type: "meat", calories: 200 },
        { name: "Chicken Thigh", type: "meat", calories: 300 },
        { name: "Watermelon", type: "fruit", calories: 10 },
      ],
      headers: [
        { text: "Name", align: "left", value: "name" },
        { text: "Food Type", align: "left", value: "type" },
        { text: "Calories", align: "left", value: "calories" },
      ],
      foodType: null,
    };
  },
  computed: {
    filteredItems() {
      return this.food.filter((i) => {
        return !this.foodType || i.type === this.foodType;
      });
    },
  },
  //...
};
</script>

to add the food array that we filter to get the the filteredItems computed property.

And we use filteredItems as the data for the items prop in the v-data-table component.

Conclusion

To use the custom filter prop in data tables in Vuetify, we can return an array with the filtered items in a computed property.

Categories
Vue Answers

How to show JSON result with Vue.js?

Sometimes, we want to show JSON result with Vue.js.

In this article, we’ll look at how to show JSON result with Vue.js.

How to show JSON result with Vue.js?

To show JSON result with Vue.js, we can use the JSON.stringify method with the pre element.

For instance, we write

<template>
  <div>
    <pre>{{ JSON.stringify(yourObject, null, 2) }}</pre>
  </div>
</template>

to pretty print the yourObject JSON object reactive property by calling JSON.stringify with it and 2 to indent each level by 2 spaces.

And we put the returned JSON string in a pre element so that all the whitespaces are rendered.

Conclusion

To show JSON result with Vue.js, we can use the JSON.stringify method with the pre element.

Categories
Vue Answers

How to fix @keyup event not working with the enter key on a button in Vue.js?

Sometimes, we want to fix @keyup event not working with the enter key pressed on a button in Vue.js.

In this article, we’ll look at how to fix @keyup event not working with the enter key pressed on a button in Vue.js.

How to fix @keyup event not working with the enter key pressed on a button in Vue.js?

To fix @keyup event not working with the enter key pressed on a button in Vue.js, we should replace @keyup with @click to listen to the click event.

For instance, we write

<template>
  <div>
    <button @click="callEvent">Enter</button>
  </div>
</template>

to listen to the click event with @click="callEvent".

Pressing enter on a button will trigger the click event.

Conclusion

To fix @keyup event not working with the enter key pressed on a button in Vue.js, we should replace @keyup with @click to listen to the click event.