forked from vuejs/v2.vuejs.org
-
Notifications
You must be signed in to change notification settings - Fork 103
[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
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 20e461a
Fixed a typo (#1625)
aabounegm aa3a92e
fix a typo (#1627)
1bf4628
Update v-for list example (#1628)
chipit24 f247d17
fix: code snippet language (close #1617) (#1626)
hmatalonga 0336b80
Update todo-list-example for a11y (#1630)
chipit24 7c12895
change NODE_ENV to follow recommendations from webpack (#1622)
parkourben99 9244cfc
note that modes are not available in transition groups, fixes #1619
chrisvfritz 19733d5
add examples for in API doc
chrisvfritz e959d38
add comparison note about react-like libraries
chrisvfritz 418d7f3
Update components.md (#1632)
artiomgiza 28ad8a1
cookbook entry for storage (#1550)
cfjedimaster a78885d
Update index.md (#1634)
Alex-Sokolov 44d73a1
docs(guide/comparison): correct size stat of Angular CLI project (#1638)
trotyl 3d1fdd8
Update client-side-storage.md (#1640)
Alex-Sokolov 5bd7be0
Update creating-custom-scroll-directives.md (#1639)
Alex-Sokolov ef9b64e
chore: update ad code
yyx990803 bf5a331
remove unnecessary word 'know' (#1641)
pipopotamasu 9e977cd
Updated broken links for the Vue Test Utils documentation. (#1643)
eee7fcd
Reorganize component props to introduce prop types earlier, fixes #16…
chrisvfritz dc9b6cb
First stab at SFC to npm documentation (#1558)
mgdodge 256c51b
Rename packaing-sfc-for-npm.md to packaging-sfc-for-npm.md (#1652)
Alex-Sokolov c2be312
Update link in comparison guide (#1653)
flaviocopes 8e08656
chore: update vue version
yyx990803 b025983
Fix link to docs on custom inputs (#1660)
alastairs 055629e
correct wrong link (#1661)
byog f3d9557
Feature/vuemeetups (#1665)
bencodezen 7cb981c
Added details about Lifecycle Hooks (#1664)
Knogobert ec38b56
improve comparisons react scaling down section
chrisvfritz fda94a9
add text versions of image code examples
chrisvfritz b0d792b
remove extra comma in components
chrisvfritz cffbc40
TypeScript project generation in @vue/cli 3.0 (#1668)
k3a 4a734c2
chore: update sponsors
yyx990803 40e5066
Merge remote-tracking branch 'upstream/master' into june-2018
MachinisteWeb 8d7aec7
Merge branch 'master' into june-2018
MachinisteWeb 9e3d2c2
Sylvain and Forresst feedbacks
MachinisteWeb 1bfac64
@forrestt and @RaisonBlue feedback
MachinisteWeb 933dfb4
Feedback of @rspt
MachinisteWeb 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -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 | ||
</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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cliquez-moi pour être
|
||
</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] ) | ||
|
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,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: | ||
|
||
 | ||
|
||
And here it is in Chrome: | ||
|
||
 | ||
|
||
And then finally, an example in Microsoft Edge. Note that you can find application storage values under the Debugger tab. | ||
|
||
 | ||
|
||
<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. |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cliquez-moi pour être
accueillisalué