Skip to content

[Mise à jour] — Juin 2018 #137

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 38 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
91a46ed
Update unit-testing-vue-components.md (#1623)
38elements May 11, 2018
20e461a
Fixed a typo (#1625)
aabounegm May 13, 2018
aa3a92e
fix a typo (#1627)
May 14, 2018
1bf4628
Update v-for list example (#1628)
chipit24 May 14, 2018
f247d17
fix: code snippet language (close #1617) (#1626)
hmatalonga May 14, 2018
0336b80
Update todo-list-example for a11y (#1630)
chipit24 May 17, 2018
7c12895
change NODE_ENV to follow recommendations from webpack (#1622)
parkourben99 May 17, 2018
9244cfc
note that modes are not available in transition groups, fixes #1619
chrisvfritz May 17, 2018
19733d5
add examples for in API doc
chrisvfritz May 17, 2018
e959d38
add comparison note about react-like libraries
chrisvfritz May 17, 2018
418d7f3
Update components.md (#1632)
artiomgiza May 17, 2018
28ad8a1
cookbook entry for storage (#1550)
cfjedimaster May 18, 2018
a78885d
Update index.md (#1634)
Alex-Sokolov May 18, 2018
44d73a1
docs(guide/comparison): correct size stat of Angular CLI project (#1638)
trotyl May 21, 2018
3d1fdd8
Update client-side-storage.md (#1640)
Alex-Sokolov May 21, 2018
5bd7be0
Update creating-custom-scroll-directives.md (#1639)
Alex-Sokolov May 21, 2018
ef9b64e
chore: update ad code
yyx990803 May 22, 2018
bf5a331
remove unnecessary word 'know' (#1641)
pipopotamasu May 22, 2018
9e977cd
Updated broken links for the Vue Test Utils documentation. (#1643)
May 22, 2018
eee7fcd
Reorganize component props to introduce prop types earlier, fixes #16…
chrisvfritz May 25, 2018
dc9b6cb
First stab at SFC to npm documentation (#1558)
mgdodge May 25, 2018
256c51b
Rename packaing-sfc-for-npm.md to packaging-sfc-for-npm.md (#1652)
Alex-Sokolov May 25, 2018
c2be312
Update link in comparison guide (#1653)
flaviocopes May 28, 2018
8e08656
chore: update vue version
yyx990803 Jun 1, 2018
b025983
Fix link to docs on custom inputs (#1660)
alastairs Jun 3, 2018
055629e
correct wrong link (#1661)
byog Jun 3, 2018
f3d9557
Feature/vuemeetups (#1665)
bencodezen Jun 3, 2018
7cb981c
Added details about Lifecycle Hooks (#1664)
Knogobert Jun 3, 2018
ec38b56
improve comparisons react scaling down section
chrisvfritz Jun 3, 2018
fda94a9
add text versions of image code examples
chrisvfritz Jun 3, 2018
b0d792b
remove extra comma in components
chrisvfritz Jun 3, 2018
cffbc40
TypeScript project generation in @vue/cli 3.0 (#1668)
k3a Jun 3, 2018
4a734c2
chore: update sponsors
yyx990803 Jun 4, 2018
40e5066
Merge remote-tracking branch 'upstream/master' into june-2018
MachinisteWeb Jun 10, 2018
8d7aec7
Merge branch 'master' into june-2018
MachinisteWeb Jun 10, 2018
9e3d2c2
Sylvain and Forresst feedbacks
MachinisteWeb Jun 11, 2018
1bfac64
@forrestt and @RaisonBlue feedback
MachinisteWeb Jun 12, 2018
933dfb4
Feedback of @rspt
MachinisteWeb Jun 12, 2018
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
Binary file added src/images/devtools-storage-chrome.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/devtools-storage-edge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/devtools-storage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 127 additions & 2 deletions src/v2/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1590,14 +1590,139 @@ type: api

- 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.

### vm.$emit( event, [...args] )
### vm.$emit( eventName, [...args] )

- **Arguments :**
- `{string} évènement`
- `{string} nom d'évènement`
- `[...arguments]`

Déclenche un évènement sur l'instance actuelle. Tous les arguments additionnels sont passés à la fonction de rappel de l'écouteur.

- **Exemples:**

Utiliser `$emit` avec un nom d'évènement :

```js
Vue.component('welcome-button', {
template: `
<button v-on:click="$emit('welcome')">
Cliquez-moi pour être accueilli

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cliquez-moi pour être accueilli salué

Cela me parait plus en adéquation avec le code

</button>
`
})
```
```html
<div id="emit-example-simple">
<welcome-button v-on:welcome="sayHi"></welcome-button>
</div>
```
```js
new Vue({
el: '#emit-example-simple',
methods: {
sayHi: function () {
alert('Salut !')
}
}
})
```
{% raw %}
<div id="emit-example-simple" class="demo">
<welcome-button v-on:welcome="sayHi"></welcome-button>
</div>
<script>
Vue.component('welcome-button', {
template: `
<button v-on:click="$emit('welcome')">
Cliquez-moi pour être accueilli

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cliquez-moi pour être accueilli salué

Cela me parait plus en adéquation avec le code

</button>
`
})
new Vue({
el: '#emit-example-simple',
methods: {
sayHi: function () {
alert('Salut !')
}
}
})
</script>
{% endraw %}

Utiliser `$emit` avec des arguments additionnels :

```js
Vue.component('magic-eight-ball', {
data: function () {
return {
possibleAdvice: ['Oui', 'Non', 'Peut-être']
}
},
methods: {
giveAdvice: function () {
var randomAdviceIndex = Math.floor(Math.random() * this.possibleAdvice.length)
this.$emit('give-advice', this.possibleAdvice[randomAdviceIndex])
}
},
template: `
<button v-on:click="giveAdvice">
Cliquez-moi pour un indice
</button>
`
})
```

```html
<div id="emit-example-argument">
<magic-eight-ball v-on:give-advice="showAdvice"></magic-eight-ball>
</div>
```

```js
new Vue({
el: '#emit-example-argument',
methods: {
showAdvice: function (advice) {
alert(advice)
}
}
})
```

{% raw %}
<div id="emit-example-argument" class="demo">
<magic-eight-ball v-on:give-advice="showAdvice"></magic-eight-ball>
</div>
<script>
Vue.component('magic-eight-ball', {
data: function () {
return {
possibleAdvice: ['Oui', 'Non', 'Peut-être']
}
},
methods: {
giveAdvice: function () {
var randomAdviceIndex = Math.floor(Math.random() * this.possibleAdvice.length)
this.$emit('give-advice', this.possibleAdvice[randomAdviceIndex])
}
},
template: `
<button v-on:click="giveAdvice">
Cliquez-moi pour un indice
</button>
`
})
new Vue({
el: '#emit-example-argument',
methods: {
showAdvice: function (advice) {
alert(advice)
}
}
})
</script>
{% endraw %}

## Méthodes d'Instance / Cycle de Vie

### vm.$mount( [élémentOuSelecteur] )
Expand Down
4 changes: 2 additions & 2 deletions src/v2/cookbook/avoiding-memory-leaks.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ order: 10
---
## Introduction

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>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>

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

Expand Down Expand Up @@ -166,4 +166,4 @@ deactivated: function () {

## Wrapping Up

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.
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 changes: 176 additions & 0 deletions src/v2/cookbook/client-side-storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
---
title: Client-Side Storage (EN)
type: cookbook
order: 11
---

## Base Example

<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>

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.

Let's begin with a simple form based example:

``` html
<div id="app">
My name is <input v-model="name">
</div>
```

This example has one form field bound to a Vue value called `name`. Here's the JavaScript:

``` js
const app = new Vue({
el:'#app',
data: {
name:''
},
mounted() {
if(localStorage.name) this.name = localStorage.name;
},
watch: {
name(newName) {
localStorage.name = newName;
}
}
});
```

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.

You can run this yourself here:

<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>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>

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:

![Storage devtools in Firefox](/images/devtools-storage.png)

And here it is in Chrome:

![Storage devtools in Chrome](/images/devtools-storage-chrome.png)

And then finally, an example in Microsoft Edge. Note that you can find application storage values under the Debugger tab.

![Storage devtools in Edge](/images/devtools-storage-edge.png)

<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>

Immediately writing the value may not advisable. Let's consider a slightly more advanced example. First, the updated form.

``` html
<div id="app">
My name is <input v-model="name">
and I am <input v-model="age"> years old.
<p/>
<button @click="persist">Save</button>
</div>
```

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.

``` js
const app = new Vue({
el:'#app',
data: {
name:'',
age:0
},
mounted() {
if(localStorage.name) this.name = localStorage.name;
if(localStorage.age) this.age = localStorage.age;
},
methods: {
persist() {
localStorage.name = this.name;
localStorage.age = this.age;
console.log('now pretend I did more stuff...');
}
}
})
```

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.

<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>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>

## Working with Complex Values

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).

``` html
<div id="app">
<h2>Cats</h2>
<div v-for="(cat,n) in cats">
<p>
<span class="cat">{{cat}}</span> <button @click="removeCat(n)">Remove</button>
</p>
</div>

<p>
<input v-model="newCat">
<button @click="addCat">Add Cat</button>
</p>

</div>
```

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.

``` js
const app = new Vue({
el:'#app',
data: {
cats:[],
newCat:null
},
mounted() {

if(localStorage.getItem('cats')) {
try {
this.cats = JSON.parse(localStorage.getItem('cats'));
} catch(e) {
localStorage.removeItem('cats');
}
}
},
methods: {
addCat() {
// ensure they actually typed something
if(!this.newCat) return;
this.cats.push(this.newCat);
this.newCat = '';
this.saveCats();
},
removeCat(x) {
this.cats.splice(x,1);
this.saveCats();
},
saveCats() {
let parsed = JSON.stringify(this.cats);
localStorage.setItem('cats', parsed);
}
}
})
```

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.)

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:

<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>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>

## Alternative Patterns

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.

* [vue-local-storage](https://github.com/pinguinjkeke/vue-local-storage)
* [vue-reactive-storage](https://github.com/ropbla9/vue-reactive-storage)

## Wrapping Up

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.
2 changes: 1 addition & 1 deletion src/v2/cookbook/creating-custom-scroll-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ new Vue({

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

```
```css
.box {
transition: 1.5s all cubic-bezier(0.39, 0.575, 0.565, 1);
}
Expand Down
Loading