Categories
Vue Answers

How to add Bootstrap to Vue CLI project?

Sometimes, we want to add bootstrap to Vue CLI project.

In this article, we’ll look at how to add bootstrap to Vue CLI project.

How to add Bootstrap to Vue CLI project?

To add bootstrap to Vue CLI project, we can install the Bootstrap and jQuery packages and then import all the files from the packages.

To install the packages, we run

npm install bootstrap popper.js jquery

Then we import them in main.js by writing

import 'bootstrap/dist/css/bootstrap.min.css'
import 'jquery/src/jquery.js'
import 'bootstrap/dist/js/bootstrap.min.js'

Importing them in main.js will make them available in the whole app.

Conclusion

To add bootstrap to Vue CLI project, we can install the Bootstrap and jQuery packages and then import all the files from the packages.

Categories
Vue Answers

How to access the getter from another Vuex module?

Sometimes, we want to access the getter from another Vuex module.

In this article, we’ll look at how to access the getter from another Vuex module.

How to access the getter from another Vuex module?

To access the getter from another Vuex module, we can get them from the rootGetters parameter.

For instance, we write

const store = new Vuex.Store({
  //...
  getters: {
    someGetter: (state, getters, rootState, rootGetters) => {
      //...
      rootGetters.someOtherGetter;
      rootGetters["bar/someOtherGetter"];
      //...
    },
    //...
  },
  //...
});

to create a Vuex.Store instance with the someGetter getter.

In it, we get the someOtherGetter getter with rootGetters.someOtherGetter.

And we get the namespaced bar/someOtherGetter getter with rootGetters["bar/someOtherGetter"].

Conclusion

To access the getter from another Vuex module, we can get them from the rootGetters parameter.

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.