diff --git a/src/.vuepress/components/alert-box.vue b/src/.vuepress/components/alert-box.vue deleted file mode 100644 index 791efa42fb..0000000000 --- a/src/.vuepress/components/alert-box.vue +++ /dev/null @@ -1,14 +0,0 @@ - - - diff --git a/src/.vuepress/components/blog-post1.vue b/src/.vuepress/components/blog-post1.vue deleted file mode 100644 index 3c7a63fd9a..0000000000 --- a/src/.vuepress/components/blog-post1.vue +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/src/.vuepress/components/blog-post2.vue b/src/.vuepress/components/blog-post2.vue deleted file mode 100644 index b3d6edcdca..0000000000 --- a/src/.vuepress/components/blog-post2.vue +++ /dev/null @@ -1,14 +0,0 @@ - - - diff --git a/src/.vuepress/components/button-counter.vue b/src/.vuepress/components/button-counter.vue deleted file mode 100644 index bc113632b2..0000000000 --- a/src/.vuepress/components/button-counter.vue +++ /dev/null @@ -1,13 +0,0 @@ - - - diff --git a/src/.vuepress/components/components-1.vue b/src/.vuepress/components/components-1.vue deleted file mode 100644 index 1059054ed2..0000000000 --- a/src/.vuepress/components/components-1.vue +++ /dev/null @@ -1,5 +0,0 @@ - diff --git a/src/.vuepress/components/components-2.vue b/src/.vuepress/components/components-2.vue deleted file mode 100644 index 4e2141b12b..0000000000 --- a/src/.vuepress/components/components-2.vue +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/src/.vuepress/components/components-3.vue b/src/.vuepress/components/components-3.vue deleted file mode 100644 index 977572ebb2..0000000000 --- a/src/.vuepress/components/components-3.vue +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/src/.vuepress/components/components-4.vue b/src/.vuepress/components/components-4.vue deleted file mode 100644 index 7a4845e163..0000000000 --- a/src/.vuepress/components/components-4.vue +++ /dev/null @@ -1,27 +0,0 @@ - - - diff --git a/src/.vuepress/components/components-5.vue b/src/.vuepress/components/components-5.vue deleted file mode 100644 index f734589848..0000000000 --- a/src/.vuepress/components/components-5.vue +++ /dev/null @@ -1,11 +0,0 @@ - - - diff --git a/src/.vuepress/components/components-6.vue b/src/.vuepress/components/components-6.vue deleted file mode 100644 index aba40af9c8..0000000000 --- a/src/.vuepress/components/components-6.vue +++ /dev/null @@ -1,58 +0,0 @@ - - - - - diff --git a/src/.vuepress/components/events-1.vue b/src/.vuepress/components/events-1.vue deleted file mode 100644 index 1db5aaf4a3..0000000000 --- a/src/.vuepress/components/events-1.vue +++ /dev/null @@ -1,16 +0,0 @@ - - - diff --git a/src/.vuepress/components/events-2.vue b/src/.vuepress/components/events-2.vue deleted file mode 100644 index 300812de2a..0000000000 --- a/src/.vuepress/components/events-2.vue +++ /dev/null @@ -1,23 +0,0 @@ - - - diff --git a/src/.vuepress/components/events-3.vue b/src/.vuepress/components/events-3.vue deleted file mode 100644 index 6784ffafa8..0000000000 --- a/src/.vuepress/components/events-3.vue +++ /dev/null @@ -1,16 +0,0 @@ - - - diff --git a/src/.vuepress/components/forms-1.vue b/src/.vuepress/components/forms-1.vue deleted file mode 100644 index 0df191b1f7..0000000000 --- a/src/.vuepress/components/forms-1.vue +++ /dev/null @@ -1,16 +0,0 @@ - - - diff --git a/src/.vuepress/components/forms-2.vue b/src/.vuepress/components/forms-2.vue deleted file mode 100644 index 46b694f95b..0000000000 --- a/src/.vuepress/components/forms-2.vue +++ /dev/null @@ -1,18 +0,0 @@ - - - diff --git a/src/.vuepress/components/forms-3.vue b/src/.vuepress/components/forms-3.vue deleted file mode 100644 index 68c69a0654..0000000000 --- a/src/.vuepress/components/forms-3.vue +++ /dev/null @@ -1,16 +0,0 @@ - - - diff --git a/src/.vuepress/components/forms-4.vue b/src/.vuepress/components/forms-4.vue deleted file mode 100644 index 2d0063f0e5..0000000000 --- a/src/.vuepress/components/forms-4.vue +++ /dev/null @@ -1,22 +0,0 @@ - - - diff --git a/src/.vuepress/components/forms-5.vue b/src/.vuepress/components/forms-5.vue deleted file mode 100644 index 4244d0c97c..0000000000 --- a/src/.vuepress/components/forms-5.vue +++ /dev/null @@ -1,21 +0,0 @@ - - - diff --git a/src/.vuepress/components/forms-6.vue b/src/.vuepress/components/forms-6.vue deleted file mode 100644 index 4bebf6074f..0000000000 --- a/src/.vuepress/components/forms-6.vue +++ /dev/null @@ -1,21 +0,0 @@ - - - diff --git a/src/.vuepress/components/forms-7.vue b/src/.vuepress/components/forms-7.vue deleted file mode 100644 index 574fce39e7..0000000000 --- a/src/.vuepress/components/forms-7.vue +++ /dev/null @@ -1,21 +0,0 @@ - - - diff --git a/src/.vuepress/components/forms-8.vue b/src/.vuepress/components/forms-8.vue deleted file mode 100644 index a3da617f2d..0000000000 --- a/src/.vuepress/components/forms-8.vue +++ /dev/null @@ -1,25 +0,0 @@ - - - diff --git a/src/.vuepress/components/tab-archive.vue b/src/.vuepress/components/tab-archive.vue deleted file mode 100644 index 9b54ae2c81..0000000000 --- a/src/.vuepress/components/tab-archive.vue +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/src/.vuepress/components/tab-home.vue b/src/.vuepress/components/tab-home.vue deleted file mode 100644 index 566d6caaab..0000000000 --- a/src/.vuepress/components/tab-home.vue +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/src/.vuepress/components/tab-posts.vue b/src/.vuepress/components/tab-posts.vue deleted file mode 100644 index 3d01a211f3..0000000000 --- a/src/.vuepress/components/tab-posts.vue +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/src/guide/component-basics.md b/src/guide/component-basics.md index dfea033cc3..6d36059611 100644 --- a/src/guide/component-basics.md +++ b/src/guide/component-basics.md @@ -1,7 +1,5 @@ # Components Basics -
Watch a free video course on Vue School
- ## 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, ` +

+ See the Pen + Component basics by Vue (@Vue) + on CodePen. +

+ 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: ``` - +

+ See the Pen + Component basics: reusing components by Vue (@Vue) + on CodePen. +

+ 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 - - - +
+ + + +
``` - +

+ See the Pen + Component basics: passing props by Vue (@Vue) + on CodePen. +

+ 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
-
+
+

+ See the Pen + Component basics: emitting events by Vue (@Vue) + on CodePen. +

+ ### 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: - +

+ See the Pen + Component basics: slots by Vue (@Vue) + on CodePen. +

+ 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: - +

+ See the Pen + Component basics: dynamic components by Vue (@Vue) + on CodePen. +

+ 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 -
+
@@ -59,19 +64,24 @@ Vue.createApp({ } } } -}).mount('#example-2') +}).mount('#event-with-method') ``` Result: - +

+ See the Pen + Event handling: with a method by Vue (@Vue) + on CodePen. +

+ ## Methods in Inline Handlers Instead of binding directly to a method name, we can also use methods in an inline JavaScript statement: ```html -
+
@@ -84,12 +94,17 @@ Vue.createApp({ alert(message) } } -}).mount('#example-3') +}).mount('#inline-handler') ``` Result: - +

+ See the Pen + Event handling: with an inline handler by Vue (@Vue) + on CodePen. +

+ 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)

Message is: {{ message }}

``` - +

+ See the Pen + Handling forms: basic v-model by Vue (@Vue) + on CodePen. +

+ ### Multiline text @@ -37,7 +42,12 @@ For languages that require an [IME](https://en.wikipedia.org/wiki/Input_method) ``` - +

+ See the Pen + Handling forms: textarea by Vue (@Vue) + on CodePen. +

+ Interpolation on textareas won't work. Use `v-model` instead. @@ -58,12 +68,17 @@ Single checkbox, boolean value: ``` - +

+ See the Pen + Handling forms: checkbox by Vue (@Vue) + on CodePen. +

+ Multiple checkboxes, bound to the same Array: ```html -
+
@@ -82,15 +97,20 @@ Vue.createApp({ checkedNames: [] } } -}).mount('#example-3') +}).mount('#v-model-multiple-checkboxes') ``` - +

+ See the Pen + Handling forms: multiple checkboxes by Vue (@Vue) + on CodePen. +

+ ### Radio ```html -
+

@@ -108,17 +128,22 @@ Vue.createApp({ picked: '' } } -}).mount('#example-4') +}).mount('#v-model-radiobutton') ``` - +

+ See the Pen + Handling forms: radiobutton by Vue (@Vue) + on CodePen. +

+ ### Select Single select: ```html -
+
` element will render in an "unselected" state. On iOS this will cause the user not being able to select the first item because iOS does not fire a change event in this case. It is therefore recommended to provide a disabled option with an empty value, as demonstrated in the example above. @@ -157,12 +187,17 @@ Multiple select (bound to Array): Selected: {{ selected }} ``` - +

+ See the Pen + Handling forms: select bound to array by Vue (@Vue) + on CodePen. +

+ Dynamic options rendered with `v-for`: ```html -
+