-
## Base Example
Here's an example of a Vue component:
@@ -17,7 +15,10 @@ app.component('button-counter', {
count: 0
}
},
- template: ``
+ template: `
+ `
})
```
@@ -33,7 +34,12 @@ Components are reusable Vue instances with a name: in this case, `
+
+
Since components are reusable Vue instances, they accept the same options as a root instance, such as `data`, `computed`, `watch`, `methods`, and lifecycle hooks. The only exceptions are a few root-specific options like `el`.
@@ -49,7 +55,12 @@ Components can be reused as many times as you want:
```
-
+
+
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.
@@ -73,7 +84,7 @@ app.component('my-component-name', {
Globally registered components can be used in the template of `app` instance created afterwards - and even inside all subcomponents of that Vue instance's component tree.
-That's all you need to know about registration for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Component Registration](TODO:components-registration.html).
+That's all you need to know about registration for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Component Registration](component-registration.md).
## Passing Data to Child Components with Props
@@ -82,12 +93,14 @@ Earlier, we mentioned creating a component for blog posts. The problem is, that
Props are custom attributes you can register on a component. When a value is passed to a prop attribute, it becomes a property on that component instance. To pass a title to our blog post component, we can include it in the list of props this component accepts, using a `props` option:
```js
-const app = Vue.createApp()
+const app = Vue.createApp({})
app.component('blog-post', {
props: ['title'],
template: `
{{ title }}
`
})
+
+app.mount('#blog-post-demo')
```
A component can have as many props as you'd like and by default, any value can be passed to any prop. In the template above, you'll see that we can access this value on the component instance, just like with `data`.
@@ -95,12 +108,19 @@ A component can have as many props as you'd like and by default, any value can b
Once a prop is registered, you can pass data to it as a custom attribute, like this:
```html
-
-
-
+
+
In a typical app, however, you'll likely have an array of posts in `data`:
@@ -141,7 +161,7 @@ Then want to render a component for each one:
Above, you'll see that we can use `v-bind` to dynamically pass props. This is especially useful when you don't know the exact content you're going to render ahead of time.
-That's all you need to know about props for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Props](TODO:components-props.html).
+That's all you need to know about props for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Props](component-props.html).
## Listening to Child Components Events
@@ -166,7 +186,7 @@ Which can be used in the template to control the font size of all blog posts:
```html
+
### Emitting a Value With an Event
@@ -305,7 +330,7 @@ Now `v-model` should work perfectly with this component:
```
-That's all you need to know about custom component events for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Custom Events](TODO:components-custom-events.html).
+That's all you need to know about custom component events for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Custom Events](component-custom-events.md).
## Content Distribution with Slots
@@ -319,7 +344,12 @@ Just like with HTML elements, it's often useful to be able to pass content to a
Which might render something like:
-
+
+
Fortunately, this task is made very simple by Vue's custom `` element:
@@ -336,13 +366,18 @@ app.component('alert-box', {
As you'll see above, we just add the slot where we want it to go -- and that's it. We're done!
-That's all you need to know about slots for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Slots](TODO:components-slots.html).
+That's all you need to know about slots for now, but once you've finished reading this page and feel comfortable with its content, we recommend coming back later to read the full guide on [Slots](component-slots.md).
## Dynamic Components
Sometimes, it's useful to dynamically switch between components, like in a tabbed interface:
-
+
+
The above is made possible by Vue's `` element with the `is` special attribute:
@@ -356,7 +391,7 @@ In the example above, `currentTabComponent` can contain either:
- the name of a registered component, or
- a component's options object
-See [this sandbox](https://codesandbox.io/s/dynamic-components-dvtl8) to experiment with the full code, or [this version](https://codesandbox.io/s/dynamic-components-with-options-xfnrp) for an example binding to a component's options object, instead of its registered name.
+See [this sandbox](https://codepen.io/team/Vue/pen/oNXaoKy) to experiment with the full code, or [this version](https://codepen.io/team/Vue/pen/oNXapXM) for an example binding to a component's options object, instead of its registered name.
Keep in mind that this attribute can be used with regular HTML elements, however they will be treated as components, which means all attributes **will be bound as DOM attributes**. For some properties such as `value` to work as you would expect, you will need to bind them using the [`.prop` modifier](TODO:../api/#v-bind).
@@ -405,7 +440,7 @@ It should be noted that **these limitations does _not_ apply if you are using st
- String templates (e.g. `template: '...'`)
- [Single-file (`.vue`) components](TODO:single-file-components.html)
-- [`
## Method Event Handlers
@@ -36,7 +41,7 @@ The logic for many event handlers will be more complex though, so keeping your J
For example:
```html
-
+
Sometimes we also need to access the original DOM event in an inline statement handler. You can pass it into a method using the special `$event` variable:
@@ -156,7 +171,7 @@ Order matters when using modifiers because the relevant code is generated in the
```
-Unlike the other modifiers, which are exclusive to native DOM events, the `.once` modifier can also be used on [component events](TODO:components-custom-events.html). If you haven't read about components yet, don't worry about this for now.
+Unlike the other modifiers, which are exclusive to native DOM events, the `.once` modifier can also be used on [component events](component-custom-events.html). If you haven't read about components yet, don't worry about this for now.
Vue also offers the `.passive` modifier, corresponding to [`addEventListener`'s `passive` option](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters).
diff --git a/src/guide/forms.md b/src/guide/forms.md
index 0bbbef6191..76e2e9a7da 100644
--- a/src/guide/forms.md
+++ b/src/guide/forms.md
@@ -26,7 +26,12 @@ For languages that require an [IME](https://en.wikipedia.org/wiki/Input_method)