Categories
Vue Answers

How to link to an external website with Vue.js and Vue Router?

Sometimes, we want to link to an external website with Vue.js and Vue Router.

In this article, we’ll look at how to link to an external website with Vue.js and Vue Router.

How to link to an external website with Vue.js and Vue Router?

To link to an external website with Vue.js and Vue Router, we can just use a regular anchor element.

For instance, we write

<template>
  <div>
    <a :href="websiteUrl" target="_blank">
      {{ url }}
    </a>
  </div>
</template>

to set the href prop to websiteUrl where websiteUrl is an external URL.

We set target to _blank to open a new tab when we click on the link.

Conclusion

To link to an external website with Vue.js and Vue Router, we can just use a regular anchor element.

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.