10000 Add an emits option to examples that emit events by skirtles-code · Pull Request #705 · vuejs/docs · GitHub
[go: up one dir, main page]

Skip to content

Add an emits option to examples that emit events #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/api/instance-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
})

app.component('welcome-button', {
emits: ['welcome'],
template: `
<button v-on:click="$emit('welcome')">
Click me to be welcomed
Expand All @@ -236,7 +237,7 @@

```html
<div id="emit-example-argument">
<advice-component v-on:give-advice="showAdvice"></advice-component>
<advice-component v-on:advise="showAdvice"></advice-component>
</div>
```

Expand All @@ -250,6 +251,7 @@
})

app.component('advice-component', {
emits: ['advise'],
data() {
return {
adviceText: 'Some advice'
Expand All @@ -258,12 +260,14 @@
template: `
<div>
<input type="text" v-model="adviceText">
<button v-on:click="$emit('give-advice', adviceText)">
<button v-on:click="$emit('advise', adviceText)">
Click me for sending advice
</button>
</div>
`
})

app.mount('#emit-example-argument')
```

- **See also:**
Expand Down
2 changes: 2 additions & 0 deletions src/guide/component-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ Here's that in action:
```js
app.component('custom-input', {
props: ['modelValue'],
emits: ['update:modelValue'],
template: `
<input
:value="modelValue"
Expand All @@ -327,6 +328,7 @@ Keep in mind, the `get` method should return the `modelValue` property, or which
```js
app.component('custom-input', {
props: ['modelValue'],
emits: ['update:modelValue'],
template: `
<input v-model="value">
`,
Expand Down
21 changes: 11 additions & 10 deletions src/guide/component-custom-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,11 @@ By default, `v-model` on a component uses `modelValue` as the prop and `update:m
In this case, child component will expect a `title` prop and emits `update:title` event to sync:

```js
const app = Vue.createApp({})

app.component('my-component', {
props: {
title: String
},
emits: ['update:title'],
template: `
<input
type="text"
Expand Down Expand Up @@ -123,13 +122,12 @@ Each v-model will sync to a different prop, without the need for extra options i
```

```js
const app = Vue.createApp({})

app.component('user-name', {
props: {
firstName: String,
lastName: String
},
emits: ['update:firstName', 'update:lastName'],
template: `
<input
type="text"
Expand Down Expand Up @@ -157,7 +155,7 @@ Modifiers added to a component `v-model` will be provided to the component via t
Notice that when the component's `created` lifecycle hook triggers, the `modelModifiers` prop contains `capitalize` and its value is `true` - due to it being set on the `v-model` binding `v-model.capitalize="bar"`.

```html
<my-component v-model.capitalize="bar"></my-component>
<my-component v-model.capitalize="myText"></my-component>
```

```js
Expand All @@ -168,6 +166,7 @@ app.component('my-component', {
default: () => ({})
}
},
emits: ['update:modelValue'],
template: `
<input type="text"
:value="modelValue"
Expand Down Expand Up @@ -204,6 +203,7 @@ app.component('my-component', {
default: () => ({})
}
},
emits: ['update:modelValue'],
methods: {
emitValue(e) {
let value = e.target.value
Expand All @@ -225,19 +225,20 @@ app.mount('#app')
For `v-model` bindings with arguments, the generated prop name will be `arg + "Modifiers"`:

```html
<my-component v-model:foo.capitalize="bar"></my-component>
<my-component v-model:description.capitalize="myText"></my-component>
```

```js
app.component('my-component', {
props: ['foo', 'fooModifiers'],
props: ['description', 'descriptionModifiers'],
emits: ['update:description'],
template: `
<input type="text"
:value="foo"
@input="$emit('update:foo', $event.target.value)">
:value="description"
@input="$emit('update:description', $event.target.value)">
`,
created() {
console.log(this.fooModifiers) // { capitalize: true }
console.log(this.descriptionModifiers) // { capitalize: true }
}
})
```
3 changes: 2 additions & 1 deletion src/guide/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ app.component('todo-item', {
<button @click="$emit('remove')">Remove</button>
</li>
`,
props: ['title']
props: ['title'],
emits: ['remove']
})

app.mount('#todo-list-example')
Expand Down
1 change: 1 addition & 0 deletions src/guide/migration/v-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ We recommend:
props: {
modelValue: String // previously was `value: String`
},
emits: ['update:modelValue'],
methods: {
changePageTitle(title) {
this.$emit('update:modelValue', title) // previously was `this.$emit('input', title)`
Expand Down
1 change: 1 addition & 0 deletions src/guide/render-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ The `v-model` directive is expanded to `modelValue` and `onUpdate:modelValue` pr

```js
props: ['modelValue'],
emits: ['update:modelValue'],
render() {
return Vue.h(SomeComponent, {
modelValue: this.modelValue,
Expand Down
0