Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Splitting Components

Splitting Components

Component-based architecture is Vue's core design philosophy.

In this chapter you will learn how to split a page into reusable small components and implement communication between components via props and emit.


Why Split Components?

As features grow, a single file becomes longer and longer; hundreds of lines of code crammed into one .vue file are hard to maintain.

The purpose of splitting components: each component does only one thing, and they combine to accomplish complex functionality.

What Granularity Is Appropriate?

A good rule of thumb: when a block of code has its own data, its own behavior, and can be reused elsewhere, it should become an independent component.

Page Area Should It Be a Component? Reason
Navigation bar Is Needed on every page; independent functionality
Article card Is Every item in the list has the same structure
Category filter button group Is Has its own state and interactions
Simple wrapper container No Only for layout, no independent behavior

Parent to Child: defineProps

The parent component passes data to the child component through props.

You can think of it as "function parameters" — when the parent calls the child component, it passes data as arguments.

Example

When the parent uses BlogCard, it passes data by binding attributes with a colon:

Example

props isOne-Way Data Flow: data can only flow from parent to child, and child components cannot modify the value of props. If a child component needs to make changes, it should notify the parent to do so via emit.


Child to Parent: defineEmits

When the child component needs to notify the parent that something happened (e.g., "I was clicked"), it sends an event via emit.

You can think of it as a "callback function" — the child triggers the event, and the parent listens and responds.

Example

The parent component listens to the child's events via @事件名="处理函数" :

Example

The Complete Data Flow

The parent passes data down through props, and the child sends events up through emit.

Data is always "owned" and modified by the parent component; the child component is only responsible for displaying and notifying.


Hands-On: Extract Three Components

The blog project needs three components, with the following directory structure:



src/

├── components/

│   ├── NavBar.vue          # 顶部导航栏

│   ├── BlogCard.vue        # 单篇文章卡片

│   └── CategoryFilter.vue  # 分类筛选按钮组

├── views/

│   └── HomeView.vue        # 首页(父组件)

└── App.vue

NavBar.vue — Navigation bar

Example

HomeView.vue — Integrates all components

Example


Chapter Summary

In this chapter you mastered three core concepts of componentization: why to split components, parent-to-child via props (defineProps), and child-to-parent via emit (defineEmits).

The blog project's three components NavBar, BlogCard, and CategoryFilter are now in place, and the code structure is much clearer than the messy single block from Chapter 1.