获取url参数
getParam: function (name) {
let search = document.location.search
let pattern = new RegExp('[?&]' + name + '=([^&]+)', 'g')
let matcher = pattern.exec(search)
let items = null
if (matcher != null) {
try {
items = decodeURIComponent(decodeURIComponent(matcher[1]))
} catch (e) {
try {
items = decodeURIComponent(matcher[1])
} catch (e) {
items = matcher[1]
}
}
}
return items
}
vue使用路由动态设置页面title
vue是单页面应用程序,一般只有一个index.html文件,通过路由去跳转到不同组件,当需要根据路由设置页面title时,可使用导航卫士实现。
router中定义一个路由:
{
path: '/',
name: 'home',
component: home,
meta: {
title: 'VPEA学生生涯档案'
}
}
使用router的beforeEach方法:
/* 导航守卫 */
router.beforeEach((to, from, next) => {
/* 路由发生变化修改页面title */
if (to.meta.title) {
document.title = to.meta.title
}
next()
})
vue刷新页面
1.以下这两种方法都可以刷新页面,相当于f5强制刷新,体验不好
location. reload() 和 this.$router.go(0)
2.新建一个空白页,页面跳转之后再立马跳转回来,相比第一种方法不会出现一瞬间的空白页,只是地址栏有一个快速的切换过程。可采用
let NewPage = '_empty'
this.$router.push(NewPage)
this.$router.go(-1)
js判断滚动条是否滚动到底部
// 文档被卷起的高度
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop
// 可视区的高度
let windowHeight = document.documentElement.clientHeight || document.body.clientHeight
// 滚动条的总高度
let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight
if (scrollTop + windowHeight === scrollHeight) {
console.log('已经滚动到底部')
}
其中scrollTop为滚动条滚动高度,