-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Migrate instance methods API #78
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da74edb
feat: added watch
NataliaTepluhina 4430142
feat: added instance methods
NataliaTepluhina 4832f7b
feat: added explanation on Object watcher
NataliaTepluhina 860a729
Merge branch 'master' into instance-methods-api
NataliaTepluhina 749f3be
fix: fixed headers
NataliaTepluhina b38fbf5
Merge branch 'instance-methods-api' of github.com:vuejs/docs-next int…
NataliaTepluhina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
# Instance Methods | ||
|
||
## $watch | ||
|
||
- **Arguments:** | ||
|
||
- `{string | Function} source` | ||
- `{Function | Object} callback` | ||
- `{Object} [options]` | ||
- `{boolean} deep` | ||
- `{boolean} immediate` | ||
|
||
- **Returns:** `{Function} unwatch` | ||
|
||
- **Usage:** | ||
|
||
Watch an expression or a computed function on the Vue instance for changes. The callback gets called with the new value and the old value. The expression only accepts dot-delimited paths. For more complex expressions, use a function instead. | ||
|
||
- **Example:** | ||
|
||
```js | ||
const app = Vue.createApp({ | ||
data() { | ||
return { | ||
a: 1, | ||
b: 2 | ||
} | ||
}, | ||
created() { | ||
// keypath | ||
this.$watch('a', (newVal, oldVal) => { | ||
// do something | ||
}) | ||
|
||
// function | ||
this.$watch( | ||
// every time the expression `this.a + this.b` yields a different result, | ||
// the handler will be called. It's as if we were watching a computed | ||
// property without defining the computed property itself | ||
() => this.a + this.b, | ||
(newVal, oldVal) => { | ||
// do something | ||
} | ||
) | ||
} | ||
}) | ||
``` | ||
|
||
When watched value is an Object or Array, any changes to its properties or elements won't trigger the watcher because they reference the same Object/Array: | ||
|
||
```js | ||
const app = Vue.createApp({ | ||
data() { | ||
return { | ||
article: { | ||
text: 'Vue is awesome!' | ||
}, | ||
comments: ['Indeed!', 'I agree'] | ||
} | ||
}, | ||
created() { | ||
this.$watch('article', () => { | ||
console.log('Article changed!') | ||
}) | ||
|
||
this.$watch('comments', () => { | ||
console.log('Comments changed!') | ||
}) | ||
}, | ||
methods: { | ||
// These methods won't trigger a watcher because we changed only a property of Object/Array, | ||
// not the Object/Array itself | ||
changeArticleText() { | ||
this.article.text = 'Vue 3 is awesome' | ||
}, | ||
addComment() { | ||
this.comments.push('New comment') | ||
}, | ||
|
||
// These methods will trigger a watcher because we replaced Object/Array completely | ||
changeWholeArticle() { | ||
this.article = { text: 'Vue 3 is awesome' } | ||
}, | ||
clearComments() { | ||
this.comments = [] | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
`$watch` returns an unwatch function that stops firing the callback: | ||
|
||
```js | ||
const app = Vue.createApp({ | ||
data() { | ||
return { | ||
a: 1 | ||
} | ||
} | ||
}) | ||
|
||
const vm = app.mount('#app') | ||
|
||
const unwatch = vm.$watch('a', cb) | ||
// later, teardown the watcher | ||
unwatch() | ||
``` | ||
|
||
- **Option: deep** | ||
|
||
To also detect nested value changes inside Objects, you need to pass in `deep: true` in the options argument. Note that you don't need to do so to listen for Array mutations. | ||
|
||
```js | ||
vm.$watch('someObject', callback, { | ||
deep: true | ||
}) | ||
vm.someObject.nestedValue = 123 | ||
// callback is fired | ||
``` | ||
|
||
- **Option: immediate** | ||
|
||
Passing in `immediate: true` in the option will trigger the callback immediately with the current value of the expression: | ||
|
||
```js | ||
vm.$watch('a', callback, { | ||
immediate: true | ||
}) | ||
// `callback` is fired immediately with current value of `a` | ||
``` | ||
|
||
Note that with `immediate` option you won't be able to unwatch the given property on the first callback call. | ||
|
||
```js | ||
// This will cause an error | ||
const unwatch = vm.$watch( | ||
'value', | ||
function() { | ||
doSomething() | ||
unwatch() | ||
}, | ||
{ immediate: true } | ||
) | ||
``` | ||
|
||
If you still want to call an unwatch function inside the callback, you should check its availability first: | ||
|
||
```js | ||
const unwatch = vm.$watch( | ||
'value', | ||
function() { | ||
doSomething() | ||
if (unwatch) { | ||
unwatch() | ||
} | ||
}, | ||
{ immediate: true } | ||
) | ||
``` | ||
|
||
- **See also:** [Watchers](../guide/computed.html#watchers) | ||
|
||
## $emit | ||
|
||
- **Arguments:** | ||
|
||
- `{string} eventName` | ||
- `[...args]` | ||
|
||
Trigger an event on the current instance. Any additional arguments will be passed into the listener's callback function. | ||
|
||
- **Examples:** | ||
|
||
Using `$emit` with only an event name: | ||
|
||
```html | ||
<div id="emit-example-simple"> | ||
<welcome-button v-on:welcome="sayHi"></welcome-button> | ||
NataliaTepluhina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</div> | ||
``` | ||
|
||
```js | ||
const app = Vue.createApp({ | ||
methods: { | ||
sayHi() { | ||
console.log('Hi!') | ||
} | ||
} | ||
}) | ||
|
||
app.component('welcome-button', { | ||
template: ` | ||
<button v-on:click="$emit('welcome')"> | ||
Click me to be welcomed | ||
</button> | ||
` | ||
}) | ||
|
||
app.mount('#emit-example-simple') | ||
``` | ||
|
||
Using `$emit` with additional arguments: | ||
|
||
```html | ||
<div id="emit-example-argument"> | ||
<advice-component v-on:give-advice="showAdvice"></advice-component> | ||
</div> | ||
``` | ||
|
||
```js | ||
const app = Vue.createApp({ | ||
methods: { | ||
showAdvice(advice) { | ||
alert(advice) | ||
} | ||
} | ||
}) | ||
|
||
app.component('advice-component', { | ||
data() { | ||
return { | ||
adviceText: 'Some advice' | ||
} | ||
}, | ||
template: ` | ||
<div> | ||
<input type="text" v-model="adviceText"> | ||
<button v-on:click="$emit('give-advice', adviceText)"> | ||
Click me for sending advice | ||
</button> | ||
</div> | ||
` | ||
}) | ||
``` | ||
|
||
- **See also:** | ||
- [`emits` option](./options-data.html#emits) | ||
- [Emitting a Value With an Event](../guide/component-basics.html#emitting-a-value-with-an-event) | ||
|
||
## $forceUpdate | ||
|
||
- **Usage:** | ||
|
||
Force the Vue instance to re-render. Note it does not affect all child components, only the instance itself and child components with inserted slot content. | ||
|
||
## $nextTick | ||
|
||
- **Arguments:** | ||
|
||
- `{Function} [callback]` | ||
|
||
- **Usage:** | ||
|
||
Defer the callback to be executed after the next DOM update cycle. Use it immediately after you've changed some data to wait for the DOM update. This is the same as the global `Vue.nextTick`, except that the callback's `this` context is automatically bound to the instance calling this method. | ||
|
||
- **Example:** | ||
|
||
```js | ||
Vue.createApp({ | ||
// ... | ||
methods: { | ||
// ... | ||
example() { | ||
// modify data | ||
this.message = 'changed' | ||
// DOM is not updated yet | ||
this.$nextTick(function() { | ||
// DOM is now updated | ||
// `this` is bound to the current instance | ||
this.doSomethingElse() | ||
}) | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
- **See also:** [Vue.nextTick](TODO) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.