Vue中在新窗口打開頁面及Vue-router的使用
背景
在開發(fā)提分加項目的過程中,遇到了點擊下拉菜單時在新窗口中打開頁面,由于之前一直做的是單頁面應(yīng)用,沒有碰到過類似的需求,于是上網(wǎng)搜了一下解決辦法,也再次系統(tǒng)地溫習(xí)了一下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
•動態(tài)路由匹配:一個“路徑參數(shù)”使用冒號:標記。當匹配到一個路由時,參數(shù)值會被設(shè)置到this.$route.params,可以在每個組件內(nèi)使用。
•嵌套路由:要在嵌套的出口中渲染組件,需要在 VueRouter 的參數(shù)中使用 children 配置,要注意,以 / 開頭的嵌套路徑會被當作根路徑。 這讓你充分的使用嵌套組件而無須設(shè)置嵌套的路徑。
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 }, { // 查看方案內(nèi)容 path: 'planList/:planID', name: 'score-plan-list', meta: { text: '查看方案內(nèi)容' }, component: ScorePlanList }, { // 下載方案內(nèi)容 path: 'download/:planID/:classID', name: 'score-download-list', meta: { text: '下載方案內(nèi)容' }, component: DownloadList }, { // 查看推送試題 path: 'push/:planID/:level', name: 'score-question-push', meta: { text: '查看推送試題' }, component: QuestionPush }, { // 提分方案首頁 path: '', name: 'scoreplus-index', component: ScoreIndex } ] }
•編程式導(dǎo)航
1.router.push(location, onComplete?, onAbort?):想要導(dǎo)航到不同的 URL,則使用 router.push 方法。這個方法會向 history 棧添加一個新的記錄,所以,當用戶點擊瀏覽器后退按鈕時,則回到之前的 URL。
// 字符串 router.push('home') // 對象 router.push({ path: 'home' }) // 命名的路由 router.push({ name: 'user', params: { userId: 123 }}) // 帶查詢參數(shù),變成 /register?plan=private router.push({ path: 'register', query: { plan: 'private' }})
在 2.2.0+,可選的在 router.push 或 router.replace 中提供 onComplete 和 onAbort 回調(diào)作為第二個和第三個參數(shù)。這些回調(diào)將會在導(dǎo)航成功完成 (在所有的異步鉤子被解析之后) 或終止 (導(dǎo)航到相同的路由、或在當前導(dǎo)航完成之前導(dǎo)航到另一個不同的路由) 的時候進行相應(yīng)的調(diào)用。
2.router.replace(location, onComplete?, onAbort?):跟 router.push 很像,唯一的不同就是,它不會向 history 添加新記錄,而是跟它的方法名一樣 —— 替換掉當前的 history 記錄。
3.router.go(n):這個方法的參數(shù)是一個整數(shù),意思是在 history 記錄中向前或者后退多少步,類似 window.history.go(n)。
•命名路由:可以在創(chuàng)建 Router 實例的時候,在 routes 配置中給某個路由設(shè)置名稱。要鏈接到一個命名路由,可以給 router-link 的 to 屬性傳一個對象。
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link> router.push({ name: 'user', params: { userId: 123 }})
•重定向和別名
1.重定向(redirect):
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' } ] })
總結(jié)
以上所述是小編給大家介紹的Vue中在新窗口打開頁面及Vue-router的 使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vuejs 切換導(dǎo)航條高亮(路由菜單高亮)的方法示例
這篇文章主要介紹了vuejs 切換導(dǎo)航條高亮路由高亮的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Vue3-新特性defineOptions和defineModel示例詳解
在Vue3.3中新引入了defineOptions宏主要是用來定義Option API的選項,可以用defineOptions定義任意的選項,props、emits、expose、slots除外,本文給大家介紹Vue3-新特性defineOptions和defineModel,感興趣的朋友一起看看吧2023-11-11vue?內(nèi)置組件?component?的用法示例詳解
這篇文章主要介紹了vue內(nèi)置組件component的用法,本文給大家介紹了component內(nèi)置組件切換方法,通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08