Skip to content

Instantly share code, notes, and snippets.

@mercs600
mercs600 / store-index.js
Created April 6, 2020 17:04
nuxt-exercise-6-errors
export const actions = {
async nuxtServerInit ({ commit }, { $http, error }) {
try {
const menu = await $http.$gest('api/menus')
commit('setMenu', menu)
} catch (err) {
return error(err)
}
}
}
@mercs600
mercs600 / layouts-default.vue
Created April 5, 2020 16:55
nuxt-exercise-6-display-menu
<template>
<div class="container">
<div class="menu">
<nuxt-link
v-for="({link, label}, key) in menu"
:key="key"
:to="link"> {{ label }} </nuxt-link>
</div>
<nuxt
:keep-alive-props="{ max: 10 }"
@mercs600
mercs600 / store-index.js
Created April 5, 2020 16:23
nuxt-exercise-6-store-actions
export const actions = {
async nuxtServerInit ({ commit }, { $http }) {
const menu = await $http.$get('api/menu')
commit('setMenu', menu)
}
}
export const state = () => ({
menu: []
})
@mercs600
mercs600 / store-index.js
Created April 5, 2020 16:11
nuxt-exercise-6-vuex-register
export const state = () => ({
menu: []
})
export const mutations = {
setMenu(state, payload) {
state.menu = payload
}
}
@mercs600
mercs600 / nuxt.config.js
Created April 5, 2020 15:59
nuxt-exercise-6-register-servermiddlware
export default {
...
/**
* Nuxt server middlewares
*/
serverMiddleware: [
{ path: '/api/menu', handler: '~/api/menu.js' },
],
...
}
@mercs600
mercs600 / nuxt.config.js
Created April 5, 2020 15:59
nuxt-exercise-6-register-servermiddlware
export default {
...
/**
* Nuxt server middlewares
*/
serverMiddleware: [
{ path: '/api/menu', handler: '~/api/menu.js' },
],
...
}
@mercs600
mercs600 / nuxt.config.js
Created April 5, 2020 15:59
nuxt-exercise-6-register-servermiddlware
export default {
...
/**
* Nuxt server middlewares
*/
serverMiddleware: [
{ path: '/api/menu', handler: '~/api/menu.js' },
],
...
}
@mercs600
mercs600 / api-menu.js
Last active April 5, 2020 16:07
nuxt-exercise-6-fake-api
export default function (req, res, next) {
const menu = [
{
label: 'Home',
link: '/'
},
{
label: 'asyncData simple',
link: '/asyncData-basic'
},
@mercs600
mercs600 / fetch-blog-_id+author.vue
Created April 4, 2020 14:57
nuxt-exercise-5-singlepost-author
<template>
<div>
<div v-if="$fetchState.pending">
<content-placeholders>
<content-placeholders-heading/>
<content-placeholders-text :lines="3" />
</content-placeholders>
</div>
<div v-if="$fetchState.error">
<h1>{{ $fetchState.error.statusCode }} An error occurred: {{ $fetchState.error.message }}</h1>
@mercs600
mercs600 / Author.vue
Created April 4, 2020 14:51
nuxt-exercise-5-author
<template>
<div>
<div v-if="$fetchState.pending">
<content-placeholders-text :lines="1" />
</div>
<div v-else-if="author">
<p>{{ author.name }} / {{ author.email }}</p>
</div>
</div>
</template>