22、什么是中间件和权限拦截中间件实操

新建中间件

middleware\auth.js

javascript 复制代码
// 定义权限判断中间件,中间件的第一个参数是context
export default ({store, redirect}) => {
    console.log("中间件被调用")
    // if (!store || !store.state.userinfo) {
    //     redirect("/")
    // }
}

页面使用中间件

pages\users\index.vue

javascript 复制代码
<template>
    <div>
        用户首页
    </div>
</template>

<script>
export default {
    middleware: "auth"
};
</script>

<style lang="scss" scoped>

</style>

布局使用中间件

layouts\blog\index.vue

javascript 复制代码
<template>
    <div>
        <div>测试标题</div>
        <nuxt></nuxt>
    </div>
</template>>

<script>
export default {
    middleware: "auth"
}
</script>

<style>

</style>

全局使用中间件

nuxt.config.js

javascript 复制代码
export default {
  router: {
    middleware: "auth"
  }
}
相关推荐
ThsPool5 天前
【ENVI二次开发学习整理 02】IDL语言基础与程序设计
学习·中间件
程序员良辰5 天前
【TongWeb7】启动接近两分钟问题排查
java·开发语言·中间件
哎呦,帅小伙哦7 天前
Nanomsg库AIO模块学习心得
中间件·nanomsg
kisloy10 天前
【爬虫入门第10讲】Scrapy 框架深度解析(一):五大组件与中间件
爬虫·scrapy·中间件
晴天1611 天前
AI应用开发(智能体-全栈开发工程师 岗位分析)-Day04
人工智能·中间件
fuquxiaoguang12 天前
MCP协议移交Linux基金会:AI智能体中间件标准定型,国产厂商迎“Kubernetes时刻”
linux·人工智能·中间件
谁看我谁 狼家二丫12 天前
[MAF预定义ChatClient中间件-06]利用ImageGeneratingChatClient开发专业图片生成Agent
中间件
q5673152313 天前
Scrapy 框架集成稳定 HTTP 代理:中间件配置与断线重试实战
爬虫·网络协议·scrapy·http·中间件·http代理
phltxy13 天前
LangChain_Agent中间件实战
人工智能·python·深度学习·语言模型·中间件·langchain
程序员良辰13 天前
【TongWeb7启动报错】TongWeb控制台可以访问,但是应用访问404
java·开发语言·中间件