nuxt-stash

0.2.0 • Public • Published

nuxt-stash

A Nuxt.js module that makes it easy to push server-generated context object to the Vue app and make it available to all Vue components.

The typical use is to allow Vue components to access currently logged user, or some global settings that are pulled from the database, without having to bother setting up Vuex with its actions and mutators.

This module was heavily inspired by (and borrowed the naming from) vue-stash.

Installation

Install package:

yarn add nuxt-stash

Usage

Add module to nuxt.config.js:

module.exports = {
    modules: [
        'nuxt-stash',
    ],
}

Create stash.js or stash/index.js in your Nuxt application source directory:

export default async function createStash (ctx) {
    // ctx here is Nuxt.js context (https://nuxtjs.org/api/context/)
    return {
        foo: 'bar',
        user: await User.findById(ctx.req.session.userId),
    }
}

Keep in mind that the function will be called only during server-side rendering. The data will then be passed to client-side automatically.

During client-side rendering, the module will not be even imported. Therefore, it's safe to import server-side related stuff from there, it will not get webpacked into client-side build.

Acessing stash

In your Vue components, access stash directly from the template:

<template>
<div>
    <p v-if="$stash.user">Hello, {{ $stash.user.name }}</p>
</div>
</template>

or from the script:

<template>
<div>
    <p v-if="user">Hello, {{ user.name }}</p>
</div>
</template>
 
<script>
export default {
    computed: {
        user () {
            return this.$stash.user
        },
    },
}
</script>

Modifying stash

The stash is (intentionally) reactive, feel free to modify it from within the Vue app:

<template>
<div>
    <p>Foo is {{ $stash.foo }}</p>
    <button @click="$stash.foo = 'Changed!'">Change Foo</button>
</div>
</template>

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.2.0
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.2.0
    0
  • 0.1.0
    0

Package Sidebar

Install

npm i nuxt-stash

Weekly Downloads

0

Version

0.2.0

License

MIT

Unpacked Size

4.98 kB

Total Files

5

Last publish

Collaborators

  • ilyasemenov