vue實(shí)現(xiàn)A頁(yè)面跳轉(zhuǎn)到B頁(yè)面的方法:1、設(shè)置A頁(yè)面,代碼如“
”;2、將跳轉(zhuǎn)的url添加到$router中;3、設(shè)置B頁(yè)面,代碼如“created() {…}”;4、修改js內(nèi)容即可。
本文操作環(huán)境:windows7系統(tǒng)、Vue2.9.6版,DELL G3電腦。
vue怎么實(shí)現(xiàn)A頁(yè)面跳轉(zhuǎn)到B頁(yè)面?
vue 從A界面跳轉(zhuǎn)到B界面并攜帶參數(shù)
最近遇到一個(gè)需求,需要從A界面點(diǎn)擊一個(gè)按鈕跳轉(zhuǎn)到B界面,并且攜帶參數(shù)。
我是這樣子實(shí)現(xiàn)了需求:
A頁(yè)面:
<el-button size="mini" type="success" @click="add" icon="el-icon-plus" round plain>{{$t('common.add')}}</el-button> <el-button type="primary" size="mini" @click="edit" icon="el-icon-edit" plain round>{{ $t('common.edit') }}</el-button>
點(diǎn)擊事件:
add() { this.lockTaskStatus = 'new' this.toLockTaskManagePage()},edit() { this.lockTaskStatus = 'edit' this.toLockTaskManagePage()},toLockTaskManagePage() { this.$router.push({ path: '/taskLockManage', name: 'TaskLockManage', params: { status: this.lockTaskStatus } })}
將將跳轉(zhuǎn)的url添加到 $router中。
path 中的url 最前面加 / 代表是根目錄下,不加則表示是子路由。
通過path + params的組合傳遞參數(shù)。
B頁(yè)面:
created() { this.getParams() }, watch: { // 監(jiān)測(cè)路由變化,只要變化了就調(diào)用獲取路由參數(shù)方法將數(shù)據(jù)存儲(chǔ)本組件即可 $route: 'getParams' }, methods: { getParams() { // 取到路由帶過來的參數(shù) const res = this.$route.params.status console.log('getParams', res) }}
最后,還需在router/index.js中注冊(cè):
{ path: '/taskLockManage', component: Layout, redirect: '/taskManage/index', hidden: true, children: [ { path: 'taskLockManage', component: () => import('@/views/taskManage/taskLockManage'), name: 'TaskLockManage', meta: { title: 'taskLockManage', icon: 'user', noCache: true } } ]}
這樣,便可實(shí)現(xiàn)了跳轉(zhuǎn)。(PS:這是目前發(fā)現(xiàn)的比較好的方法,希望哪位大佬有更好的方法指導(dǎo)。)