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

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - ฅ˙-˙ฅ

用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中进行配置,路由会从该文件中获取。