Blog.

My Journey Learning Vue.js

Cover Image for My Journey Learning Vue.js
Wisly Ong
Wisly Ong

Learning Vue.js has been an exciting journey. As a developer who has worked with various JavaScript frameworks, I was eager to dive into Vue.js and explore its simplicity and powerful features. Here’s a brief overview of my experience and some example code that I found particularly insightful.

Getting Started with Vue.js

Vue.js is a progressive JavaScript framework that is designed to be incrementally adoptable. It’s flexible and can be used for building both simple and complex applications. I started by setting up a basic Vue.js project using Vue CLI.

# Install Vue CLI globally
npm install -g @vue/cli

# Create a new Vue project
vue create my-vue-app

# Navigate to the project directory
cd my-vue-app

# Start the development server
npm run serve

This was a straightforward process, and the CLI provided a smooth setup with a minimal configuration to get started quickly.

Understanding the Basics: Components

One of the core concepts in Vue.js is components. Components in Vue.js are reusable instances with their own logic and UI. Here’s an example of a simple Vue component that displays a greeting message:

<template>
  <div>
    <h1>{{ message }}</h1>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello, Vue.js!'
    }
  }
}
</script>

<style scoped>
h1 {
  color: #42b983;
}
</style>

In this example:

  • ・ The template block defines the HTML structure.
  • ・ The script block contains the logic, such as the data and methods.
  • ・ The style block is scoped to this component, ensuring the styles don’t leak out.

Reactive Data Binding

Vue.js excels in reactive data binding. The framework automatically keeps the UI in sync with the data. Here’s a simple example of two-way data binding using v-model:

<template>
  <div>
    <input v-model="name" placeholder="Enter your name" />
    <p>Your name is: {{ name }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: ''
    }
  }
}
</script>

<style scoped>
input {
  padding: 5px;
  margin-right: 10px;
}
</style>

With v-model, the input field is bound to the name property. As the user types, the UI updates automatically to reflect the current value.

Event Handling

Handling events in Vue.js is straightforward. The v-on directive is used to listen to DOM events and execute methods:

<template>
  <div>
    <button @click="incrementCounter">Click me</button>
    <p>You've clicked the button {{ counter }} times.</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      counter: 0
    }
  },
  methods: {
    incrementCounter() {
      this.counter++;
    }
  }
}
</script>

<style scoped>
button {
  background-color: #42b983;
  color: white;
  border: none;
  padding: 10px;
  cursor: pointer;
}
</style>

In this example, the button click event is handled by the incrementCounter method, which updates the counter value. The UI automatically reflects the updated value, showcasing Vue's reactive nature.

Conclusion

Learning Vue.js has been a rewarding experience. The framework’s simplicity, combined with its powerful features, makes it a great choice for both beginners and experienced developers. As I continue to explore more advanced features like Vue Router and Vuex, I’m excited to build more complex and interactive applications with Vue.js.

If you're considering learning a new JavaScript framework, Vue.js is definitely worth your time. Happy coding!