Categories
Vue Answers

How to use Vue.js with Nginx?

Sometimes, we want to use Vue.js with Nginx.

In this article, we’ll look at how to use Vue.js with Nginx.

How to use Vue.js with Nginx?

To use Vue.js with Nginx, we should redirect all Vue.js app URLs to index.html.

For instance, we write

location / {
  try_files $uri $uri/ /index.html;
}

in our Nginx config to redirect all Vue app URLs to index.html.

Then we can use history mode with Vue Router to remove the hash by writing

const router = new VueRouter({
  mode: "history",
  routes: [
    //...
  ],
});

by setting mode to 'history' when we’re creating a VueRouter instance.

Conclusion

To use Vue.js with Nginx, we should redirect all Vue.js app URLs to index.html.

Categories
Vue Answers

How to clear an input in a Vue.js form?

Sometimes, we want to clear an input in a Vue.js form.

In this article, we’ll look at how to clear an input in a Vue.js form.

How to clear an input in a Vue.js form?

To clear an input in a Vue.js form, we can set our v-model values to an empty string.

For instance, we write

<template>
  <div>
    <form id="todo-field" @submit.prevent="submitForm">
      <input type="text" v-model="name" />
    </form>
  </div>
</template>

<script>
export default {
  //...
  data() {
    return {
      name: "",
    };
  },
  methods: {
    submitForm(event) {
      this.name = "";
    },
  },
  //...
};
</script>

to add a form with an input that has its input value to name.

We call submitForm when we submit the form.

In submitForm, we set this.name to an empty string to empty string when we submit the form.

Conclusion

To clear an input in a Vue.js form, we can set our v-model values to an empty string.

Categories
Vue Answers

How to fix the ‘document is not defined’ error in Nuxt.js?

Sometimes, we want to fix the ‘document is not defined’ error in Nuxt.js.

In this article, we’ll look at how to fix the ‘document is not defined’ error in Nuxt.js.

How to fix the ‘document is not defined’ error in Nuxt.js?

To fix the ‘document is not defined’ error in Nuxt.js, we can wrap our client side code with the client-only component.

For instance, we write

<template>
  <div>
    <client-only placeholder="loading...">
      <your-component> ... </your-component>
    </client-only>
  </div>
</template>

to wrap the the client side only your-component component with client-only so that it’s only rendered in the browser.

document is defined only in the browser, so we need client-only to avoid the error.

Conclusion

To fix the ‘document is not defined’ error in Nuxt.js, we can wrap our client side code with the client-only component.

Categories
Vue Answers

How to access the window object in Vue.js?

Sometimes, we want to access the window object in Vue.js.

In this article, we’ll look at how to access the window object in Vue.js.

How to access the window object in Vue.js?

To access the window object in Vue.js, we can add window properties as global properties.

For instance, in Vue 2, we write

import Vue from 'vue';

Vue.prototype.$authUser = window.authUser;

to add the window.authUser value into the Vue prototype.

Then we can access routes in our component with

this.$authUser 

In Vue 3, we add the same property into app.config.globalProperties.

We write

app.config.globalProperties.$authUser = window.authUser;

Then we can access routes in our component with

this.$authUser 

Conclusion

To access the window object in Vue.js, we can add window properties as global properties.

Categories
Vue Answers

How to use async and await in Vue.js?

Sometimes, we want to use async and await in Vue.js.

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

How to use async and await in Vue.js?

To use async and await in Vue.js, we can add async and await in our component methods and hooks.

For instance, we write

<script>
export default {
  //...
  async created() {
    await this.getA();
    console.log(1);
    this.getB();
  },
  methods: {
    getA: async () => {
      return $axios.post(`/getA`, params);
    },
    getB: () => {
      console.log(3);
    },
  },
  //...
};
</script>

to make the created hook an async method.

We use await to wait for the getA method to finish before running the rest of the code.

getA returns a promise so we can use await to wait for it to finish.

Conclusion

To use async and await in Vue.js, we can add async and await in our component methods and hooks.