Categories
Vue Answers

How to reload route in Vue Router?

Sometimes, we want to reload route in Vue Router.

In this article, we’ll look at how to reload route in Vue Router.

How to reload route in Vue Router?

To reload route in Vue Router, we call this.$router.go in our component.

We call it with no argument to do the same thing as windwo.history.go called with no argument, which reloads the page.

Conclusion

To reload route in Vue Router, we call this.$router.go in our component.

Categories
Vue Answers

How to reference static assets within Vue.js and JavaScript?

Sometimes, we want to reference static assets within Vue.js and JavaScript.

In this article, we’ll look at how to reference static assets within Vue.js and JavaScript.

How to reference static assets within Vue.js and JavaScript?

To reference static assets within Vue.js and JavaScript, we can use @/ as the alias of the src/ folder.

For instance, we write

<img src="@/assets/images/home.png"/>

to set the src attribute of the img element to @/assets/images/home.png which is the same as src/assets/images/home.png in the Vue project folder.

Conclusion

To reference static assets within Vue.js and JavaScript, we can use @/ as the alias of the src/ folder.

Categories
Vue Answers

How to detect mouseover or hover with Vue.js?

Sometimes, we want to detect mouseover or hover with Vue.js.

In this article, we’ll look at how to detect mouseover or hover with Vue.js.

How to detect mouseover or hover with Vue.js?

To detect mouseover or hover with Vue.js, we can listen for the mouseover and mouseleave events.

For instance, we write

<template>
  <div @mouseover="upHere = true" @mouseleave="upHere = false">
    <h2>Something</h2>
    <some-component v-show="upHere"></some-component>
  </div>
</template>

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

to listen to the mouseover and mouseleave events triggered from the div with @mouseover and @mouseleave.

We set upHere to true if the mouse pointer is in the div and we set upHere to false when the mouse pointer leaves the div.

And we add the upHere reactive property to the object returned by data.

Conclusion

To detect mouseover or hover with Vue.js, we can listen for the mouseover and mouseleave events.

Categories
Vue Answers

How to open a link in a new tab with Vue Router?

Sometimes, we want to open a link in a new tab with Vue Router.

In this article, we’ll look at how to open a link in a new tab with Vue Router.

How to open a link in a new tab with Vue Router?

To open a link in a new tab with Vue Router, we can use the this.$router.resolve method to get the full object with the URL of a given route.

Then we call window.open with the route object’s href property URL and '_blank' to open the URL in the new tab.

For instance, in our component method or hook, we write

const routeData = this.$router.resolve({
  name: "routeName",
  query: { data: "someData" },
});
window.open(routeData.href, "_blank");

to call this.$router.resolve with an object that has the route name and query query parameters object.

resolve returns an object with the href property that has the full URL of the route.

Then we call window.open with routeData.href and '_blank' to open the routeData.href URL in a new tab.

Conclusion

To open a link in a new tab with Vue Router, we can use the this.$router.resolve method to get the full object with the URL of a given route.

Then we call window.open with the route object’s href property URL and '_blank' to open the URL in the new tab.

Categories
Vue Answers

How to fix [Vue warn]: Property or method is not defined on the instance but referenced during render with Vue.js?

Sometimes, we want to fix [Vue warn]: Property or method is not defined on the instance but referenced during render with Vue.js.

In this article, we’ll look at how to fix [Vue warn]: Property or method is not defined on the instance but referenced during render with Vue.js.

How to fix [Vue warn]: Property or method is not defined on the instance but referenced during render with Vue.js?

To fix [Vue warn]: Property or method is not defined on the instance but referenced during render with Vue.js, we should make sure the reactive property is defined in the component.

For instance, we write

<template>
  <span>{{ name }}</span>
</template>

<script>
export default {
  name: "MyComponent",
  data() {
    return {
      name: "",
    };
  },
};
</script>

to add the name reactive property in the object returned by data.

Then we can reference name in the template without errors.

Conclusion

To fix [Vue warn]: Property or method is not defined on the instance but referenced during render with Vue.js, we should make sure the reactive property is defined in the component.