mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-04 03:17:14 +00:00
Use omit and add example
This commit is contained in:
parent
c9250b4d07
commit
06edc8029b
@ -73,3 +73,19 @@ export default {
|
|||||||
## Context
|
## Context
|
||||||
|
|
||||||
To see the list of available keys in `context`, take a look at [this documentation](https://github.com/nuxt/nuxt.js/tree/master/examples/async-data#context).
|
To see the list of available keys in `context`, take a look at [this documentation](https://github.com/nuxt/nuxt.js/tree/master/examples/async-data#context).
|
||||||
|
|
||||||
|
## Action `nuxtServerInit`
|
||||||
|
|
||||||
|
If you define the action `nuxtServerInit` in your store, nuxt.js will call it with the context, it can be useful when having some data on the server you want to give to the client-side, for example, the authenticated user:
|
||||||
|
```js
|
||||||
|
// store/index.js
|
||||||
|
actions: {
|
||||||
|
nuxtServerInit ({ commit }, { req }) {
|
||||||
|
if (req.authUser) {
|
||||||
|
commit('user', req.authUser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The context given to `nuxtServerInit` is the same as the `data` of `fetch` method except `context.redirect()` and `context.error()` are omitted.
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
const debug = require('debug')('nuxt:render')
|
const debug = require('debug')('nuxt:render')
|
||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import { stringify } from 'querystring'
|
import { stringify } from 'querystring'
|
||||||
import { pick } from 'lodash'
|
import { omit } from 'lodash'
|
||||||
import { app, router<%= (store ? ', store' : '') %> } from './index'
|
import { app, router<%= (store ? ', store' : '') %> } from './index'
|
||||||
import { getMatchedComponents, getContext, urlJoin } from './utils'
|
import { getMatchedComponents, getContext, urlJoin } from './utils'
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ export default context => {
|
|||||||
|
|
||||||
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
||||||
<% if (store) { %>
|
<% if (store) { %>
|
||||||
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', pick(getContext(context), 'req', 'res', 'params', 'query')) : null)
|
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', omit(getContext(context), 'redirect', 'error')) : null)
|
||||||
if (!(promise instanceof Promise)) promise = Promise.resolve()
|
if (!(promise instanceof Promise)) promise = Promise.resolve()
|
||||||
<% } else { %>
|
<% } else { %>
|
||||||
let promise = Promise.resolve()
|
let promise = Promise.resolve()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nuxt",
|
"name": "nuxt",
|
||||||
"version": "0.4.5",
|
"version": "0.4.6",
|
||||||
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
|
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
Loading…
Reference in New Issue
Block a user