You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Nuxt.js allows you to define JavaScript plugins to be run before instantiating the root vue.js application. This is especially helpful when using your own libraries or external modules.
3
+
description: Nuxt.js vous permet de définir les plugins JavaScript à exécuter avant d'instancier l'application Vue.js racine. Cela est particulièrement pratique quand vous utilisez vos propres bibliothèques ou modules externes.
4
4
---
5
5
6
-
> Nuxt.js allows you to define JavaScript plugins to be run before instantiating the root vue.js application. This is especially helpful when using your own libraries or external modules.
6
+
> Nuxt.js vous permet de définir les plugins JavaScript à exécuter avant d'instancier l'application Vue.js racine. Cela est particulièrement pratique quand vous utilisez vos propres bibliothèques ou modules externes.
7
7
8
-
<divclass="Alert">It is important to know that in any Vue [instance lifecycle](https://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram), only `beforeCreate` and `created` hooks are called **both from client-side and server-side**. All other hooks are called only from the client-side.</div>
8
+
<divclass="Alert">Il est important de savoir que, dans le [cycle de vie d'une instance de Vue](https://fr.vuejs.org/v2/guide/instance.html#Diagramme-du-cycle-de-vie), les hooks `beforeCreate` et `created` sont appelés **à la fois du côté client et du côté serveur**. Tous les autres *hooks* ne sont appelés que depuis le client.</div>
9
9
10
-
## External Packages
10
+
## Modules externes
11
11
12
-
We may want to use external packages/modules in our application, one great example is[axios](https://github.com/mzabriskie/axios)for making HTTP request for both server and client.
12
+
Nous souhaitons utiliser des packages / modules externes dans notre application, un excellent exemple est[axios](https://github.com/mzabriskie/axios)pour les requêtes HTTP depuis le serveur et le client.
13
13
14
-
We install it via npm:
14
+
Nous l'installons via npm:
15
15
16
16
```bash
17
17
npm install --save axios
18
18
```
19
19
20
-
Then, we can use it directly in our pages:
20
+
Puis nous pouvons l'utiliser directement dans nos pages:
21
21
22
22
```html
23
23
<template>
@@ -28,15 +28,15 @@ Then, we can use it directly in our pages:
28
28
importaxiosfrom'axios'
29
29
30
30
exportdefault {
31
-
asyncasyncData ({ params }) {
31
+
asyncdata ({ params }) {
32
32
let { data } =awaitaxios.get(`https://my-api/posts/${params.id}`)
33
33
return { title:data.title }
34
34
}
35
35
}
36
36
</script>
37
37
```
38
38
39
-
But there is **one problem here**, if we import axios in another page, it will be included again for the page bundle. We want to include `axios`only once in our application, for this, we use the `build.vendor`key in our `nuxt.config.js`:
39
+
Mais il y a **un problème**, si nous importons axios dans une autre page, il sera à nouveau inclus dans le paquetage de la page. Nous voulons inclure `axios`une seule fois dans notre application, pour cela, nous utilisons la clé `build.vendor`dans notre `nuxt.config.js`:
40
40
41
41
```js
42
42
module.exports= {
@@ -46,32 +46,32 @@ module.exports = {
46
46
}
47
47
```
48
48
49
-
Then, I can import`axios`anywhere without having to worry about making the bundle bigger!
49
+
Je peux ensuite importer`axios`partout sans avoir à m'inquiéter de l'importer plusieurs fois et de rendre le paquetage plus lourd.
50
50
51
-
## Vue Plugins
51
+
## Plugins Vue
52
52
53
-
If we want to use [vue-notifications](https://github.com/se-panfilov/vue-notifications)to display notification in our application, we need to setup the plugin before launching the app.
53
+
Si nous voulons utiliser [vue-notifications](https://github.com/se-panfilov/vue-notifications)pour afficher des notifications dans notre application, nous devons configurer le plugin avant de lancer l'application.
54
54
55
-
File`plugins/vue-notifications.js`:
55
+
Dans`plugins/vue-notifications.js`:
56
56
```js
57
57
importVuefrom'vue'
58
58
importVueNotificationsfrom'vue-notifications'
59
59
60
60
Vue.use(VueNotifications)
61
61
```
62
62
63
-
Then, we add the file inside the`plugins`key of `nuxt.config.js`:
63
+
Puis nous ajoutons le fichier dans l'attribut`plugins`de `nuxt.config.js`:
64
64
```js
65
65
module.exports= {
66
-
plugins: ['~/plugins/vue-notifications']
66
+
plugins: ['~plugins/vue-notifications']
67
67
}
68
68
```
69
69
70
-
To learn more about the `plugins` configuration key, check out the [plugins api](/api/configuration-plugins).
70
+
Pour en savoir plus sur l'attribut `plugins`, consultez [La propriété `plugins`](/api/configuration-plugins) de l'API.
71
71
72
-
Actually, `vue-notifications`will be included in the app bundle, but because it's a library, we want to include it in the vendor bundle for better caching.
72
+
Acutellement, `vue-notifications`sera inclus dans le paquetage de l'application. Mais comme il s'agit d'une bibliothèque, nous voulons l'inclure dans le paquetage `vendor` pour une meilleure mise en cache.
73
73
74
-
We can update our `nuxt.config.js`to add`vue-notifications`in the vendor bundle:
74
+
Nous pouvons mettre à jour `nuxt.config.js`pour ajouter`vue-notifications`dans le bundle `vendor`:
75
75
```js
76
76
module.exports= {
77
77
build: {
@@ -81,9 +81,9 @@ module.exports = {
81
81
}
82
82
```
83
83
84
-
## Inject in $root & context
84
+
## Injection dans $root et context
85
85
86
-
Some plugins need to be injected in the App root to be used, like[vue-18n](https://github.com/kazupon/vue-i18n). Nuxt.js gives you the possibility to export a function in your plugin to receives the root component but also the context.
86
+
Plusieurs plugins ont besoin d'être injectés à la racine de l'application pour être utilisés, comme[vue-18n](https://github.com/kazupon/vue-i18n). Nuxt.js vous donne la possibilité d'exporter une fonction dans votre plugin pour recevoir l'instance racine ainsi que le contexte.
87
87
88
88
`plugins/i18n.js`:
89
89
```js
@@ -93,10 +93,10 @@ import VueI18n from 'vue-i18n'
93
93
Vue.use(VueI18n)
94
94
95
95
exportdefault ({ app, store }) => {
96
-
//Set i18n instance on app
97
-
//This way we can use it in middleware and pages asyncData & fetch
96
+
//Appliquer l'instance i18n de l'application
97
+
//De cette façon nous pouvons l'utiliser en tant que middleware et pages asyncData & fetch
98
98
app.i18n=newVueI18n({
99
-
/* vue-i18n options... */
99
+
/* options vue-i18n... */
100
100
})
101
101
}
102
102
```
@@ -111,13 +111,13 @@ module.exports = {
111
111
}
112
112
```
113
113
114
-
Please take a look at the [i18n example](/examples/i18n) to see how we use it.
114
+
Pour voir comment utiliser ce plugin, consultez cet [exemple i18n](/examples/i18n).
115
115
116
-
## Client-side only
116
+
## Côté client uniquement
117
117
118
-
Some plugins might work **only for the browser**, you can use the`ssr: false`option in `plugins`to run the file only on the client-side.
118
+
Certains plugins fonctionnent **uniquement dans un navigateur**. Vous pouvez utiliser l'option`ssr: false`dans `plugins`pour exécuter le fichier uniquement côté client.
119
119
120
-
Example:
120
+
Exemple :
121
121
122
122
`nuxt.config.js`:
123
123
```js
@@ -136,6 +136,6 @@ import VueNotifications from 'vue-notifications'
136
136
Vue.use(VueNotifications)
137
137
```
138
138
139
-
In case you need to require some libraries only for the server, you can use the `process.server`variable set to `true`when webpack is creating the `server.bundle.js` file.
139
+
Dans le cas où vous devez importer certaines bibliothèques uniquement pour le serveur, vous pouvez utiliser la variable `process.server`définie sur `true`lorsque le serveur web crée le fichier `server.bundle.js`.
140
140
141
-
Also, if you need to know if you are inside a generated app (via `nuxt generate`), you can check `process.static`, set to`true`during generation and after. To know the state when a page is being server-rendered by `nuxt generate`before being saved, you can use`process.static && process.server`.
141
+
Si vous avez besoin également de savoir si vous êtes dans une application générée (via `nuxt generate`), vous pouvez vérifier la propriété `process.static` mise à`true`pendant la génération et après. Pour connaitre dans quel état est une page qui est en train d'être rendue par `nuxt generate`avant d'être sauvée, vous pouvez utilisez`process.static && process.server`.
0 commit comments