|
2 | 2 |
|
3 | 3 | > This page assumes you've already read the [Components Basics](/guide/essentials/component-basics). Read that first if you are new to components.
|
4 | 4 |
|
5 |
| -## Component Names |
| 5 | +A Vue component needs to be "registered" so that Vue knows where to locate its implementation when it is encountered in a template. There are two ways to register components: global and local. |
6 | 6 |
|
7 |
| -When registering a component, it will always be given a name. For example, in the global registration we've seen so far: |
8 |
| - |
9 |
| -```js |
10 |
| -const app = Vue.createApp({...}) |
11 |
| - |
12 |
| -app.component('my-component-name', { |
13 |
| - /* ... */ |
14 |
| -}) |
15 |
| -``` |
16 |
| - |
17 |
| -The component's name is the first argument of `app.component`. In the example above, the component's name is "my-component-name". |
18 |
| - |
19 |
| -The name you give a component may depend on where you intend to use it. When using a component directly in the DOM (as opposed to in a string template or Single File Component), we strongly recommend following the [W3C rules](https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name) for custom tag names: |
20 |
| - |
21 |
| -1. All lowercase |
22 |
| -2. Contains a hyphen (i.e., has multiple words connected with the hyphen symbol) |
23 |
| - |
24 |
| -By doing so, this will help you avoid conflicts with current and future HTML elements. |
25 |
| - |
26 |
| -You can see other recommendations for component names in the [Style Guide](/style-guide/#base-component-names-strongly-recommended). |
27 |
| - |
28 |
| -### Name Casing |
29 |
| - |
30 |
| -When defining components in a string template or a single-file component, you have two options when defining component names: |
| 7 | +## Global Registration |
31 | 8 |
|
32 |
| -#### With kebab-case |
| 9 | +We can make components available globally in the current [Vue application](/guide/essentials/application.html) using the `app.component()` method: |
33 | 10 |
|
34 | 11 | ```js
|
35 |
| -app.component('my-component-name', { |
36 |
| - /* ... */ |
37 |
| -}) |
38 |
| -``` |
39 |
| - |
40 |
| -When defining a component with kebab-case, you must also use kebab-case when referencing its custom element, such as in `<my-component-name>`. |
| 12 | +import { createApp } from 'vue' |
41 | 13 |
|
42 |
| -#### With PascalCase |
| 14 | +const app = createApp({}) |
43 | 15 |
|
44 |
| -```js |
45 |
| -app.component('MyComponentName', { |
46 |
| - /* ... */ |
47 |
| -}) |
| 16 | +app.component( |
| 17 | + // the registered name |
| 18 | + 'MyComponent', |
| 19 | + // the implementation |
| 20 | + { |
| 21 | + /* ... */ |
| 22 | + } |
| 23 | +) |
48 | 24 | ```
|
49 | 25 |
|
50 |
| -When defining a component with PascalCase, you can use either case when referencing its custom element. That means both `<my-component-name>` and `<MyComponentName>` are acceptable. Note, however, that only kebab-case names are valid directly in the DOM (i.e. non-string templates). |
51 |
| - |
52 |
| -## Global Registration |
53 |
| - |
54 |
| -So far, we've only created components using `app.component`: |
| 26 | +If using SFCs, you will be registering the imported `.vue` files: |
55 | 27 |
|
56 | 28 | ```js
|
57 |
| -Vue.createApp({...}).component('my-component-name', { |
58 |
| - // ... options ... |
59 |
| -}) |
| 29 | +import MyComponent from './App.vue` |
| 30 | +
|
| 31 | +app.component('MyComponent', MyComponent) |
60 | 32 | ```
|
61 | 33 |
|
62 |
| -These components are **globally registered** for the application. That means they can be used in the template of any component instance within this application: |
| 34 | +The `app.component()` method can be chained: |
63 | 35 |
|
64 | 36 | ```js
|
65 |
| -const app = Vue.createApp({}) |
66 |
| - |
67 |
| -app.component('component-a', { |
68 |
| - /* ... */ |
69 |
| -}) |
70 |
| -app.component('component-b', { |
71 |
| - /* ... */ |
72 |
| -}) |
73 |
| -app.component('component-c', { |
74 |
| - /* ... */ |
75 |
| -}) |
76 |
| - |
77 |
| -app.mount('#app') |
| 37 | +app |
| 38 | + .component('ComponentA', ComponentA) |
| 39 | + .component('ComponentB', ComponentB) |
| 40 | + .component('ComponentC', ComponentC) |
78 | 41 | ```
|
79 | 42 |
|
| 43 | +Globally registered components can be used in the template of any component instance within this application: |
| 44 | +
|
80 | 45 | ```vue-html
|
81 |
| -<div id="app"> |
82 |
| - <component-a></component-a> |
83 |
| - <component-b></component-b> |
84 |
| - <component-c></component-c> |
85 |
| -</div> |
| 46 | +<!-- this will work in any component inside the app --> |
| 47 | +<ComponentA/> |
| 48 | +<ComponentB/> |
| 49 | +<ComponentC/> |
86 | 50 | ```
|
87 | 51 |
|
88 | 52 | This even applies to all subcomponents, meaning all three of these components will also be available _inside each other_.
|
89 | 53 |
|
90 | 54 | ## Local Registration
|
91 | 55 |
|
92 |
| -Global registration often isn't ideal. For example, if you're using a build system like Webpack, globally registering all components means that even if you stop using a component, it could still be included in your final build. This unnecessarily increases the amount of JavaScript your users have to download. |
| 56 | +While convenient, global registration has a few drawbacks: |
93 | 57 |
|
94 |
| -In these cases, you can define your components as plain JavaScript objects: |
| 58 | +1. Global registration prevents build systems from removing unused components (a.k.a "tree-shaking"). If you globally register a component but ends up not using it anywhere in your app, it will still be included in the final bundle. |
95 | 59 |
|
96 |
| -```js |
97 |
| -const ComponentA = { |
98 |
| - /* ... */ |
99 |
| -} |
100 |
| -const ComponentB = { |
101 |
| - /* ... */ |
102 |
| -} |
103 |
| -const ComponentC = { |
104 |
| - /* ... */ |
105 |
| -} |
106 |
| -``` |
| 60 | +2. Global registration makes dependency relationships less explicit in large applications. It makes it difficult to locate a child component's implementation from a parent component using it. This can affect long term maintainability similar to using too many global variables. |
107 | 61 |
|
108 |
| -Then define the components you'd like to use in a `components` option: |
| 62 | +Local registration scopes the availability of the registered components to the current component only. It makes the dependency relationship more explicit, and is more tree-shaking-friendly. |
109 | 63 |
|
110 |
| -```js |
111 |
| -const app = Vue.createApp({ |
112 |
| - components: { |
113 |
| - 'component-a': ComponentA, |
114 |
| - 'component-b': ComponentB |
115 |
| - } |
116 |
| -}) |
117 |
| -``` |
| 64 | +<div class="composition-api"> |
118 | 65 |
|
119 |
| -For each property in the `components` object, the key will be the name of the custom element, while the value will contain the options object for the component. |
| 66 | +When using SFC with `<script setup>`, imported components are automatically local-registered: |
120 | 67 |
|
121 |
| -Note that **locally registered components are _not_ also available in subcomponents**. For example, if you wanted `ComponentA` to be available in `ComponentB`, you'd have to use: |
122 |
| - |
123 |
| -```js |
124 |
| -const ComponentA = { |
125 |
| - /* ... */ |
126 |
| -} |
| 68 | +```vue |
| 69 | +<script setup> |
| 70 | +import ComponentA from './ComponentA.vue' |
| 71 | +</script> |
127 | 72 |
|
128 |
| -const ComponentB = { |
129 |
| - components: { |
130 |
| - 'component-a': ComponentA |
131 |
| - } |
132 |
| - // ... |
133 |
| -} |
| 73 | +<template> |
| 74 | + <ComponentA /> |
| 75 | +</template> |
134 | 76 | ```
|
135 | 77 |
|
136 |
| -Or if you're using ES2015 modules, such as through Babel and Webpack, that might look more like: |
| 78 | +If not using SFC, you will need to use the `components` option: |
137 | 79 |
|
138 | 80 | ```js
|
139 |
| -import ComponentA from './ComponentA.vue' |
| 81 | +import ComponentA from './ComponentA.js' |
140 | 82 |
|
141 | 83 | export default {
|
142 | 84 | components: {
|
143 | 85 | ComponentA
|
| 86 | + }, |
| 87 | + setup() { |
| 88 | + // ... |
144 | 89 | }
|
145 |
| - // ... |
146 | 90 | }
|
147 | 91 | ```
|
148 | 92 |
|
149 |
| -Note that in ES2015+, placing a variable name like `ComponentA` inside an object is shorthand for `ComponentA: ComponentA`, meaning the name of the variable is both: |
| 93 | +</div> |
| 94 | +<div class="options-api"> |
150 | 95 |
|
151 |
| -- the custom element name to use in the template, and |
152 |
| -- the name of the variable containing the component options |
| 96 | +Local registration are done using the `components` option: |
153 | 97 |
|
154 |
| -## Module Systems |
| 98 | +```vue |
| 99 | +<script> |
| 100 | +import ComponentA from './ComponentA.vue' |
155 | 101 |
|
156 |
| -If you're not using a module system with `import`/`require`, you can probably skip this section for now. If you are, we have some special instructions and tips just for you. |
| 102 | +export default { |
| 103 | + components: { |
| 104 | + ComponentA |
| 105 | + } |
| 106 | +} |
| 107 | +</script> |
157 | 108 |
|
158 |
| -### Local Registration in a Module System |
| 109 | +<template> |
| 110 | + <ComponentA /> |
| 111 | +</template> |
| 112 | +``` |
159 | 113 |
|
160 |
| -If you're still here, then it's likely you're using a module system, such as with Babel and Webpack. In these cases, we recommend creating a `components` directory, with each component in its own file. |
| 114 | +</div> |
161 | 115 |
|
162 |
| -Then you'll need to import each component you'd like to use, before you locally register it. For example, in a hypothetical `ComponentB.js` or `ComponentB.vue` file: |
| 116 | +For each property in the `components` object, the key will be the registered name of the component, while the value will contain the implementation of the component. The above example is using the ES2015 property shorthand and is equivalent to: |
163 | 117 |
|
164 | 118 | ```js
|
165 |
| -import ComponentA from './ComponentA' |
166 |
| -import ComponentC from './ComponentC' |
167 |
| - |
168 | 119 | export default {
|
169 | 120 | components: {
|
170 |
| - ComponentA, |
171 |
| - ComponentC |
| 121 | + ComponentA: ComponentA |
172 | 122 | }
|
173 | 123 | // ...
|
174 | 124 | }
|
175 | 125 | ```
|
176 | 126 |
|
177 |
| -Now both `ComponentA` and `ComponentC` can be used inside `ComponentB`'s template. |
| 127 | +Note that **locally registered components are _not_ also available in descendent components**. In this case, `ComponentA` will be made available to the current component only, not any of its child or descendent components. |
| 128 | + |
| 129 | +## Component Name Casing |
| 130 | + |
| 131 | +Throughout the guide, we are using PascalCase for component registration names. This is because: |
| 132 | + |
| 133 | +1. PascalCase names are valid JavaScript identifiers. This makes it easier to import and register components in JavaScript. |
| 134 | + |
| 135 | +2. `<PascalCase />` makes it more obvious that this is a Vue component instead of a native HTML element in templates. |
| 136 | + |
| 137 | +This is the recommended style when working with SFC or string templates. However, as discussed in [DOM Template Parsing Caveats](/guide/essentials/component-basics.html#dom-template-parsing-caveats), PascalCase tags are not usable in DOM templates. |
| 138 | + |
| 139 | +Luckily, Vue supports resolving kebab-case tags to components registered using PascalCase. This means a component registered as `MyComponent` can be referenced in the template via both `<MyComponent>` and `<my-component>`. This allows us to use the same JavaScript component registration code regardless of template source. |
0 commit comments