Node.js/nuxt/路由:修订间差异
来自康健生活
创建页面,内容为“== router.beforeEach == 刷新白屏 == 组件独享守卫 == 刷新白屏 == middleware == 在需要鉴权的页面添加配置项: <code>middleware: 'auth'</code…” |
|||
| 第5行: | 第5行: | ||
== middleware == | == middleware == | ||
在需要鉴权的页面添加配置项: | 在需要鉴权的页面添加配置项: | ||
<code>middleware: 'auth'</code> | <code>middleware: 'auth'</code>,关于中间件,所有的中间件都必须放置在middleware/目录下。文件名将作为中间件的名称(如:middleware/auth将成为中间件auth)。 | ||
中间件将收到的上下文作为第一个参数。 | |||
<blockquote><syntaxhighlight lang="javascript"> | |||
export default function (context) { | |||
context.userAgent = context.isServer ? context.req.headers['user-agent'] : navigator.userAgent | |||
}</syntaxhighlight></blockquote> | |||
执行顺序: | |||
* nuxt.config.js | |||
* 匹配的布局 | |||
* 匹配的页面 | |||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| 第28行: | 第39行: | ||
}) | }) | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
==== 参考 ==== | |||
* https://github.com/nuxt/example-auth0 | |||
2020年7月28日 (二) 05:34的最新版本
router.beforeEach
刷新白屏
组件独享守卫
刷新白屏
middleware
在需要鉴权的页面添加配置项:
middleware: 'auth',关于中间件,所有的中间件都必须放置在middleware/目录下。文件名将作为中间件的名称(如:middleware/auth将成为中间件auth)。
中间件将收到的上下文作为第一个参数。
export default function (context) { context.userAgent = context.isServer ? context.req.headers['user-agent'] : navigator.userAgent }
执行顺序:
- nuxt.config.js
- 匹配的布局
- 匹配的页面
export default function ({ store, redirect }) {
// If the user is not authenticated
if (!store.state.authenticated) {
return redirect('/login')
}
}
export default function ({ $axios, redirect }) {
$axios.onRequest(config => {
console.log('Making request to ' + config.url)
})
$axios.onError(error => {
const code = parseInt(error.response && error.response.status)
if (code === 400) {
redirect('/400')
}
})
}
参考
No categories