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"
  }
}
相关推荐
程序员小八77714 小时前
Go Web 工程化:日志、配置与错误处理中间件,让服务「能上线」
前端·中间件·golang
tqs_1234514 小时前
SaaS 多租户 Agent 平台实战:自研 Agent 中间件 SDK,Nacos 配置驱动实现开箱即用
中间件
未若君雅裁17 小时前
自定义中间件:Node-style 与 Wrap-style 钩子全解析
python·中间件·langchain
__zRainy__2 天前
Node系列 · Express:常用的中间件
中间件·node.js·express
2601_962181962 天前
【ROS2 中间件RMW】基于FastDDS共享内存实现ROS2跨进程零拷贝通讯
中间件
未若君雅裁2 天前
上下文压缩双刃剑-Summarization与ContextEditing中间件实战
python·中间件·langchain
烂蜻蜓3 天前
Flask入门教程(十六):中间件与扩展——增强应用功能的两种机制
python·中间件·flask
__zRainy__4 天前
Node系列 · Express:中间件
中间件·node.js·express
LayZhangStrive5 天前
后端开发中间件 - Nacos下载并快速使用教程
中间件·nacos
念何架构之路5 天前
Gin内置中间件详解
中间件·gin