Categories
Top Vue Packages

Top Vue Packages for Adding Icons, Rich Text Editor, Image Upload, and Event Calendar

Vue.js is an easy to use web app framework that we can use to develop interactive front end apps. In this article, we’ll look at the best packages for adding icons, rich text editor, image upload, and event calendar.

vue-fontawesome

vue-fontawesome is a set of components we can use to add Font Awesome icons into our Vue app.

To use it, we run:

npm i --save @fortawesome/fontawesome-svg-core
npm i --save @fortawesome/free-solid-svg-icons
npm i --save @fortawesome/vue-fontawesome

to install all the required packages.

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import { library } from "@fortawesome/fontawesome-svg-core";
import { faUserSecret } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
library.add(faUserSecret);

Vue.component("font-awesome-icon", FontAwesomeIcon);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div id="app">
    <font-awesome-icon icon="user-secret"/>
  </div>
</template>

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

We call library.add to register the icon we want to use.

We use the font-awesome-icon with the icon prop to specify the icon we want to use.

It works with both the free and pro versions of Font Awesome.

We just have to install different packages for the pro version.

vue-event-calendar

vue-event-calendar is a very simple calendar library for Vue.

To use it, we run:

npm i vue-event-calendar

to install it.

Then we can write:

main.js

import Vue from "vue";
import App from "./App.vue";
import "vue-event-calendar/dist/style.css";
import vueEventCalendar from "vue-event-calendar";
Vue.use(vueEventCalendar, { locale: "en" });
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <vue-event-calendar :events="demoEvents"></vue-event-calendar>
</template>

<script>
export default {
  data() {
    return {
      demoEvents: [
        {
          date: "2020/11/12",
          title: "eat"
        },
        {
          date: "2020/12/15",
          title: "drink",
          desc: "description",
          customClass: "disabled highlight"
        }
      ]
    };
  }
};
</script>

to use it.

We use the vue-event-calendar to add a calendar to our app.

Then we set the events prop to an array with objects that have the date and title properties to display events.

date is the date of the event. title is the title of the event.

desc is the description.

customClass are classes that we can add to let us style it with our own CSS.

It emits events when the day or month changed, so we can add listeners to listen to those.

We can change the locale with the locale option.

And we can navigate months and dates with the this.$EventCalendar object.

We can write:

this.$EventCalendar.nextMonth()

to move to the next month and:

this.$EventCalendar.preMonth()

to move to the previous month.

And we can write:

this.$EventCalendar.toDate('2020/11/12')

to move to a given date.

vue-img-inputer

vue-img-inputer is a file uploader library for Vue apps.

To use it, we run:

npm i vue-img-inputer

to install it.

Then we write:

main.js

import Vue from "vue";
import App from "./App.vue";
import ImgInputer from "vue-img-inputer";
import "vue-img-inputer/dist/index.css";

Vue.component("ImgInputer", ImgInputer);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <img-inputer v-model="file" theme="light" size="large"/>
</template>

<script>
export default {
  data(){
    return {
      file: undefined
    }
  }
};
</script>

to use it.

We register the component and add the CSS.

Then we use the img-inputer component and bind the selected file with v-model .

There are also many other options we can change, like text for errors, URL for images, icons, and more.

It also emits events for when the file changed or file size exceeded.

vue-pell-editor

vue-pell-editor is a rich text editor for Vue apps.

To use it, we install it by running:

npm i vue-pell-editor

Then we write:

main.js

import Vue from "vue";
import App from "./App.vue";
import VuePellEditor from "vue-pell-editor";

Vue.use(VuePellEditor);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div>
    <vue-pell-editor
      v-model="editorContent"
      :actions="editorOptions"
      :placeholder="editorPlaceholder"
      :style-with-css="false"
      default-paragraph-separator="p"
    />
    <p v-html="editorContent"></p>
  </div>
</template>

<script>
export default {
  data: () => ({
    editorContent: "",
    editorOptions: [
      "bold",
      "underline",
      {
        name: "italic",
        result: () => window.pell.exec("italic")
      },
      {
        name: "custom",
        icon: "<b><u><i>C</i></u></b>",
        title: "Custom Action",
        result: () => console.log("YOLO")
      },
      {
        name: "image",
        result: () => {
          const url = window.prompt("image URL");
          if (url) window.pell.exec("insertImage", ensureHTTP(url));
        }
      },
      {
        name: "link",
        result: () => {
          const url = window.prompt("link URL");
          if (url) window.pell.exec("createLink", ensureHTTP(url));
        }
      }
    ],
    editorPlaceholder: "Write something amazing..."
  }),
  methods: {}
};
</script>

to use it.

We add buttons individually with the editorOptions , which we set as the value of the actions prop.

placeholder has the placeholder.

v-model binds to the entered content.

style-with-css indicates whether we want to style with CSS.

Conclusion

vue-fontawesome lets us use Font Awesome icons. vue-event-calendar displays an event calendar. vue-img-inputer lets us add image upload capability to our app, vue-pell-editor is a rich text editor for Vue apps.

Categories
Top Vue Packages

Top Vue Packages for Displaying Messages, Lazy Loading Images, and Modals

In this article, we’ll look at the best packages for displaying messages, lazy loading images, and adding modals.

vue-toastr

vue-toastr is a toast component for Vue apps.

To use it, we run:

npm i vue-toastr

to install it.

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueToastr from "vue-toastr";
Vue.use(VueToastr, {});

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div></div>
</template>

<script>
export default {
  mounted() {
    this.$toastr.defaultPosition = "toast-top-left";
    this.$toastr.s("<b>hello</b>");
  }
};
</script>

We create a toast by registering a plugin.

Then we use the $toastr property to set the default position and display the toast with the s method.

HTML is supported.

Other options like timeout, styles, etc. can be set.

vue-thin-modal

If we want to add models to our app easily, we can use the vue-thin-modal package to do it.

To install it, we run:

npm i vue-thin-modal

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueThinModal from "vue-thin-modal";
import "vue-thin-modal/dist/vue-thin-modal.css";

Vue.use(VueThinModal);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div>
    <button type="button" @click="open">Open Modal</button>
    <modal name="demo">
      <div class="basic-modal">
        <h1 class="title">Title</h1>
        <button class="button" type="button" @click="close">Close Modal</button>
      </div>
    </modal>
  </div>
</template>

<script>
export default {
  methods: {
    open() {
      this.$modal.push("demo");
    },

    close() {
      this.$modal.pop();
    }
  }
};
</script>

We register the component and import the CSS.

Then we use the modal component to add the modal.

this.$modal.push opens the modal.

The argument is the value of the name prop of modal .

To close it, we call the pop method.

v-lazy-image

v-lazy-image is an image component that loads the image only when it’s shown on the screen.

To use it, we run:

npm i v-lazy-image

Then we can write:

main.js

import Vue from "vue";
import App from "./App.vue";
import { VLazyImagePlugin } from "v-lazy-image";

Vue.use(VLazyImagePlugin);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div>
    <v-lazy-image
      src="http://placekitten.com/200/200"
      src-placeholder="http://placekitten.com/201/201"
    />
  </div>
</template>

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

to use it.

src is the URL of the actual image.

src-placeholder is the URL of the placeholder image.

We can progressively load images.

To do that, we can add transitions effects with CSS.

For instance, we can write:

<template>
  <div>
    <v-lazy-image
      src="http://placekitten.com/200/200"
      src-placeholder="http://placekitten.com/201/201"
    />
  </div>
</template>

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

<style scoped>
.v-lazy-image {
  filter: blur(20px);
  transition: filter 0.7s;
}
.v-lazy-image-loaded {
  filter: blur(0);
}
</style>

We just add transition effects to the v-lazy-image class to add a transition effect when the image loads.

Responsive images are also supported.

vue-flash-message

vue-flash-message is a package that lets us show messages on the screen.

To install it, we run:

npm i vue-flash-message

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueFlashMessage from "vue-flash-message";
Vue.use(VueFlashMessage);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div>
    <flash-message></flash-message>
  </div>
</template>

<script>
export default {
  mounted() {
    this.flash("loaded", "success");
  }
};
</script>

We use the flash-message component to display messages which will be displayed for a few seconds.

We call this.flash to show the message.

The first argument is the content.

The 2nd argument is the type.

We can add the CSS that comes with the package to add styles:

import Vue from "vue";
import App from "./App.vue";
import VueFlashMessage from "vue-flash-message";
import "vue-flash-message/dist/vue-flash-message.min.css";

Vue.use(VueFlashMessage);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

Other kinds of messages include error, warning, and info.

Conclusion

We can use vue-toastr or vue-flash-messag to display messages.

vue-thin-modal is a modal component for Vue apps.

v-lazy-image is a component for lazy loading images.

Categories
Top Vue Packages

Top Vue Packages for Adding Sliders, Highlighting Code, and More

In this article, we’ll look at the best packages for adding auto-scrolling, animating numbers, highlighting code, sliders, and displaying notifications.

vue-seamless-scroll

vue-seamless-scroll lets us add a component that automatically scrolls through its contents.

To use it, we run:

npm i vue-seamless-scroll

to install it.

Then we can write:

<template>
  <vue-seamless-scroll :data="listData" class="seamless-warp">
    <ul class="item">
      <li v-for="item in listData" :key="item">
        <span v-text="item"></span>
      </li>
    </ul>
  </vue-seamless-scroll>
</template>

<script>
import vueSeamlessScroll from "vue-seamless-scroll";

export default {
  components: {
    vueSeamlessScroll
  },
  data() {
    return {
      listData: Array(20)
        .fill()
        .map((_, i) => i)
    };
  }
};
</script>

We register the component.

And then we use the vue-seamless-scroll component.

We set the data prop to set the scroll data.

Then we loop through the entries we want to render.

Whatever is inside will be scrolled through continuously.

It’ll loop so it’ll keep scrolling from beginning to end until we navigate away from the page.

animated-number-vue

animated-number-vue lets us animate numbers in a Vue app.

To use it, we run:

npm i animated-number-vue

to install it.

Then we use it by writing:

<template>
  <animated-number :value="value" :formatValue="formatToPrice" :duration="300"/>
</template>
<script>
import AnimatedNumber from "animated-number-vue";

export default {
  components: {
    AnimatedNumber
  },
  data() {
    return {
      value: 1000
    };
  },
  methods: {
    formatToPrice(value) {
      return `$ ${value.toFixed(2)}`;
    }
  }
};
</script>

We use the animated-number component to display the animated number.

duration is the length of the animation.

value is the end number of the animation.

Vue Carousel 3d

Vue Carousel 3d is a 3D carousel for Vue apps.

To use it, we run:

npm i vue-carousel-3d

to install it.

Then we write:

main.js

import Vue from "vue";
import App from "./App.vue";
import Carousel3d from "vue-carousel-3d";

Vue.use(Carousel3d);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div>
    <carousel-3d>
      <slide :index="0">Slide 1</slide>
      <slide :index="1">Slide 2</slide>
      <slide :index="2">Slide 3</slide>
    </carousel-3d>
  </div>
</template>

<script>
export default {};

to use it.

We use the carousel-3d with at least 3 slides.

The slide components house the slides.

Then we can flip through the slides.

Vue Toast Notification

Vue Toast Notification is a Vue plugin that lets us display toast in our Vue app.

To use it, we install it by running:

npm i vue-toast-notification

Then we can use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueToast from "vue-toast-notification";
import "vue-toast-notification/dist/theme-default.css";

Vue.use(VueToast);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div id="app"></div>
</template>

<script>
export default {
  mounted() {
    this.$toast.open("hello!");
  }
};
</script>

We import the CSS and register the library.

Then we call this.$toast.open to open a basic notification.

We also display notifications for errors, warnings, and info messages.

Also, we can write:

<template>
  <div id="app"></div>
</template>

<script>
export default {
  mounted() {
    this.$toast.open({
      message: "Hello Vue",
      type: "success",
      duration: 5000,
      dismissible: true
    });
  }
};
</script>

to set our own options.

dismissible makes it dismissible.

message is the message.

type is the type of toast.

duration is the length of time to display the toast.

vue-hljs

vue-hljs lets us display code in our Vue app with syntax highlighting.

To use it, we run:

npm i vue-hljs

to install it.

Then we write:

main.js

import Vue from "vue";
import App from "./App.vue";
import vueHljs from "vue-hljs";
import "vue-hljs/dist/vue-hljs.min.css";

Vue.use(vueHljs);
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div id="app">
    <div v-highlight>
      <pre>
        <code class="javascript">const foo = 'bar'</code>
      </pre>
    </div>
  </div>
</template>

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

to display the code.

We just use the v-highlight directive to add the syntax highlighting.

Conclusion

vue-hljs lets us add syntax highlighting to our code display.

vue-seamless-scroll makes scrolling automatic.

Vue Toast Notification displays notifications in various formats.

Vue Carousel 3d lets us add a 3D slider.

animated-number-vue animates numbers.

Categories
Top Vue Packages

Top Vue Packages for Adding Inputs, QR Codes, and Handle Events

Vue.js is an easy to use web app framework that we can use to develop interactive front end apps. In this article, we’ll look at how the best packages for adding inputs, focusing elements, adding QR codes, and emit and listen to global events.

Vue Cleave Component

Vue Cleave Component is an input component that lets us enter things like credit card numbers.

To use it, first, we run:

npm i vue-cleave-component

to install it.

Then we can use it by writing:

<template>
  <div>
    <cleave v-model="cardNumber" :options="options" class="form-control" name="card"></cleave>
  </div>
</template>

<script>
import Cleave from "vue-cleave-component";

export default {
  data() {
    return {
      cardNumber: null,
      options: {
        creditCard: true,
        delimiter: "-"
      }
    };
  },
  components: {
    Cleave
  }
};
</script>

We bind the inputted value to cardNumber with v-model .

options is the options we can set for the input.

creditCard set to true means that we let users enter credit card numbers.

delimiter is the delimiter between each chunk.

So we’ll see the dash between each set of 4 digits.

vue-bus

vue-bus is a Vue plugin to add an app-wide event bus to our app.

To use it, we install it by running:

npm i vue-bus

Then we can use it bu writing”

<template>
  <div></div>
</template>

<script>
export default {
  created() {
    this.$bus.on("add-todo", this.addTodo);
    this.$bus.once("once", () => console.log("fire once"));
    this.$bus.emit("once");
    this.$bus.emit("add-todo");
  },
  beforeDestroy() {
    this.$bus.off("add-todo", this.addTodo);
  },
  methods: {
    addTodo() {
      console.log("add todo");
    }
  }
};
</script>

We use the this.$bus.on method to listen to events.

this.$bus.once creates an event that’s fired only once.

emit emits an event.

this.$bus.off clears event listeners.

Vue MQ (MediaQuery)

Vue MQ (MediaQuery) lets us define breakpijts and build responsive designs easily.

To use it, we run:

npm i vue-mq

to install it.

Then we use it by writing:

main.js

import Vue from "vue";
import App from "./App.vue";
import VueMq from "vue-mq";

Vue.use(VueMq, {
  breakpoints: {
    sm: 450,
    md: 1250,
    lg: Infinity
  },
  defaultBreakpoint: "sm"
});
Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

App.vue

<template>
  <div>
    <p>{{displayText}}</p>
  </div>
</template>

<script>
export default {
  computed: {
    displayText() {
      return this.$mq === "sm" ? "small" : "large";
    }
  }
};
</script>

We use the this.$mq property to check the size of the screen.

Then we can display text accordingly.

vue-focus

vue-focus is a reusable directive for letting us focus or blur an element.

To use it, we install it by running:

npm i vue-focus

Then we can use it by writing:

<template>
  <div>
    <input type="text" v-focus="focused" @focus="focused = true" @blur="focused = false">
    <button @click="focused = true">focus</button>
  </div>
</template>

<script>
import { mixin as focusMixin } from "vue-focus";

export default {
  mixins: [focusMixin],
  data() {
    return {
      focused: false
    };
  }
};
</script>

We have an input element, which we control the focus of with the v-focus directive.

The directive comes from registering the mixin.

The focus event is emitted when it’s focused, which we set focused to true when it’s emitted.

v-viewer

v-viewer is an image viewer component that can be used to display an image gallery.

To use it, we run:

npm i v-viewer

to install the package.

Then we write:

main.js

import Vue from "vue";
import App from "./App.vue";
import "viewerjs/dist/viewer.css";
import Viewer from "v-viewer";
Vue.use(Viewer);

Vue.config.productionTip = false;

new Vue({
  render: h => h(App)
}).$mount("#app");

We register the plugin and import the CSS.

App.vue

<template>
  <div>
    <viewer :images="images">
      <img v-for="src in images" :src="src" :key="src">
    </viewer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        "https://placekitten.com/200/200",
        "https://placekitten.com/201/201"
      ]
    };
  }
};
</script>

To add the image gallery with 2 images in it.

All we did is to use the viewer component and nest the images in it.

vue-qr

vue-qr is a plugin that lets us add a QR code to our app.

To use it, we install it by running:

npm i vue-qr

Then we can use it by writing:

<template>
  <div>
    <vue-qr text="Hello!" :callback="test" qid="test"></vue-qr>
  </div>
</template>

<script>
import VueQr from "vue-qr";

export default {
  components: { VueQr },
  methods: {
    test(dataUrl, id) {
      console.log(dataUrl, id);
    }
  }
};
</script>

We use the vue-qr component with the text we want to show when scanned into the text prop. The callback, which we pass in as the value of the callback prop is called when it’s loaded.

Conclusion

vue-qr is a QR code component. v-viewer lets us add an image viewer to our Vue app. vue-focus lets us control the focus of elements. Vue Cleave Component is an input component that lets users enter credit card numbers. vue-bus is an event bus library for Vue apps.

Categories
Vue Tips

Vue Tips — DOM Elements, Vue Router, and Dynamic Components

Vue.js is a popular framework for creating front end web apps.

In this article, we’ll look at some tips for writing better Vue.js apps.

Open a Link in a New Tab with Vue Router

We can open a link in a new tab with Vue Router.

For instance, we can write:

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

We call the this.$router.resolve method to get the URL to open.

Then we call window.open to open the given URL.

The first argument is the URL.

'_blank' indicates that we open the URL in a new tab.

Run Vue.js Dev Server with HTTPS

We can change the Vue dev server’s config to serve the project over HTTPS rather than HTTP.

To do that, we read the private key and certificate.

And we set the URL to serve the project on.

For instance, we can write:

const fs = require('fs')

module.exports = {
  devServer: {
    https: {
      key: fs.readFileSync('./certs/key.pem'),
      cert: fs.readFileSync('./certs/cert.pem'),
    },
    public: 'https://localhost:8888/'
  }
}

We read in the files and set them as the properties of the https property.

To make a certificate, we can use the mkcert program to do it.

We can install it on Windows, Mac OS, or Linux by following the instructions on https://github.com/FiloSottile/mkcert.

Then we can create a new key and certificate by running:

mkcert -install

and:

mkcert example.com "*.example.com" example.test localhost 127.0.0.1 ::1

Then we created a certificate valid for localhost.

We just copy the files to the cert folder and rename them to match what we have in the config.

Then we can run npm run dev and serve the project.

Rerun Vue Component mounted() Method

To rerun the code that we wrote in the mounted method, we can move the code into its own method.

Then we can call that in mounted or anywhere else.

For instance, we can write:

new Vue({
  methods: {
    init(){
      //...
    }
  },
  mounted(){
    this.init();
  }
})

Then we can write:

<button @click="init">reset</button>

We set the init method as the click handler and also run it in the mounted hook.

Now we can reuse it as we wish.

Trigger Events Using Vue

We can set a ref on an element and then trigger the event on the element.

For instance, we can write:

<template>
  <button type="button" @click="onClick" ref="aButton">
    click me
  </button>
</template>

<script>
export default {
  methods: {
    onClick($event) {
      const elem = this.$refs.aButton.$el;
      elem.click();
    }
  }
}
</script>

We have a button that we assigned a ref to.

Then we get the button’s ref’s element in the onClick method.

It returns the DOM node for the button.

Then we call click to trigger a click on the button DOM node.

Share a Method Between Components in Vue

To share a method between components in Vue, we can create a module that exports the method.

For instance, we can write:

src/shared.js

export default {
  bar() {
    alert("bar")
  }
}

Then we can use it in our component by writing:

src/App.js

<template>...</template>

<script>
import shared from './shared'

export default {
  created() {
    shared.bar();
  }
}
</script>

We imported the shared module and called the bar method inside it.

Alternatively, we can create a mixin, which is a piece of code that can be merged into our component.

For instance, we can write:

const cartMixin = {
  methods: {
    addToCart(product) {
      this.cart.push(product);
    }
  }
};

Then we can use it in our component by writing:

const Store = Vue.extend({
  template: '#app',
  mixins: [cartMixin],
  data(){
    return {
      cart: []
    }
  }
})

We call Vue.extend to create a subclass of the Vue constructor.

We can then make a new instance of Store and render that in our app.

Pass a Component as Props and Use it in a Child Component in Vue

We can pass in the component’s tag name into the is prop of the component component.

For instance, we can write:

<template>
  <div>
    <component :is="childComponent">foo bar</component>
  </div>
</template>

childComponent is the string with the tag name of the component.

The stuff between the tags is the content that’s filled into the default slot.

Conclusion

We can create a shared module or a mixin to create shared code in Vue.

Also, we can use this.$router.resolve to get the path that we can use elsewhere.

Vue projects can be served with HTTPS.

We can get the HTML element’s DOM object with refs.