Vue.js: A lightweight alternative to React and Angular


Explore a progressive JavaScript framework that’s easy to learn and powerful to use.

Background

If you’ve been involved in front-end web development, you’ve likely encountered the challenges of managing complex user interfaces and state. These issues have long been pain points for developers working with vanilla JavaScript or jQuery.

Enter Vue.js, a framework designed to address these very problems. Vue aims to provide a declarative and component-based programming model that helps you efficiently develop user interfaces, whether simple or complex. It’s a way to build dynamic web applications with the simplicity typically associated with smaller libraries, but with the power of full-fledged frameworks.

The competition

If you’re familiar with front-end frameworks, you might ask: “Why not just use React or Angular?”. It’s a valid question. React and Angular are mature and widely used in the industry. However, Vue offers some unique benefits that make it worth considering:

  1. Gentle Learning Curve: Vue’s core library is focused on the view layer only, making it easy to pick up and integrate with other libraries or existing projects.
  2. Flexible Architecture: Vue allows you to write your entire application with Vue, or you can start small and progressively integrate Vue features as your application grows.
  3. Lightweight: Vue has a relatively small file size, which means faster loading times and better performance, especially on mobile devices.
  4. Detailed Documentation: Vue provides extensive, clear documentation, making it easier for developers to get started and find solutions to problems.
  5. Growing Ecosystem: Vue has a rapidly expanding ecosystem with official libraries for routing (Vue Router) and state management (Vuex), as well as a large number of community-built plugins and components.

Getting Started with Vue

Now that we’ve covered the background and advantages of Vue, let’s dive into creating a simple project. We’ll create a basic Vue application and then expand on it to showcase some of Vue’s features.

Setting Up

First, make sure you have Node.js installed. Then, you can create a new Vue project using the Vue CLI:

npm init vue@latest
cd <your-project-name>
npm install
npm run dev

This command creates a new directory with a basic Vue project structure using the latest version of Vue (Vue 3).

Writing Our First Vue Component

Open the src/App.vue file. You’ll find a basic Vue component structure. Let’s modify it to create a simple counter:

<script setup>
import { ref } from 'vue'

const count = ref(0)

function increment() {
  count.value++
}
</script>

<template>
  <h1>{{ count }}</h1>
  <button @click="increment">Increment</button>
</template>

This is a basic Vue component using the Composition API. It displays a count and a button to increment it.

Advanced Features

While a simple counter is straightforward, Vue really shines when dealing with more complex scenarios. Let’s look at some more advanced examples that showcase Vue’s unique features.

Reactive State Management

Vue’s reactivity system is powerful and intuitive. Here’s an example using multiple reactive values:

<script setup>
import { ref, computed } from 'vue'

const count = ref(0)
const doubleCount = computed(() => count.value * 2)

function increment() {
  count.value++
}
</script>

<template>
  <h1>Count: {{ count }}</h1>
  <h2>Double Count: {{ doubleCount }}</h2>
  <button @click="increment">Increment</button>
</template>

This example demonstrates how to use ref for reactive state and computed for derived state.

Component Composition

Vue’s Composition API allows for flexible component logic organization:

<script setup>
import { ref } from 'vue'

function useCounter() {
  const count = ref(0)
  function increment() {
    count.value++
  }
  return { count, increment }
}

const { count, increment } = useCounter()
</script>

<template>
  <h1>{{ count }}</h1>
  <button @click="increment">Increment</button>
</template>

This showcases how to use composables to encapsulate and reuse component logic.

Lifecycle Hooks

Vue provides lifecycle hooks that let you add custom logic at specific stages:

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'

const count = ref(0)
let timer

onMounted(() => {
  timer = setInterval(() => {
    count.value++
  }, 1000)
})

onUnmounted(() => {
  clearInterval(timer)
})
</script>

<template>
  <h1>{{ count }}</h1>
</template>

This example shows how to use onMounted and onUnmounted hooks to set up and clean up side effects.

Props and Events

Vue makes it easy to communicate between parent and child components:

<!-- Parent.vue -->
<script setup>
import { ref } from 'vue'
import Child from './Child.vue'

const parentCount = ref(0)

function handleChildIncrement(amount) {
  parentCount.value += amount
}
</script>

<template>
  <h1>Parent Count: {{ parentCount }}</h1>
  <Child :count="parentCount" @increment="handleChildIncrement" />
</template>

<!-- Child.vue -->
<script setup>
defineProps(['count'])
const emit = defineEmits(['increment'])

function incrementParent() {
  emit('increment', 1)
}
</script>

<template>
  <h2>Child Count: {{ count }}</h2>
  <button @click="incrementParent">Increment Parent</button>
</template>

This demonstrates how to pass props down to child components and emit events up to parent components.

Conclusion

We’ve only scratched the surface of what Vue can do. Its progressive nature makes it easy to adopt incrementally, while its robust feature set allows it to scale to handle complex applications. By leveraging features like the Composition API, computed properties, and component composition, you can write more maintainable and efficient code.

While Vue offers many advantages, the choice of front-end framework often depends on your specific needs and the ecosystem you’re working in. Always evaluate your options based on your project requirements.

For more advanced usage and best practices, I highly recommend checking out the official Vue documentation.

関連記事

カテゴリー:

ブログ

情シス求人

  1. チームメンバーで作字やってみた#1

ページ上部へ戻る