Skip to content

Commit b60ae59

Browse files
[Mise à jour] — Juin 2018 (#137)
* Update unit-testing-vue-components.md (vuejs#1623) * Fixed a typo (vuejs#1625) Removed an extra 's' to fix a grammatical mistake * fix a typo (vuejs#1627) * Update v-for list example (vuejs#1628) * Add presentation role to style-only li item * fix: code snippet language (close vuejs#1617) (vuejs#1626) * Update todo-list-example for a11y (vuejs#1630) * Update todo-list-example for a11y * Implement review suggestions * Use explicit label instead of aria-label * Move v-model directive to top for improved readability * change NODE_ENV to follow recommendations from webpack (vuejs#1622) * change NODE_ENV to follow recommendations from weback * Update deployment.md * note that modes are not available in transition groups, fixes vuejs#1619 * add examples for in API doc * add comparison note about react-like libraries * Update components.md (vuejs#1632) * Update components.md * Update components.md * improve flow of single root component section * cookbook entry for storage (vuejs#1550) * cookbook entry for storage * minor tweak * updates to article * updates to article * maybe the last tweak * the honest to god really last mod * Update index.md (vuejs#1634) * docs(guide/comparison): correct size stat of Angular CLI project (vuejs#1638) * Update client-side-storage.md (vuejs#1640) * Update creating-custom-scroll-directives.md (vuejs#1639) * chore: update ad code * remove unnecessary word 'know' (vuejs#1641) * Updated broken links for the Vue Test Utils documentation. (vuejs#1643) * Reorganize component props to introduce prop types earlier, fixes vuejs#1644 (vuejs#1646) @BlitZz96 brought up a good point in [this issue](vuejs#1644) about users lacking the necessary background knowledge to fully understand Passing Static and Dynamic Props, at least as it relates to booleans and potentially other types in the future. To fix this, I added a new Prop Types section right before it. * First stab at SFC to npm documentation (vuejs#1558) * First stab at SFC to npm documentation * Clean up sample code, clarify .vue usage with SSR builds * Run build tasks in parallel, fix dependency order * Backtick all instances of .vue, minor edits for clarity * Fix typo on link * Update url to vue-sfc-rollup utility in acknowledgements * Fix order, other edits suggested by @sdras * Additional explanation about sfc-to-npm recipe package.json * Rename packaing-sfc-for-npm.md to packaging-sfc-for-npm.md (vuejs#1652) * Update link in comparison guide (vuejs#1653) The phrase "These state management patterns and even Redux itself can be easily integrated into Vue applications" has a link to a deprecated project. Instead I link to a yarnpkg.com search I saw used in https://vuejs.org/v2/guide/state-management.html, which links all "redux vue" projects (so it's always up to date) * chore: update vue version * Fix link to docs on custom inputs (vuejs#1660) The old link redirected me to https://vuejs.org/v2/guide/components-custom-events.html#Binding-Native-Events-to-Components which was unexpected. I think the correct link is https://vuejs.org/v2/guide/components.html#Using-v-model-on-Components as per this change. * correct wrong link (vuejs#1661) * Update events.md correct wrong link * Update events.md correct wrong link * Feature/vuemeetups (vuejs#1665) * feature: Add link to VueMeetups * feature: Add section on becoming a community leader * minor tweaks to meetup section * Added details about Lifecycle Hooks (vuejs#1664) * Added details about Lifecycle Hooks Mostly a clarification for beginners without prior knowledge about what Lifecycle hooks are. Makes it easier to understand when perhaps only knowing that the developer has a `mounted()` hook. I left out the function syntax because of generality. Add if it makes more sense to add. I am not fully knowledgable in Vue yet. * add (de)activated to lifecycle hooks list in style guide * improve comparisons react scaling down section * add text versions of image code examples * remove extra comma in components * TypeScript project generation in @vue/cli 3.0 (vuejs#1668) * TypeScript project generation in @vue/cli 3.0 update information about generating TypeScript project using new @vue/cli 3.0 * tweaks to Vue CLI 3 TypeScript support docs * chore: update sponsors * Sylvain and Forresst feedbacks Signed-off-by: MachinisteWeb <[email protected]> * @ForrestT and @raisonblue feedback Signed-off-by: MachinisteWeb <[email protected]> * Feedback of @rspt Signed-off-by: MachinisteWeb <[email protected]>
1 parent 9a0b18f commit b60ae59

32 files changed

+1184
-396
lines changed
48.8 KB
Loading

src/images/devtools-storage-edge.png

48.1 KB
Loading

src/images/devtools-storage.png

118 KB
Loading

src/v2/api/index.md

+127-2
Original file line numberDiff line numberDiff line change
@@ -1590,14 +1590,139 @@ type: api
15901590
15911591
- Si l'évènement et la fonction de rappel sont fournis, supprime l'écouteur uniquement pour cet événément et cette fonction de rappel spécifique.
15921592
1593-
### vm.$emit( event, [...args] )
1593+
### vm.$emit( eventName, [...args] )
15941594
15951595
- **Arguments :**
1596-
- `{string} évènement`
1596+
- `{string} nom d'évènement`
15971597
- `[...arguments]`
15981598
15991599
Déclenche un évènement sur l'instance actuelle. Tous les arguments additionnels sont passés à la fonction de rappel de l'écouteur.
16001600
1601+
- **Exemples:**
1602+
1603+
Utiliser `$emit` avec un nom d'évènement :
1604+
1605+
```js
1606+
Vue.component('welcome-button', {
1607+
template: `
1608+
<button v-on:click="$emit('welcome')">
1609+
Cliquez-moi pour être salué
1610+
</button>
1611+
`
1612+
})
1613+
```
1614+
```html
1615+
<div id="emit-example-simple">
1616+
<welcome-button v-on:welcome="sayHi"></welcome-button>
1617+
</div>
1618+
```
1619+
```js
1620+
new Vue({
1621+
el: '#emit-example-simple',
1622+
methods: {
1623+
sayHi: function () {
1624+
alert('Salut !')
1625+
}
1626+
}
1627+
})
1628+
```
1629+
{% raw %}
1630+
<div id="emit-example-simple" class="demo">
1631+
<welcome-button v-on:welcome="sayHi"></welcome-button>
1632+
</div>
1633+
<script>
1634+
Vue.component('welcome-button', {
1635+
template: `
1636+
<button v-on:click="$emit('welcome')">
1637+
Cliquez-moi pour être salué
1638+
</button>
1639+
`
1640+
})
1641+
new Vue({
1642+
el: '#emit-example-simple',
1643+
methods: {
1644+
sayHi: function () {
1645+
alert('Salut !')
1646+
}
1647+
}
1648+
})
1649+
</script>
1650+
{% endraw %}
1651+
1652+
Utiliser `$emit` avec des arguments additionnels :
1653+
1654+
```js
1655+
Vue.component('magic-eight-ball', {
1656+
data: function () {
1657+
return {
1658+
possibleAdvice: ['Oui', 'Non', 'Peut-être']
1659+
}
1660+
},
1661+
methods: {
1662+
giveAdvice: function () {
1663+
var randomAdviceIndex = Math.floor(Math.random() * this.possibleAdvice.length)
1664+
this.$emit('give-advice', this.possibleAdvice[randomAdviceIndex])
1665+
}
1666+
},
1667+
template: `
1668+
<button v-on:click="giveAdvice">
1669+
Cliquez-moi pour un indice
1670+
</button>
1671+
`
1672+
})
1673+
```
1674+
1675+
```html
1676+
<div id="emit-example-argument">
1677+
<magic-eight-ball v-on:give-advice="showAdvice"></magic-eight-ball>
1678+
</div>
1679+
```
1680+
1681+
```js
1682+
new Vue({
1683+
el: '#emit-example-argument',
1684+
methods: {
1685+
showAdvice: function (advice) {
1686+
alert(advice)
1687+
}
1688+
}
1689+
})
1690+
```
1691+
1692+
{% raw %}
1693+
<div id="emit-example-argument" class="demo">
1694+
<magic-eight-ball v-on:give-advice="showAdvice"></magic-eight-ball>
1695+
</div>
1696+
<script>
1697+
Vue.component('magic-eight-ball', {
1698+
data: function () {
1699+
return {
1700+
possibleAdvice: ['Oui', 'Non', 'Peut-être']
1701+
}
1702+
},
1703+
methods: {
1704+
giveAdvice: function () {
1705+
var randomAdviceIndex = Math.floor(Math.random() * this.possibleAdvice.length)
1706+
this.$emit('give-advice', this.possibleAdvice[randomAdviceIndex])
1707+
}
1708+
},
1709+
template: `
1710+
<button v-on:click="giveAdvice">
1711+
Cliquez-moi pour un indice
1712+
</button>
1713+
`
1714+
})
1715+
new Vue({
1716+
el: '#emit-example-argument',
1717+
methods: {
1718+
showAdvice: function (advice) {
1719+
alert(advice)
1720+
}
1721+
}
1722+
})
1723+
</script>
1724+
{% endraw %}
1725+
16011726
## Méthodes d'Instance / Cycle de Vie
16021727
16031728
### vm.$mount( [élémentOuSelecteur] )

src/v2/cookbook/avoiding-memory-leaks.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ order: 10
55
---
66
## Introduction
77

8-
If you are developing applications with Vue, then you need to watch out for memory leaks. This issue is especially important in Single Page Applications (SPAs) because by design, users should not have to refresh their browser when using an SPA, so it is up to the Javascript application to clean up components and make sure that garbage collection takes place as expected.
8+
<p>Cette page est en cours de traduction. Pour nous aider, vous pouvez participer sur <a href="https://github.com/vuejs-fr/vuejs.org" target="_blank">le dépôt GitHub dédié de Vuejs-FR</a>.</p><p>If you are developing applications with Vue, then you need to watch out for memory leaks. This issue is especially important in Single Page Applications (SPAs) because by design, users should not have to refresh their browser when using an SPA, so it is up to the JavaScript application to clean up components and make sure that garbage collection takes place as expected.</p>
99

1010
Memory leaks in Vue applications do not typically come from Vue itself, rather they can happen when incorporating other libraries into an application.
1111

@@ -166,4 +166,4 @@ deactivated: function () {
166166

167167
## Wrapping Up
168168

169-
Vue makes it very easy to develop amazing, reactive Javascript applications, but you still need to be careful about memory leaks. These leaks will often occur when using additional 3rd Party libraries that manipulate the DOM outside of Vue. Make sure to test your application for memory leaks and take appropriate steps to clean up components where necessary.
169+
Vue makes it very easy to develop amazing, reactive JavaScript applications, but you still need to be careful about memory leaks. These leaks will often occur when using additional 3rd Party libraries that manipulate the DOM outside of Vue. Make sure to test your application for memory leaks and take appropriate steps to clean up components where necessary.
+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
title: Client-Side Storage (EN)
3+
type: cookbook
4+
order: 11
5+
---
6+
7+
## Base Example
8+
9+
<p>Cette page est en cours de traduction. Pour nous aider, vous pouvez participer sur <a href="https://github.com/vuejs-fr/vuejs.org" target="_blank">le dépôt GitHub dédié de Vuejs-FR</a>.</p><p>Client-side storage is an excellent way to quickly add performance gains to an application. By storing data on the browser itself, you can skip fetching information from the server every time the user needs it. While especially useful when offline, even online users will benefit from using data locally versus a remote server. Client-side storage can be done with [cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies), [Local Storage](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API) (technically "Web Storage"), [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API), and [WebSQL](https://www.w3.org/TR/webdatabase/) (a deprecated method that should not be used in new projects).</p>
10+
11+
In this cookbook entry we'll focus on Local Storage, the simplest of the storage mechanisms. Local Storage uses a key/value system for storing data. It is limited to storing only simple values but complex data can be stored if you are willing to encode and decode the values with JSON. In general, Local Storage is appropriate for smaller sets of data you would want to persist, things like user preferences or form data. Larger data with more complex storage needs would be better stored typically in IndexedDB.
12+
13+
Let's begin with a simple form based example:
14+
15+
``` html
16+
<div id="app">
17+
My name is <input v-model="name">
18+
</div>
19+
```
20+
21+
This example has one form field bound to a Vue value called `name`. Here's the JavaScript:
22+
23+
``` js
24+
const app = new Vue({
25+
el:'#app',
26+
data: {
27+
name:''
28+
},
29+
mounted() {
30+
if(localStorage.name) this.name = localStorage.name;
31+
},
32+
watch: {
33+
name(newName) {
34+
localStorage.name = newName;
35+
}
36+
}
37+
});
38+
```
39+
40+
Focus on the `mounted` and `watch` parts. We use `mounted` to handle loading the value from localStorage. To handle writing the data base, we watch the `name` value and on change, immediately write it.
41+
42+
You can run this yourself here:
43+
44+
<p data-height="265" data-theme-id="0" data-slug-hash="KodaKb" data-default-tab="js,result" data-user="cfjedimaster" data-embed-version="2" data-pen-title="testing localstorage" class="codepen">See the Pen <a href="https://codepen.io/cfjedimaster/pen/KodaKb/">testing localstorage</a> by Raymond Camden (<a href="https://codepen.io/cfjedimaster">@cfjedimaster</a>) on <a href="https://codepen.io">CodePen</a>.</p>
45+
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
46+
47+
Type something in the form and then reload this page. You'll note that the value you typed previously will show up automatically. Don't forget that your browser provides excellent developer tools for inspecting client-side storage. Here's an example in Firefox:
48+
49+
![Storage devtools in Firefox](/images/devtools-storage.png)
50+
51+
And here it is in Chrome:
52+
53+
![Storage devtools in Chrome](/images/devtools-storage-chrome.png)
54+
55+
And then finally, an example in Microsoft Edge. Note that you can find application storage values under the Debugger tab.
56+
57+
![Storage devtools in Edge](/images/devtools-storage-edge.png)
58+
59+
<p class="tip">As a quick aside, these dev tools also offer you a way to remove storage values. This can be very useful when testing.</p>
60+
61+
Immediately writing the value may not advisable. Let's consider a slightly more advanced example. First, the updated form.
62+
63+
``` html
64+
<div id="app">
65+
My name is <input v-model="name">
66+
and I am <input v-model="age"> years old.
67+
<p/>
68+
<button @click="persist">Save</button>
69+
</div>
70+
```
71+
72+
Now we've got two fields (again, bound to a Vue instance) but now there is the addition of a button that runs a `persist` method. Let's look at the JavaScript.
73+
74+
``` js
75+
const app = new Vue({
76+
el:'#app',
77+
data: {
78+
name:'',
79+
age:0
80+
},
81+
mounted() {
82+
if(localStorage.name) this.name = localStorage.name;
83+
if(localStorage.age) this.age = localStorage.age;
84+
},
85+
methods: {
86+
persist() {
87+
localStorage.name = this.name;
88+
localStorage.age = this.age;
89+
console.log('now pretend I did more stuff...');
90+
}
91+
}
92+
})
93+
```
94+
95+
As before, `mounted` is used to load persisted data, if it exists. This time, though, data is only persisted when the button is clicked. We could also do any validations or transformations here before storing the value. You could also store a date representing when the values were stored. With that metadata, the `mounted` method could make a logical call on whether or not to store the values again, such as in this version below. You can try this version below.
96+
97+
<p data-height="265" data-theme-id="0" data-slug-hash="rdOjLN" data-default-tab="js,result" data-user="cfjedimaster" data-embed-version="2" data-pen-title="testing localstorage 2" class="codepen">See the Pen <a href="https://codepen.io/cfjedimaster/pen/rdOjLN/">testing localstorage 2</a> by Raymond Camden (<a href="https://codepen.io/cfjedimaster">@cfjedimaster</a>) on <a href="https://codepen.io">CodePen</a>.</p>
98+
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
99+
100+
## Working with Complex Values
101+
102+
As mentioned above, Local Storage only works with simple values. To store more complex values, like objects or arrays, you must serialize and deserialize the values with JSON. Here is a more advanced example that persists an array of cats (the best kind of array possible).
103+
104+
``` html
105+
<div id="app">
106+
<h2>Cats</h2>
107+
<div v-for="(cat,n) in cats">
108+
<p>
109+
<span class="cat">{{cat}}</span> <button @click="removeCat(n)">Remove</button>
110+
</p>
111+
</div>
112+
113+
<p>
114+
<input v-model="newCat">
115+
<button @click="addCat">Add Cat</button>
116+
</p>
117+
118+
</div>
119+
```
120+
121+
This "app" consists of a simple list on top (with a button to remove a cat) and a small form at the bottom to add a new cat. Now let's look at the JavaScript.
122+
123+
``` js
124+
const app = new Vue({
125+
el:'#app',
126+
data: {
127+
cats:[],
128+
newCat:null
129+
},
130+
mounted() {
131+
132+
if(localStorage.getItem('cats')) {
133+
try {
134+
this.cats = JSON.parse(localStorage.getItem('cats'));
135+
} catch(e) {
136+
localStorage.removeItem('cats');
137+
}
138+
}
139+
},
140+
methods: {
141+
addCat() {
142+
// ensure they actually typed something
143+
if(!this.newCat) return;
144+
this.cats.push(this.newCat);
145+
this.newCat = '';
146+
this.saveCats();
147+
},
148+
removeCat(x) {
149+
this.cats.splice(x,1);
150+
this.saveCats();
151+
},
152+
saveCats() {
153+
let parsed = JSON.stringify(this.cats);
154+
localStorage.setItem('cats', parsed);
155+
}
156+
}
157+
})
158+
```
159+
160+
In this application, we've switched to use the Local Storage APIs versus "direct" access. Both work but the API method is generally preferred. `mounted` now has to grab the value and parse the JSON value. If anything goes wrong here we assume the data is corrupt and delete it. (Remember, any time your web application uses client-side storage, the user has access to it and can modify it at will.)
161+
162+
We have three methods now to handle working with cat. Both `addCat` and `removeCat` handle updating the "live" Vue data stored in `this.cats`. They then run `saveCats` which handles serializing and persisting the data. You can play with this version below:
163+
164+
<p data-height="265" data-theme-id="0" data-slug-hash="qoYbyW" data-default-tab="js,result" data-user="cfjedimaster" data-embed-version="2" data-pen-title="localstorage, complex" class="codepen">See the Pen <a href="https://codepen.io/cfjedimaster/pen/qoYbyW/">localstorage, complex</a> by Raymond Camden (<a href="https://codepen.io/cfjedimaster">@cfjedimaster</a>) on <a href="https://codepen.io">CodePen</a>.</p>
165+
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
166+
167+
## Alternative Patterns
168+
169+
While the Local Storage API is relatively simple, it is missing some basic features that would be useful in many applications. The following plugins wrap Local Storage access and make it easier to use, while also adding functionality like default values.
170+
171+
* [vue-local-storage](https://github.com/pinguinjkeke/vue-local-storage)
172+
* [vue-reactive-storage](https://github.com/ropbla9/vue-reactive-storage)
173+
174+
## Wrapping Up
175+
176+
While the browser will never replace a server persistence system, having multiple ways to cache data locally can be a huge performance boost for your application, and working with it in Vue.js makes it even more powerful.

src/v2/cookbook/creating-custom-scroll-directives.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ new Vue({
5050

5151
We'd also need a style property that will transition the intermediary values here, in this case:
5252

53-
```
53+
```css
5454
.box {
5555
transition: 1.5s all cubic-bezier(0.39, 0.575, 0.565, 1);
5656
}

0 commit comments

Comments
 (0)