Skip to content

Change api to middleware #35

@AndrewBogdanovTSS

Description

@AndrewBogdanovTSS

That's just a suggestion. Since we don't have any example of a middleware inside a template but we use an undocumented api folder which is just a discrepant addition in my opinion. Why not use middleware as Nuxt intended? api/people.ts can be placed in middleware folder and rewritten as such:

export default async function({store, error, redirect}){
    if(process.client) return;
    await store.dispatch('getPeople')
}

and in the store actions:

async getPeople({commit}){
            commit('setPeople', await axios.get('/random-data.json').data.slice(0,3));
    }

and in nuxt.config.js module.exports object

router: {
    middleware: 'people'
  },

this way we can showcase how middleware function can be used in the template that might be beneficial for a lot of users

This question is available on Nuxt.js community (#c30)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions