Skip to content

Commit 8e1c7a2

Browse files
committed
Merge remote-tracking branch 'upstream/master' into dev
2 parents 5dbe97b + 56eef72 commit 8e1c7a2

17 files changed

+320
-139
lines changed

src/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const sidebar = {
1515
'/guide/introduction',
1616
'/guide/instance',
1717
'/guide/template-syntax',
18+
'/guide/data-methods',
1819
'/guide/computed',
1920
'/guide/class-and-style',
2021
'/guide/conditional',

src/api/composition-api.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,58 @@ const foo = inject<string>('foo') // string | undefined
158158
- **See also**:
159159
- [Provide / Inject](../guide/component-provide-inject.html)
160160
- [Composition API Provide / Inject](../guide/composition-api-provide-inject.html)
161+
162+
## `getCurrentInstance`
163+
164+
`getCurrentInstance` enables access to an internal component instance useful for advanced usages or for library creators.
165+
166+
```ts
167+
import { getCurrentIntance } from 'vue'
168+
169+
const MyComponent = {
170+
setup() {
171+
const internalIntance = getCurrentInstance()
172+
173+
internalInstance.appContext.config.globalProperties // access to globalProperties
174+
}
175+
}
176+
```
177+
178+
`getCurrentInstance` **only** works during [setup](#setup) or [Lifecycle Hooks](#lifecycle-hooks)
179+
180+
> When using outside of [setup](#setup) or [Lifecycle Hooks](#lifecycle-hooks), please call `getCurrentInstance()` on `setup` and use the instance instead.
181+
182+
```ts
183+
const MyComponent = {
184+
setup() {
185+
const internalIntance = getCurrentInstance() // works
186+
187+
const id = useComponentId() // works
188+
189+
const handleClick = () => {
190+
getCurrentInstance() // doesn't work
191+
useComponentId() // doesn't work
192+
193+
internalIntance // works
194+
}
195+
196+
onMounted(() => {
197+
getCurrentInstance() // works
198+
})
199+
200+
return () =>
201+
h(
202+
'button',
203+
{
204+
onClick: handleClick
205+
},
206+
`uid: ${id}`
207+
)
208+
}
209+
}
210+
211+
// also works if called on a composable
212+
function useComponentId() {
213+
return getCurrentInstance().uid
214+
}
215+
```

src/api/directives.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
<!-- shorthand -->
198198
<button @click="doThis"></button>
199199

200-
<!-- shorthand dynamic event (2.6.0+) -->
200+
<!-- shorthand dynamic event -->
201201
<button @[event]="doThis"></button>
202202

203203
<!-- stop propagation -->

src/api/instance-methods.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@
158158
If you still want to call an unwatch function inside the callback, you should check its availability first:
159159

160160
```js
161-
const unwatch = vm.$watch(
161+
let unwatch = null
162+
163+
unwatch = vm.$watch(
162164
'value',
163165
function() {
164166
doSomething()

src/api/options-data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292

9393
- **Details:**
9494

95-
Computed properties to be mixed into the Vcomponent instance. All getters and setters have their `this` context automatically bound to the component instance.
95+
Computed properties to be mixed into the component instance. All getters and setters have their `this` context automatically bound to the component instance.
9696

9797
Note that if you use an arrow function with a computed property, `this` won't be the component's instance, but you can still access the instance as the function's first argument:
9898

src/guide/component-basics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Então, o componente filho pode emitir um evento por si próprio chamando o [mé
234234
</button>
235235
```
236236

237-
Graças à escuta `v-on:enlarge-text="postFontSize += 0.1"`, o componente pai receberá o evento e atualizará o valor de `postFontSize`.
237+
Graças à escuta `@enlarge-text="postFontSize += 0.1"`, o componente pai receberá o evento e atualizará o valor de `postFontSize`.
238238

239239
<p class="codepen" data-height="300" data-theme-id="39028" data-default-tab="html,result" data-user="Vue" data-slug-hash="KKpGyrp" data-editable="true" style="height: 300px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Básico sobre Componentes: Emitindo Eventos">
240240
<span>Veja o exemplo <a href="https://codepen.io/vuejs-br/pen/jOqXJEv">

src/guide/component-slots.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Or even other components:
4848
<todo-button>
4949
<!-- Use a component to add an icon -->
5050
<font-awesome-icon name="plus"></font-awesome-icon>
51-
Your Profile
51+
Add todo
5252
</todo-button>
5353
```
5454

@@ -263,7 +263,7 @@ To make `item` available to the slot content provided by the parent, we can add
263263
```html
264264
<ul>
265265
<li v-for="( item, index ) in items">
266-
<slot v-bind:item="item"></slot>
266+
<slot :item="item"></slot>
267267
</li>
268268
</ul>
269269
```
@@ -330,7 +330,7 @@ Whenever there are multiple slots, use the full `<template>` based syntax for _a
330330
<template v-slot:other="otherSlotProps">
331331
...
332332
</template>
333-
</current-user>
333+
</todo-list>
334334
```
335335

336336
### Destructuring Slot Props

src/guide/composition-api-provide-inject.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default {
3636
<!-- src/components/MyMarker.vue -->
3737
<script>
3838
export default {
39-
inject: ['location', 'longitude', 'latitude']
39+
inject: ['location', 'geolocation']
4040
}
4141
</script>
4242
```

src/guide/data-methods.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Data Properties and Methods
2+
3+
## Data Properties
4+
5+
The `data` option for a component is a function. Vue calls this function as part of creating a new component instance. It should return an object, which Vue will then wrap in its reactivity system and store on the component instance as `$data`. For convenience, any top-level properties of that object are also exposed directly via the component instance:
6+
7+
```js
8+
const app = Vue.createApp({
9+
data() {
10+
return { count: 4 }
11+
}
12+
})
13+
14+
const vm = app.mount('#app')
15+
16+
console.log(vm.$data.count) // => 4
17+
console.log(vm.count) // => 4
18+
19+
// Assigning a value to vm.count will also update $data.count
20+
vm.count = 5
21+
console.log(vm.$data.count) // => 5
22+
23+
// ... and vice-versa
24+
vm.$data.count = 6
25+
console.log(vm.count) // => 6
26+
```
27+
28+
These instance properties are only added when the instance is first created, so you need to ensure they are all present in the object returned by the `data` function. Where necessary, use `null`, `undefined` or some other placeholder value for properties where the desired value isn't yet available.
29+
30+
It is possible to add a new property directly to the component instance without including it in `data`. However, because this property isn't backed by the reactive `$data` object, it won't automatically be tracked by [Vue's reactivity system](reactivity.html).
31+
32+
Vue uses a `$` prefix when exposing its own built-in APIs via the component instance. It also reserves the prefix `_` for internal properties. You should avoid using names for top-level `data` properties that start with either of these characters.
33+
34+
## Methods
35+
36+
To add methods to a component instance we use the `methods` option. This should be an object containing the desired methods:
37+
38+
```js
39+
const app = Vue.createApp({
40+
data() {
41+
return { count: 4 }
42+
},
43+
methods: {
44+
increment() {
45+
// `this` will refer to the component instance
46+
this.count++
47+
}
48+
}
49+
})
50+
51+
const vm = app.mount('#app')
52+
53+
console.log(vm.count) // => 4
54+
55+
vm.increment()
56+
57+
console.log(vm.count) // => 5
58+
```
59+
60+
Vue automatically binds the `this` value for `methods` so that it always refers to the component instance. This ensures that a method retains the correct `this` value if it's used as an event listener or callback. You should avoid using arrow functions when defining `methods`, as that prevents Vue from binding the appropriate `this` value.
61+
62+
Just like all other properties of the component instance, the `methods` are accessible from within the component's template. Inside a template they are most commonly used as event listeners:
63+
64+
```html
65+
<button @click="increment">Up vote</button>
66+
```
67+
68+
In the example above, the method `increment` will be called when the `<button>` is clicked.
69+
70+
It is also possible to call a method directly from a template. As we'll see shortly, it's usually better to use a [computed property](computed.html) instead. However, using a method can be useful in scenarios where computed properties aren't a viable option. You can call a method anywhere that a template supports JavaScript expressions:
71+
72+
```html
73+
<span :title="toTitleDate(date)">
74+
{{ formatDate(date) }}
75+
</span>
76+
```
77+
78+
If the methods `toTitleDate` or `formatDate` access any reactive data then it will be tracked as a rendering dependency, just as if it had been used in the template directly.
79+
80+
Methods called from a template should not have any side effects, such as changing data or triggering asynchronous processes. If you find yourself tempted to do that you should probably use a [lifecycle hook](instance.html#lifecycle-hooks) instead.
81+
82+
### Debouncing and Throttling
83+
84+
Vue doesn't include built-in support for debouncing or throttling but it can be implemented using libraries such as [Lodash](https://lodash.com/).
85+
86+
In cases where a component is only used once, the debouncing can be applied directly within `methods`:
87+
88+
```html
89+
<script src="https://unpkg.com/[email protected]/lodash.min.js"></script>
90+
<script>
91+
Vue.createApp({
92+
methods: {
93+
// Debouncing with Lodash
94+
click: _.debounce(function() {
95+
// ... respond to click ...
96+
}, 500)
97+
}
98+
}).mount('#app')
99+
</script>
100+
```
101+
102+
However, this approach is potentially problematic for components that are reused because they'll all share the same debounced function. To keep the component instances independent from each other, we can add the debounced function in the `created` lifecycle hook:
103+
104+
```js
105+
app.component('save-button', {
106+
created() {
107+
// Debouncing with Lodash
108+
this.debouncedClick = _.debounce(this.click, 500)
109+
},
110+
unmounted() {
111+
// Cancel the timer when the component is removed
112+
this.debouncedClick.cancel()
113+
},
114+
methods: {
115+
click() {
116+
// ... respond to click ...
117+
}
118+
},
119+
template: `
120+
<button @click="debouncedClick">
121+
Save
122+
</button>
123+
`
124+
})
125+
```

0 commit comments

Comments
 (0)