Categories
Vue Answers

How to proxy requests to a separate backend server with Vue CLI?

Sometimes, we want to proxy requests to a separate backend server with Vue CLI.

In this article, we’ll look at how to proxy requests to a separate backend server with Vue CLI.

How to proxy requests to a separate backend server with Vue CLI?

To proxy requests to a separate backend server with Vue CLI, we can add the proxy options to vue.config.js.

For instance, we write

module.exports = {
  devServer: {
    proxy: {
      "/gists": {
        target: "https://api.github.com",
        secure: false,
      },
    },
  },
};

in vue.config.js to proxy all requests to /gists to https://api.github.com.

Conclusion

To proxy requests to a separate backend server with Vue CLI, we can add the proxy options to vue.config.js.

Categories
Vue Answers

How to use v-model on an array with multiple inputs with Vue.js?

Sometimes, we want to use v-model on an array with multiple inputs with Vue.js.

In this article, we’ll look at how to use v-model on an array with multiple inputs with Vue.js.

How to use v-model on an array with multiple inputs with Vue.js?

To use v-model on an array with multiple inputs with Vue.js, we can use the v-for directive to render the array.

Then we use v-model to bind to the properties of each object.

For instance, we write

<template>
  <div id="app">
    <div v-for="(find, index) in finds">
      <input v-model="find.value" :key="find.id" />
    </div>
    <button @click="addFind">New</button>
  </div>
</template>

<script>
//...
export default {
  //...
  data() {
    return {
      finds: [],
    };
  },
  methods: {
    addFind() {
      this.finds.push({ id: uuidv4(), value: "" });
    },
  },
  //...
};
</script>

to use v-for to render the finds array into divs.

Then in each div, we add an input that binds the find.value property to the input value.

We set key to the unique find.id value so Vue can distinguish the rendered items.

Then in addFind, we call this.finds.push with an object with the unique id and value set to an empty string.

addFind is called when we click the New button.

Conclusion

To use v-model on an array with multiple inputs with Vue.js, we can use the v-for directive to render the array.

Then we use v-model to bind to the properties of each object.

Categories
Vue Answers

How to only show slot if it has content with Vue.js?

Sometimes, we want to only show slot if it has content with Vue.js.

In this article, we’ll look at how to only show slot if it has content with Vue.js.

How to only show slot if it has content with Vue.js?

To only show slot if it has content with Vue.js, we can check the this.$slots property in our component.

For instance, we write

<template>
  <div id="app">
    <div class="panel-footer" v-if="hasFooterSlot">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script>
//...
export default {
  //...
  computed: {
    hasFooterSlot() {
      return !!this.$slots.footer;
    },
  },
  //...
};
</script>

to check if the footer slot is added with !!this.$slots.footer.

We put this in the hasFooterSlot computed property, so we can use that in our template with v-if to conditionally display the footer slot with

<div class="panel-footer" v-if="hasFooterSlot">
  <slot name="footer"></slot>
</div>

Conclusion

To only show slot if it has content with Vue.js, we can check the this.$slots property in our component.

Categories
Vue Answers

How to add a page transition fade effect with Vue Router and Vue.js?

Sometimes, we want to add a page transition fade effect with Vue Router and Vue.js.

In this article, we’ll look at how to add a page transition fade effect with Vue Router and Vue.js.

How to add a page transition fade effect with Vue Router and Vue.js?

To add a page transition fade effect with Vue Router and Vue.js, we wrap the transition component around the router-view component and add our transition styles.

For instance, we write

<template>
  <div id="app">
    <transition name="fade">
      <router-view></router-view>
    </transition>
  </div>
</template>

<script>
//...
export default {
  //...
};
</script>

<style>
.fade-enter-active,
.fade-leave-active {
  transition-property: opacity;
  transition-duration: 0.25s;
}

.fade-enter-active {
  transition-delay: 0.25s;
}

.fade-enter,
.fade-leave-active {
  opacity: 0;
}
</style>

to add the the transition component and set its name prop to 'fade'.

We wrap it around router-view so that we see the transition effect when we change routes.

And we add the transition effect styles in the style tag.

The name prop should be the prefix for the classes listed in the style tag so the transition styles would be applied.

Conclusion

To add a page transition fade effect with Vue Router and Vue.js, we wrap the transition component around the router-view component and add our transition styles.

Categories
Vue Answers

What’s the difference between @click and v-on:click in Vue.js?

In this article, we’ll look at what’s the difference between @click and v-on:click in Vue.js.

What’s the difference between @click and v-on:click in Vue.js?

There’re no difference between @click and v-on:click in Vue.js.

@ is a shorthand for v-on:.

So

<a v-on:click="doSomething"></a>

is the same as

<a @click="doSomething"></a>

They both call the doSomething method when we click on the anchor element.

Conclusion

There’re no difference between @click and v-on:click in Vue.js.