惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - ฅ˙-˙ฅ

用display实现效果:上面根据内容自适应高度;下面撑满所有,并且超出时候显示滚动条 react更改多层对象变量的方法 react umi model使用注意事项 拖动改变顺序 博文阅读密码验证 - 博客园 antd input控制只能输入数字并进行格式化显示(antd 3版本) react函数式组件:父组件调用子组件方法 react form表单中自定义组件的数据双向绑定实现 vue3+vant h5: Rem 移动端布局适配之postcss-pxtorem和lib-flexible 项目记录文档 前端mock方案:使用json-server mac安装使用nginx 博文阅读密码验证 - 博客园 umi修改antd主题颜色(通过less文件修改) 部署一个vue项目到阿里云服务器 vue项目通过点击按钮实现复制图片(非图片url和base64),可发送到聊天框 vue重新刷新当前路由(非浏览器强刷,不会出现闪屏) 使用Vue.extend实现iview Upload在单文件上传时,拖拽多个文件给出错误提示 uni-app小程序iPhone X适配底部栏黑横线 使用css自定义变量实现实现主题切换功能
uni-app怎么使用路由守卫,并且路由配置和pages.json中只写一套
ฅ˙-˙ฅ · 2020-10-13 · via 博客园 - ฅ˙-˙ฅ

背景

uni-app本身没有路由,无法使用路由守卫。
那么有这么一个功能: 当某些页面需要登录,进入之前需要判断是否登录,如果没有登录则跳转到登录页。
可以封装公共方法或混入实现,但是不太优雅,这时使用路由守卫实在是太方便了!
幸好,插件uni-simple-router给我们提供了

1. uni-simple-router使用方法

uni-simple-router 是 专为 uni-app 打造的路由管理器。它保留了 Vue-router 完全相似的书写风格,让你 倍感亲切。 让构建单页面应用变得易如反掌。
具体使用详见uni-simple-router文档

2. 从pages.js获取配置的pages来配置路由

引入uni-simple-router后,我们要配置路由,那么问题来了,我们之前在pages.json中配置过页面,这里又要配置路由,相当于一个页面进行了两次配置,太麻烦!

使用一套配置方法如下:

a. 将pages.json中的配置移入pages.js中

uni-app自带一个webpack loader钩子文件pages.js,在项目src目录下建立pages.js(与pages.json同级)即可生效(pages.json仍然需要存在,内容为{}即可)。
pages.js要求CommonJS规范,直接通过module.exports输出一个钩子函数。

module.exports = () => ({
	"pages": [
		{
			"path": "pages/mine/index",
			"style": {
				"navigationBarTitleText": "我的",
				"navigationStyle": "custom"
			}
		},
		{
			"path": "pages/index/search,
			"style": {
				"navigationBarTitleText": "查询"
			},
			meta: {
				needLogin: true
			}
		},
                ···
        ],
	"globalStyle": {
		···
	}
})
b. router.js中导入pages.js,基于pages进行封装路由
import Vue from 'vue'
import store from './store'

import Router, {
	RouterMount
} from './js_sdk/hhyang-uni-simple-router/index.js'
import pages from './pages.js'

Vue.use(Router)

const myRouter =
	pages().pages.map(item => ({
		path: `/${item.path}`,
		meta: item.meta || {}
	}))
//初始化
const router = new Router({
	routes: myRouter
});

//全局路由前置守卫
router.beforeEach((to, from, next) => {
	const isLogin = store.getters.isLogin
	if (!isLogin && to.meta.needLogin) {
		next({
			path: '/pages/login/index',
			query: {
				redirect: to.path
			}
		})
	} else {
		next()
	}

})
// 全局路由后置守卫
router.afterEach((to, from) => {})
export default router;

这样,页面只需要在pages.js中进行配置,路由会从该文件中获取。