当前位置:   article > 正文

vue3-----单个赋值解决对象直接赋值导致对象响应式失效(里面的属性值会改变不了) + 路由传参_在vue3中v-if赋值之后不会发生响应式的改变如何解决

在vue3中v-if赋值之后不会发生响应式的改变如何解决

接收路由跳转传过来的参数

  1. //接收路由跳转传过来的参数
  2. const route = useRoute()
  3. const searchForm = reactive({
  4. dateTime: route.query.dateTime
  5. })

 Object.assign解决不了(因为Object.assign外层是深拷贝,内层是浅拷贝),只能单个赋值(用变量捯一遍),对象直接赋值导致对象响应式失效(里面的属性值会改变不了)================本篇重点===============

  1. // params = ...searchForm 重新赋值后会,params会变成普通对象,失去响应式的效果,需要单个赋值或者使用 Object.assign
  2. // const params = {...searchForm, gsType: route.query.gsType,ydGs: route.query.ydGs,deptId: deptId.value}
  3. //上面这种直接赋值就会出错,加reactive也没用,试过了,别犟
  4. const params = Object.assign({},{
  5. ...searchForm, gsType: route.query.gsType,ydGs: route.query.ydGs,deptId: deptId.value
  6. })
  7. const getStatisticsInfo = () =>{
  8. isIndex.value = false;
  9. statisticsInfo(params).then( res =>{
  10. let data = res.data.data.list
  11. districTElecTableData.value = data;
  12. }).catch( error =>{
  13. console.log( 'error',error )
  14. })
  15. }
  16. //上面那个拷贝没用,响应式还是没有,老老实实写在参数括号里面
  17. // 列表
  18. const getDistricTElecList = () => {
  19. isIndex.value = true;
  20. //老老实实写在参数括号里面
  21. statisticsGq({...searchForm, gsType: route.query.gsType,ydGs: route.query.ydGs}).then( res =>{
  22. let data = res.data.data
  23. districTElecTableData.value = data;
  24. }).catch( error =>{
  25. console.log( 'error',error )
  26. })
  27. };
  28. //再捯一遍,不直接赋值也能保留对象的响应式
  29. //搞个变量接收路由跳转携带的参,然后把变量赋值给响应式对象的属性
  30. let IdeptId = route.query.deptId
  31. let IydGs = route.query.ydGs
  32. let IgsType = route.query.gsType
  33. const searchForm = reactive({
  34. deptId: IdeptId,
  35. ydGs:IydGs,
  36. gsType:IgsType,
  37. beginTime: begin,
  38. endTime: end
  39. })
  40. // 表格数据
  41. let districTElecTableData = ref([]);
  42. // 列表
  43. const getDistricTElecList = () => {
  44. const param = { ...searchForm, page: dataList.page, limit: dataList.limit }
  45. getElectroList(param).then( res => {
  46. if(res.data.code == 200){
  47. dataList.tableData = [];
  48. let data = res.data.data;
  49. dataList.tableData = data.list;
  50. dataList.total = data.totalCount;
  51. }
  52. }).catch( error =>{
  53. console.log( 'error',error )
  54. })
  55. };

 只能单个赋值(用变量捯一遍)============================本篇重点的正确解决方法================================

  1. <!-- 这是页面结构,需要一个响应式对象的各个属性来绑定表单里各个组件,从而获取前端,用户选择的值 -->
  2. <el-form :inline="true">
  3. <el-form-item label="管理区">
  4. <el-select v-model="searchForm.deptId" placeholder="请选择管理区名称" label-width="100px" >
  5. <el-option v-for="item in optionsGQ" :key="item.value" :label="item.label" :value="item.value" />
  6. </el-select>
  7. </el-form-item>
  8. <el-form-item label="用电归属">
  9. <el-select v-model="searchForm.ydGs" placeholder="请选择用电归属" label-width="100px" @change = 'handleChangeYDGS'>
  10. <el-option v-for="item in optionsGS" :key="item.value" :label="item.label" :value="item.value" />
  11. </el-select>
  12. </el-form-item>
  13. <el-form-item label="用电类型">
  14. <el-select v-model="searchForm.gsType" placeholder="请选择用电类型" label-width="100px" >
  15. <el-option v-for="item in optionsYDLX" :key="item.value" :label="item.label" :value="item.value" />
  16. </el-select>
  17. </el-form-item>
  18. <el-form-item label="日期">
  19. <el-date-picker v-model="searchForm.beginTime" value-format="YYYY-MM-DD" type="date" placeholder="请选择开始日期"
  20. style="width:150px;"/>
  21. <span> &nbsp;-&nbsp; </span>
  22. <el-date-picker v-model="searchForm.endTime" value-format="YYYY-MM-DD" type="date" placeholder="请选择结束日期"
  23. style="width:150px;" />
  24. </el-form-item>
  25. <el-form-item label="用电单位" >
  26. <el-input v-model="searchForm.deptName" placeholder="请输入用电单位名称" label-width="100px" />
  27. </el-form-item>
  28. <el-form-item>
  29. <el-button type="primary" @click="getDistricTElecList">查询</el-button>
  30. <el-button @click="onReset">重置</el-button>
  31. <el-button type="primary" @click="backToPage">返回</el-button>
  32. <el-button type="primary" icon="Download" @click="exportElecDetail">导出</el-button>
  33. </el-form-item>
  34. </el-form>

 对象的各个属性,单个赋初值(用变量捯一遍),才能保住对象searchForm的响应式

  1. let IdeptId = route.query.deptId
  2. // 用电归属
  3. let IydGs = route.query.ydGs
  4. let IdeptName = route.query.deptName ? route.query.deptName : ''
  5. // 用电类型
  6. let IgsType = route.query.gsType
  7. const searchForm = reactive({
  8. deptId: IdeptId,
  9. ydGs: IydGs,
  10. gsType: IgsType,
  11. beginTime: begin,
  12. endTime: end,
  13. deptName: IdeptName
  14. })

这样就可以创建一个对象params来组合所需的属性,同时还能保持searchForm的响应式 

  1. // 表格数据
  2. let districTElecTableData = ref([]);
  3. // 列表
  4. const getDistricTElecList = () => {
  5. //这样就可以创建一个对象params来组合所需的属性,同时还能保持searchForm的响应式
  6. const param = { ...searchForm, page: dataList.page, limit: dataList.limit }
  7. getElectroList(param).then( res => {
  8. if(res.data.code == 200){
  9. dataList.tableData = [];
  10. let data = res.data.data;
  11. dataList.tableData = data.list;
  12. dataList.total = data.totalCount;
  13. }
  14. }).catch( error =>{
  15. console.log( 'error',error )
  16. })
  17. };

 携带参数跳转

  1. import { useRoute, useRouter } from 'vue-router' // useRoute跳回去,接参的;
  2. //携带参数跳转
  3. const handleRowClick = (row) => {
  4. if(row.treeNum == 1){
  5. router.push({
  6. path:'/DistricTElec/index2',
  7. query:{
  8. gsType: row.gsType,
  9. ydGs: row.ydGs,
  10. dateTime : searchForm.dateTime
  11. }
  12. })
  13. }
  14. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/68236
推荐阅读
相关标签
  

闽ICP备14008679号