Vue中在新窗口打开页面及Vue-router的使用

(编辑:jimmy 日期: 2024/9/26 浏览:2)

背景

在开发提分加项目的过程中,遇到了点击下拉菜单时在新窗口中打开页面,由于之前一直做的是单页面应用,没有碰到过类似的需求,于是上网搜了一下解决办法,也再次系统地温习了一下vue-router。

Vue中在新窗口打开页面及Vue-router的使用

Vue中在新窗口打开页面及Vue-router的使用

解决

使用路由对象的resolve方法解析路由,可以得到location、router、href等目标路由的信息。得到href就可以使用window.open开新窗口了。

const {href} = this.$router.resolve({
 name: "statistics-explain",
 params: {
 classID: id,
 examSubjectID: this.planClassData.examSubjectID,
 studentStatus: 0
 }
});
window.open(href, '_blank');

延伸

参考文章:Vue Router

"htmlcode">

export default {
path: '/scoreplus',
name: 'scoreplus',
component: { template: '<router-view />' },
redirect: { name: 'scoreplus-index' },
children: [
 {
 // 查看个人方案
 path: 'preview/:examSubjectID/:xuexinID/:recordsID/:constitute/:planID',
 name: 'score-preview',
 meta: { text: '个人方案' },
 component: ScorePreview
 },
 {
 // 查看方案内容
 path: 'planList/:planID',
 name: 'score-plan-list',
 meta: { text: '查看方案内容' },
 component: ScorePlanList
 },
 {
 // 下载方案内容
 path: 'download/:planID/:classID',
 name: 'score-download-list',
 meta: { text: '下载方案内容' },
 component: DownloadList
 },
 {
 // 查看推送试题
 path: 'push/:planID/:level',
 name: 'score-question-push',
 meta: { text: '查看推送试题' },
 component: QuestionPush
 },
 {
 // 提分方案首页
 path: '',
 name: 'scoreplus-index',
 component: ScoreIndex
 }
]
}

"htmlcode">

// 字符串
router.push('home')
// 对象
router.push({ path: 'home' })
// 命名的路由
router.push({ name: 'user', params: { userId: 123 }})
// 带查询参数,变成 /register"htmlcode">
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
router.push({ name: 'user', params: { userId: 123 }})

"htmlcode">

 const router = new VueRouter({
 routes: [
 { path: '/a', redirect: '/b' }
 ]
 })

2.别名:/a 的别名是 /b,意味着,当用户访问 /b 时,URL 会保持为 /b,但是路由匹配则为 /a,就像用户访问 /a 一样。

 const router = new VueRouter({
 routes: [
 { path: '/a', component: A, alias: '/b' }
 ]
 })

总结

以上所述是小编给大家介绍的Vue中在新窗口打开页面及Vue-router的 使用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。