From 5adbc5f902a3562a3e233c06f99f9be30254741a Mon Sep 17 00:00:00 2001 From: Sarah Drasner Date: Mon, 26 Mar 2018 08:48:06 -0500 Subject: [PATCH] Moving Components Page into its Own Category (#1482) * create components sidebar * break up first two sections * break it up further * split up the rest * clean up * initial pass, creating new, much slimmer components intro page that links to more in-depth pages * finish initial component registration page * finish initial refactor of component props page * beginning work on components custom events page * finish initial components custom events page * finish initial components slots page * finish initial dynamic and async components page * finish initial dynamic-async and edge-cases components pages * components review and tweaks up to the props page * fix components mistakes found by @phanan * fix typo in components-edge-cases * reorder Compiliation Scope in components-slots * fix link in components basics * fix codeblock language typo in components * add example of local registration in es2015 modules * add keep-alive example * remove redundant word in components * make dom template parsing caveats h2 * change simple example to base example in components * add async import example to circular references * fix typo in components-custom-events * remove redundand language in componenents-custom-events * add missing comma in components-custom-events * restate context in component-dynamic-async * remove redundant actuallys in components-dynamic-async * clarify .sync modifier usage * set up hash redirects for all old components sections * remove note about intentional single root elements * fix dom template parsing caveats example * fix closing tags in components-edge-cases * fix typo in components-edge-cases * fix typo in components-edge-cases * remove extra 'a' in components-registration * add name casing and various tweaks to components-registration * complete custom v-model example in components-custom-events * minor tweaks to components pages * rename signature to definition for clarity --- _config.yml | 1 - package.json | 2 +- src/v2/api/index.md | 2 +- src/v2/guide/components-custom-events.md | 168 +++ src/v2/guide/components-dynamic-async.md | 273 ++++ src/v2/guide/components-edge-cases.md | 319 ++++ src/v2/guide/components-props.md | 314 ++++ src/v2/guide/components-registration.md | 226 +++ src/v2/guide/components-slots.md | 229 +++ src/v2/guide/components.md | 1732 ++++++---------------- themes/vue/layout/partials/toc.ejs | 3 + themes/vue/source/css/_demo.styl | 15 +- themes/vue/source/js/common.js | 102 +- yarn.lock | 470 ++++-- 14 files changed, 2375 insertions(+), 1481 deletions(-) create mode 100644 src/v2/guide/components-custom-events.md create mode 100644 src/v2/guide/components-dynamic-async.md create mode 100644 src/v2/guide/components-edge-cases.md create mode 100644 src/v2/guide/components-props.md create mode 100644 src/v2/guide/components-registration.md create mode 100644 src/v2/guide/components-slots.md diff --git a/_config.yml b/_config.yml index 2eb260398d..3c5d26a12b 100644 --- a/_config.yml +++ b/_config.yml @@ -187,4 +187,3 @@ alias: examples/svg.html: v2/examples/svg.html examples/todomvc.html: v2/examples/todomvc.html examples/tree-view.html: v2/examples/tree-view.html - diff --git a/package.json b/package.json index 287873960d..f0bc748e4e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vuejs.org", "private": true, "hexo": { - "version": "3.4.2" + "version": "3.6.0" }, "scripts": { "start": "hexo server", diff --git a/src/v2/api/index.md b/src/v2/api/index.md index 74bb867e9c..3dc22d00c3 100644 --- a/src/v2/api/index.md +++ b/src/v2/api/index.md @@ -1432,7 +1432,7 @@ type: api - **Details:** - Contains parent-scope `v-on` event listeners (without `.native` modifiers). This can be passed down to an inner component via `v-on="$listeners"` - useful when creating higher-order components. + Contains parent-scope `v-on` event listeners (without `.native` modifiers). This can be passed down to an inner component via `v-on="$listeners"` - useful when creating transparent wrapper components. ## Instance Methods / Data diff --git a/src/v2/guide/components-custom-events.md b/src/v2/guide/components-custom-events.md new file mode 100644 index 0000000000..de393a0a53 --- /dev/null +++ b/src/v2/guide/components-custom-events.md @@ -0,0 +1,168 @@ +--- +title: Custom Events +type: guide +order: 103 +--- + +> This page assumes you've already read the [Components Basics](components.html). Read that first if you are new to components. + +## Event Names + +Unlike components and props, event names don't provide any automatic case transformation. Instead, the name of an emitted event must exactly match the name used to listen to that event. For example, if emitting a camelCased event name: + +```js +this.$emit('myEvent') +``` + +Listening to the kebab-cased version will have no effect: + +```html + +``` + +Unlike components and props, event names will never be used as variable or property names in JavaScript, so there's no reason to use camelCase or PascalCase. Additionally, `v-on` event listeners inside DOM templates will be automatically transformed to lowercase (due to HTML's case-insensitivity), so `v-on:myEvent` would become `v-on:myevent` -- making `myEvent` impossible to listen to. + +For these reasons, we recommend you **always use kebab-case for event names**. + +## Customizing Component `v-model` + +> New in 2.2.0+ + +By default, `v-model` on a component uses `value` as the prop and `input` as the event, but some input types such as checkboxes and radio buttons may want to use the `value` attribute for a [different purpose](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Value). Using the `model` option can avoid a conflict in such cases: + +```js +Vue.component('base-checkbox', { + model: { + prop: 'checked', + event: 'change' + }, + props: { + checked: Boolean + }, + template: ` + + ` +}) +``` + +Now when using `v-model` on this component: + +```js + +``` + +the value of `lovingVue` will be passed to the `checked` prop. The `lovingVue` property will then be updated when `` emits a `change` event with a new value. + +

Note that you still have to declare the checked prop in component's props option.

+ +## Binding Native Events to Components + +There may be times when you want to listen directly to a native event on the root element of a component. In these cases, you can use the `.native` modifier for `v-on`: + +```html + +``` + +This can be useful sometimes, but it's not a good idea when you're trying to listen on a very specific element, like an ``. For example, the `` component above might refactor so that the root element is actually a `