Skip to content

Commit d8f7bf3

Browse files
zhenyuWangCopilot
andcommitted
docs: update study log
Co-authored-by: Copilot <copilot@github.com>
1 parent 538ab4d commit d8f7bf3

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

English/Vue/Essentials/Components-Basics.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,54 @@ The template is inlined as a JavaScript string here, which Vue will compile on t
4141
on the fly 立即,立刻
4242

4343
The example above defines a single component and exports it as the default export of a `.js` file, but you can use named exports to export multiple components from the same file.
44+
45+
## Using a Component​
46+
#### TIP
47+
48+
We will be using SFC syntax for the rest of this guide - the concepts around components are the same regardless of whether you are using a build step or not. The Examples section shows component usage in both scenarios.\
49+
scenario [/sɪˈnɑːriəʊ/] 场景
50+
51+
To use a child component, we need to import it in the parent component. Assuming we placed our counter component inside a file called `ButtonCounter.vue`, the component will be exposed as the file's default export:\
52+
assume [/əˈsjuːm/] 假设
53+
54+
```vue
55+
<script>
56+
import ButtonCounter from './ButtonCounter.vue'
57+
58+
export default {
59+
components: {
60+
ButtonCounter
61+
}
62+
}
63+
</script>
64+
65+
<template>
66+
<h1>Here is a child component!</h1>
67+
<ButtonCounter />
68+
</template>
69+
```
70+
To expose the imported component to our template, we need to register it with the `components` option. The component will then be available as a tag using the key it is registered under.
71+
72+
It's also possible to globally register a component, making it available to all components in a given app without having to import it. The pros and cons of global vs. local registration is discussed in the dedicated Component Registration section.
73+
74+
Components can be reused as many times as you want:
75+
76+
```template
77+
<h1>Here are many child components!</h1>
78+
<ButtonCounter />
79+
<ButtonCounter />
80+
<ButtonCounter />
81+
```
82+
83+
Notice that when clicking on the buttons, each one maintains its own, separate `count`. That's because each time you use a component, a new instance of it is created.
84+
85+
In SFCs, it's recommended to use `PascalCase` tag names for child components to differentiate from native HTML elements. Although native HTML tag names are case-insensitive, Vue SFC is a compiled format so we are able to use case-sensitive tag names in it. We are also able to use `/>` to close a tag.
86+
87+
If you are authoring your templates directly in a DOM (e.g. as the content of a native `<template>` element), the template will be subject to the browser's native HTML parsing behavior. In such cases, you will need to use `kebab-case` and explicit closing tags for components:
88+
89+
```template
90+
<!-- if this template is written in the DOM -->
91+
<button-counter></button-counter>
92+
<button-counter></button-counter>
93+
<button-counter></button-counter>
94+
```

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
# 日志
66

7+
## 2026.5.14
8+
[Vue doc Components Basics](./English/Vue/Essentials/Components-Basics.md)
9+
710
## 2026.5.13
811
[Vue doc Components Basics](./English/Vue/Essentials/Components-Basics.md)
912

0 commit comments

Comments
 (0)