Hero Image of content
Vue Tutorial

This article records the common methods in Vue, using Vue 3 as an example.

Vue Tutorial

Project Setup

npm init vue@latest
# Activate ESlint and Prettier

cd project

# Install requirements
npm install
npm install yarn@latest

yarn dev

Common Methods

1. ref: Defining Reactive Data

const blog = ref({
  title: "hawcat's Blog",
  content: 'Describe',
  link: '/hawcat',
})

2. Dynamic Binding

In HTML tags, use :* for dynamic binding.

{{}} is used to display Vue data.

<a :href="blog.link"> {{ blog.title }} </a>

3. v-if Conditional Rendering

<footer v-if="blog.content.length > 100">
<button>Read More</button>
</footer>

4. v-for Loop

:key is used for optimizing the iteration speed.

<div v-for="blog in blogs" :key="blog.id">
    <h1>
      <a :href="blog.link"> {{ blog.title }} </a>
    </h1>
    <article>
      <div>
        {{ blog.content.slice(0, 100) }}
      </div>
      <footer v-if="blog.content.length > 100">
        <button>Read More</button>
      </footer>
    </article>
  </div>

5. computed

computed is a reactive API in Vue used to create computed properties based on other reactive data. It recalculates the result and caches it until the dependent data changes.

const total_blogs = computed(() => blogs.value.length)

6. Handling Events

In HTML tags, add @ to track events.

7. Variable Definition

The difference between const xx = ref() and const xx = x is that ref creates a reactive object, and Vue will track its .value. While const defines a normal constant that cannot be modified.

const initialBlogForm = {
  title: '',
  content: '',
  link: '',
}

const blogForm = ref({ ...initialBlogForm })

ref(...) makes an object reactive, so the data bound in the template will update when the value changes.

{ ...initialBlogForm } ensures that a “copy” of initialBlogForm is used rather than binding directly to it.

8. v-model

Two-way data binding for properties.

<input type="text" id="blogTitle" v-model="blogForm.title"></input>

9. Event Modifiers

Add a modifier to @event to modify its behavior.

For example: <form @submit.prevent=""> The prevent modifier prevents the default form submission and page refresh.

10. Vue Component Definition and Reusability

Decouple the main page, and use defineProps to define the parameters that the component will accept, similar to function arguments in C++ or Python.

<template>
  <div>
    <h1>
      <a :href="link"> {{ title }} </a>
    </h1>
    <article>
      <div>
        {{ content.slice(0, 100) }}
      </div>
      <footer v-if="content.length > 100">
        <button>Read More</button>
      </footer>
    </article>
  </div>
</template>

<script setup>
defineProps(['title', 'content', 'link'])
</script>

In the parent component:

<BlogPost v-for="blog in blogs" :key="blog.id" :title="blog.title" :content="blog.content" :link="blog.link"></BlogPost>

Or:

<BlogPost v-for="blog in blogs" :key="blog.id" v-bind="blog"></BlogPost>

11. Vue Component Lifecycle

Use the onMounted() method to load data when the component is mounted.

onMounted(() => {
  setTimeout(() => {
    viewCount.value = 10000
  }, 1000)
})

12. Signal Callbacks

Similar to the signal system in Qt.

Here, a custom signal titleClick is defined, which is emitted when the title is clicked, passing the title parameter.

<a :href="link" @click.prevent="$emit('titleClick', title)"> {{ title }} </a>

<!-- In setup -->
defineEmits(['titleClick'])

In the parent component:

<BlogPost @titleClick="handleTitleClick" v-for="blog in blogs" :key="blog.id" v-bind="blog">
    <button>Share to WeChat</button>
</BlogPost>
function handleTitleClick(title) {
  console.log('Clicked on title', title)
}