筆記 - Vue.js Intro (3) Components
透過 Vue.js 官網 tutorial 學習的筆記。學習標的為 Vue 3, using composition API.
Components - parent and child
Nested components are created in real Vue applications.
A parent component can render another component in its template as a child component.
import => use in the template
1 | <script setup> |
Props - parent to child
A child component can accept input from the parent via props.
First, it needs to declare the props it accepts:
1 | <!-- ChildComp.vue --> |
Note defineProps()
is a compile-time macro and doesn’t need to be imported. Once declared, the msg
prop can be used in the child component’s template. It can also be accessed in JavaScript via the returned object of defineProps()
.
The parent can pass the prop to the child just like attributes. To pass a dynamic value, we can also use the v-bind
syntax:
1 | <!-- App.vue --> |
Emits - child to parent
In addition to receiving props, a child component can also emit events to the parent:
1 | <!-- ChildComp.vue --> |
The first argument to emit()
is the event name. Any additional arguments are passed on to the event listener.
The parent can listen to child-emitted events using v-on
- here the handler receives the extra argument from the child emit call and assigns it to local state:
1 | <!-- App.vue --> |
Slots: template fragments from parent to child
In addition to passing data via props, the parent component can also pass down template fragments to the child via slots:
1 | <!-- App.vue --> |
In child component, the content inside the