feature: Add inject(key, value) as 2nd argument of plugins

This commit is contained in:
Sébastien Chopin 2017-08-24 20:38:28 +02:00
parent 42f2a59fd2
commit e32a3755dd

View File

@ -107,11 +107,36 @@ async function createApp (ssrContext) {
beforeRenderFns: ssrContext ? ssrContext.beforeRenderFns : undefined
}, app)
const inject = function (key, value) {
if (!key) throw new Error('inject(key, value) has no key provided')
if (!value) throw new Error('inject(key, value) has no value provided')
key = '$' + key
// Add into app
app[key] = value
// Add into vm
Vue.use(() => {
const installKey = '__nuxt_' + key + '_installed__'
if (Vue[installKey]) return
Vue[installKey] = true
if (!Vue.prototype.hasOwnProperty(key)) {
Object.defineProperty(Vue.prototype, key, {
get () {
return this.$root.$options[key]
}
})
}
})
<% if (store) { %>
// Add into store
store[key] = app[key]
<% } %>
}
<% plugins.filter(p => p.ssr).forEach(plugin => { %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(ctx)<% }) %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(ctx, inject)<% }) %>
<% if (plugins.filter(p => !p.ssr).length) { %>
if (process.browser) { <% plugins.filter(p => !p.ssr).forEach(plugin => { %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(ctx)<% }) %>
if (typeof <%= plugin.name %> === 'function') await <%= plugin.name %>(ctx, inject)<% }) %>
}<% } %>
<% if (store) { %>