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 `