vue2?element?實現(xiàn)表格點擊詳情返回時保留查詢參數(shù)的示例代碼
先直觀一點,上圖
列表共5條數(shù)據(jù),準備輸入Author過濾條件進行查詢

進入查看詳情頁,就隨便搞了個按鈕 啥都沒調(diào)啦

點擊返回后

一開始準備用vuex做這個功能,后來放棄了,想到直接用路由去做可能也不錯。有時間再整一套vuex版的
<!--
* @Author: chenhaoran
* @Date: 2024-03-03 13:44:10
* @LastEditors: chenhaoran
* @LastEditTime: 2024-03-03 23:07:02
-->
<template>
<div class="app-container">
<div class="search-area">
<el-form :inline="true" :model="queryParams" class="demo-form-inline">
<el-form-item label="Author">
<el-input v-model="queryParams.author" placeholder="作者"></el-input>
</el-form-item>
<el-form-item label="Status">
<el-select v-model="queryParams.status" placeholder="狀態(tài)">
<el-option label="發(fā)布" value="published"></el-option>
<el-option label="刪除" value="deleted"></el-option>
<el-option label="草稿" value="draft"></el-option>
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">查詢</el-button>
<el-button type="primary" @click="reset">重置</el-button>
</el-form-item>
</el-form>
</div>
<el-table
v-loading="listLoading"
:data="list"
element-loading-text="Loading"
border
fit
highlight-current-row
>
<el-table-column align="center" label="ID" width="95">
<template slot-scope="scope">{{ scope.$index }}</template>
</el-table-column>
<el-table-column label="Title">
<template slot-scope="scope">{{ scope.row.title }}</template>
</el-table-column>
<el-table-column label="Author" width="110" align="center">
<template slot-scope="scope">
<span>{{ scope.row.author }}</span>
</template>
</el-table-column>
<el-table-column label="Pageviews" width="110" align="center">
<template slot-scope="scope">{{ scope.row.pageviews }}</template>
</el-table-column>
<el-table-column class-name="status-col" label="Status" width="110" align="center">
<template slot-scope="scope">
<el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
</template>
</el-table-column>
<el-table-column align="center" prop="created_at" label="Display_time" width="200">
<template slot-scope="scope">
<i class="el-icon-time" />
<span>{{ scope.row.display_time }}</span>
</template>
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button @click="doView(scope.row)" type="text" size="small">查看</el-button>
<el-button type="text" size="small">編輯</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import { getList } from '@/api/table'
// import { createNamespacedHelpers } from 'vuex'
// const { mapMutations, mapActions } = createNamespacedHelpers('queryParams')
export default {
filters: {
statusFilter(status) {
const statusMap = {
published: 'success',
draft: 'gray',
deleted: 'danger'
}
return statusMap[status]
}
},
beforeRouteEnter(to, from, next) {
// console.log('beforeRouteEnter:', from);
/**
* 官方文檔是這樣寫明的:
* -- start --
* beforeRouteEnter 守衛(wèi)不能訪問this,因為守衛(wèi)在導(dǎo)航確認前被調(diào)用,因此即將登場的新組件還沒被創(chuàng)建。
* 不過,你可以通過傳一個回調(diào)給 next 來訪問組件實例。在導(dǎo)航被確認的時候執(zhí)行回調(diào),并且把組件實例作為回調(diào)方法的參數(shù)
* beforeRouteEnter (to, from, next) {
next(vm => {
// 通過 `vm` 訪問組件實例
})
}
* -- end --
* 重點是第二句話,說明是有方法給組件實例修改值的
*/
/** 有問題的寫法
* const data = { testMsg: '測試信息'}
* const saveData = data
* next(vm => {
* 在這里卡了很久,這樣寫的話,組件created抑或mounted里,可以訪問data的屬性,但卻拿不到beforeRouteEnter中定義的屬性
* //vm.saveData = saveData
* //vm.$set(vm, 'saveData', saveData)
*
* })
*
* 執(zhí)行順序:
* beforeRouteEnter
* beforeCreate
* mounted
* vm
*/
// 有效處理
let obj = {}
if (from.name == 'itemDetail') {
obj = from.params
} else {
obj = {}
}
next(vm => {
/**
* 在這里卡了很久后,嘗試將設(shè)置value寫入methods方法中使用vm來調(diào)用,
* mounted拿不到beforeRouteEnter這里定義的變量,但是它可以訪問vm實例的變量和方法
* 再將beforeRouteEnter這定義的對象作為函數(shù)參數(shù)
*/
vm.setFilterParams(obj)
})
},
data() {
return {
list: null,
listLoading: false,
queryParams: {
author: '',
status: ''
},
}
},
created(){
this.fetchData()
},
mounted() {
// if (
// Object.keys(this.$store.state.queryParams.filterParams).length === 0
// ) {
// this.queryParams = {
// // pageNum: 1,
// // pageSize: 10,
// author: '',
// status: ''
// };
// } else {
// this.queryParams = JSON.parse(
// JSON.stringify(this.$store.state.queryParams.filterParams)
// );
// }
},
methods: {
// ...mapActions(["filterCache"]),
setFilterParams(obj) {
this.queryParams = Object.assign({},obj)
this.fetchData()
},
fetchData() {
this.listLoading = true
let queryParams = this.queryParams
getList(queryParams).then(response => {
this.list = response.data.items
this.listLoading = false
})
},
// 查看
doView(row) {
this.$router.push({
/* path與params不可同時出現(xiàn) */
// path: 'table/itemDetail',
name: 'itemDetail',
params: this.queryParams
})
},
// 查詢
onSubmit() {
// this.$store.dispatch("queryParams/filterCache", this.queryParams);
// this.filterCache(this.queryParams)
this.fetchData()
},
reset() {
this.queryParams = {}
this.fetchData()
}
}
}
</script>上面重點部分就是beforeRouteEnter了:
beforeRouteEnter(to, from, next) {
// console.log('beforeRouteEnter:', from);
/**
* 官方文檔是這樣寫明的:
* -- start --
* beforeRouteEnter 守衛(wèi)不能訪問this,因為守衛(wèi)在導(dǎo)航確認前被調(diào)用,因此即將登場的新組件還沒被創(chuàng)建。
* 不過,你可以通過傳一個回調(diào)給 next 來訪問組件實例。在導(dǎo)航被確認的時候執(zhí)行回調(diào),并且把組件實例作為回調(diào)方法的參數(shù)
* beforeRouteEnter (to, from, next) {
next(vm => {
// 通過 `vm` 訪問組件實例
})
}
* -- end --
* 重點是第二句話,說明是有方法給組件實例修改值的
*/
/** 有問題的寫法
* const data = { testMsg: '測試信息'}
* const saveData = data
* next(vm => {
* 在這里卡了很久,這樣寫的話,組件created抑或mounted里,可以訪問data的屬性,但卻拿不到beforeRouteEnter中定義的屬性
* //vm.saveData = saveData
* //vm.$set(vm, 'saveData', saveData)
*
* })
*
* 執(zhí)行順序:
* beforeRouteEnter
* beforeCreate
* mounted
* vm
*/
// 有效處理
let obj = {}
if (from.name == 'itemDetail') {
obj = from.params
} else {
obj = {}
}
next(vm => {
/**
* 在這里卡了很久后,嘗試將設(shè)置value寫入methods方法中使用vm來調(diào)用,
* mounted拿不到beforeRouteEnter這里定義的變量,但是它可以訪問vm實例的變量和方法
* 再將beforeRouteEnter這定義的對象作為函數(shù)參數(shù)
*/
vm.setFilterParams(obj)
})
},<!--
* @Author: chenhaoran
* @Date: 2024-03-03 14:59:08
* @LastEditors: chenhaoran
* @LastEditTime: 2024-03-03 22:31:39
-->
<template>
<div class="item-detail">
<el-button @click="handleClick">返回</el-button>
</div>
</template>
<script>
export default {
name: 'itemDetail',
data() {
return {
}
},
created() {
// console.log(this.$route);
},
methods: {
handleClick() {
/**
* go(-1): 原頁面表單中的內(nèi)容會丟失;
* this.$router.go(-1):后退+刷新;
* this.$router.go(0):刷新;
* this.$router.go(1) :前進
*
* back(): 原頁表表單中的內(nèi)容會保留;在返回界面?zhèn)鬟f參數(shù);
* this.$router.back():后退 ;
* this.$router.back(0) 刷新;
* this.$router.back(1):前進
*
*/
this.$router.back()
}
},
watch: {
}
}
</script>
<style>
</style>到此這篇關(guān)于vue2 element 實現(xiàn)表格點擊詳情,返回時保留查詢參數(shù)的文章就介紹到這了,更多相關(guān)vue2 element 表格點擊詳情內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中ts語法使用element plus分頁組件警告錯誤問題
這篇文章主要介紹了vue3中ts語法使用element plus分頁組件警告錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
Vue?3?中?vue-router?的?router.resolve?()?API詳解
router.resolve()?就好比是一個精準的?“導(dǎo)航參謀”,當我們在?Vue?3?應(yīng)用里需要明確某個路由地址對應(yīng)的詳細信息時,它就能派上用場,本文給大家介紹Vue?3?中?vue-router?的?router.resolve?()?API,感興趣的朋友一起看看吧2025-04-04
vue中的eventBus會不會產(chǎn)生內(nèi)存泄漏你知道嗎
這篇文章主要為大家詳細介紹了vue中的eventBus會不會產(chǎn)生內(nèi)存泄漏,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
vue.js數(shù)據(jù)響應(yīng)式原理解析
這篇文章主要介紹了vue.js數(shù)據(jù)響應(yīng)式原理解析,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08
Vue 實現(xiàn)拖動滑塊驗證功能(只有css+js沒有后臺驗證步驟)
這篇文章給大家介紹了基于vue實現(xiàn)拖動滑塊驗證功能,代碼引用css與js都是線上的,將代碼全部復(fù)制到一個html中可以直接打開,超級簡單,感興趣的朋友跟隨腳本之家小編一起看看吧2018-08-08
webpack4+Vue搭建自己的Vue-cli項目過程分享
這篇文章主要介紹了webpack4+Vue搭建自己的Vue-cli,對于vue-cli的強大,使用過的人都知道,極大的幫助我們降低了vue的入門門檻,感興趣的朋友跟隨腳本之家小編一起看看吧2018-08-08

