You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: English/Vue/Essentials/Components-Basics.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,3 +41,54 @@ The template is inlined as a JavaScript string here, which Vue will compile on t
41
41
on the fly 立即,立刻
42
42
43
43
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:
0 commit comments