组件的循环引用

1、递归组件:组件在自己的模板中调用自身。
2、循环引用:当父引用子,子引用父时。

直接给大家上代码:

  • 第一步,MulAnalysisResult.vue
 1 <template>
 2   <div v-loading="loading">
 3     <div v-for="(item, index) in dataList" :key="index">
 4       <h6>{{ item.fileName }}</h6>
 5       <AnalysisResultTable :tableHeader="item.columns" :tableData="item.results" :height="'400px'"  ref="analysisResultTableRef" />
 6     </div>
 7   </div>
 8 </template>
 9 
10 <script>
11 /* eslint-disable */
12 import { getTestResults } from "@/api/test";
13 import AnalysisResultTable from "../components/AnalysisResultTable.vue";
14 
15 export default {
16   name: "MulAnalysisResult",
17   components: {
18     AnalysisResultTable,
19   },
20   data() {
21     return {
22       loading: false,
23       dataList: []
24     };
25   },
26   created() {
27     this.getList();
28   },
29   methods: {
30     getList() {
31       this.loading = true;
32       getTestResults(this.$route.query.id)
33         .then((res) => {
34           const { code, data, msg } = res;
35           if (code == 0) {
36             this.dataList = data || [];
37           } else {
38             this.$message.error(msg);
39           }
40         })
41         .finally(() => {
42           this.loading = false;
43         });
44     },
45   },
46 };
47 </script>
48 
49 <style scoped lang="less"></style>
View Code

说明没有编写css样式,请自行编写

  • 第二步,AnalysisResultTable.vue
  1 <template>
  2   <div>
  3     <el-table
  4       :data="tableData"
  5       style="width: 100%"
  6       :height="height"
  7       ref="table"
  8       :header-cell-style="{
  9         'background-color': '#FAFAFA',
 10         color: 'rgba(0, 0, 0, 0.85)',
 11         'font-weight': 'normal',
 12       }"
 13       size="small"
 14     >
 15       <template v-for="(item, index) in tableHeader">
 16         <el-table-column :min-width="120" :key="index" :prop="item.fName" show-overflow-tooltip>
 17           <template slot-scope slot="header">
 18             <el-tooltip
 19               effect="dark"
 20               :content="item.fName"
 21               placement="top"
 22               class="table-header"
 23             >
 24               <span>{{ item.fName }}</span>
 25             </el-tooltip>
 26           </template>
 27           <template slot-scope="scope">
 28             <el-input
 29               v-if="item.fType == '1'"
 30               placeholder="请输入"
 31               v-model="scope.row[item.fName]"
 32               size="mini"
 33               clearable
 34             />
 35             <div v-else-if="item.fType == '2' || item.fType == '3'">
 36               {{ scope.row[item.fName] }}
 37             </div>
 38             <div v-else class="name">
 39               <el-button size="small" type="text" @click="handleView(scope.row, item)"
 40                 >查看</el-button
 41               >
 42             </div>
 43           </template>
 44         </el-table-column>
 45       </template>
 46     </el-table>
 47     <!-- 查看下转项 -->
 48     <TripItemDialog ref="TripItemRef" :addData="addData" />
 49   </div>
 50 </template>
 51 
 52 <script>
 53 /* eslint-disable */
 54 import TripItemDialog from "@/components/dialog/intelligentDecision/tripItem.vue";
 55 export default {
 56   name: "analysisResultTable",
 57   components: { TripItemDialog },
 58   props: {
 59     tableData: {
 60       type: Array,
 61       required: true,
 62     },
 63     tableHeader: {
 64       type: Array,
 65       required: true,
 66     },
 67     height: {
 68       type: String,
 69       default: () => {
 70         return '100%';
 71       },
 72     },
 73   },
 74   data() {
 75     return {
 76       addData: {
 77         state: false,
 78         data: null,
 79         drillDownIds:[]
 80       },
 81       drillDownConfig: {
 82         columnName: null,
 83         columnValues: [],
 84         tableId: null,
 85         targetTableId: null,
 86         taskId: null
 87       },
 88     };
 89   },
 90   methods: {
 91     handleView(row, item) {
 92       this.drillDownConfig = {
 93         columnName: null,
 94         columnValues: [],
 95         tableId: null,
 96         targetTableId: null,
 97         taskId: null
 98       };
 99       const { currentTableId: tableId, fName: columnName, drillDownIds } = item;
100       let taskId = this.$route.query.id;
101 
102       let tHeader = this.tableHeader.filter((item) => item.pk == 1);
103       tHeader.forEach((m) => {
104         for (let i in row) {
105           if (m.fName == i) {
106             this.drillDownConfig.columnValues.push({
107               dataPk: m.viewColumn,
108               dataValue: row[i],
109             });
110           }
111         }
112       });
113       this.drillDownConfig.columnName = columnName;
114       this.drillDownConfig.tableId = tableId;
115       this.drillDownConfig.taskId = taskId;
116       this.addData.state = true;
117       this.addData.data = this.drillDownConfig;
118       this.addData.drillDownIds = drillDownIds;
119       this.$refs.TripItemRef.showData();
120     },
121   },
122 };
123 </script>
124 <style scoped lang="less"></style>
View Code

说明页面中使用弹框组件

  • 第三步,tripItem.vue
  1 <template>
  2   <div>
  3     <el-dialog
  4       width="60%"
  5       :title="fileName"
  6       :visible="addData.state"
  7       @close="closeModal"
  8       @config="handleSubmit"
  9       append-to-body
 10       destroy-on-close
 11       :footer="false"
 12     >
 13       <div>
 14         <el-button
 15         type="primary"
 16         icon="el-icon-download"
 17         size="small"
 18         plain
 19         @click="exportTable"
 20         :loading="isExport"
 21         >导出</el-button
 22       >
 23       </div>
 24       <div v-loading="tableLoading">
 25           <div v-for="(item, index) in dataList" :key="index">
 26               <h6 class="com_title">{{ item.fileName }}</h6>
 27               <AnalysisResultTable
 28               :tableHeader="item.columns"
 29               :tableData="item.results"
 30               ref="analysisResultTableRef"
 31             />
 32           </div>
 33       </div>
 34     </el-dialog>
 35   </div>
 36 </template>
 37 
 38 <script>
 39 /* eslint-disable */
 40 import { testDown} from "接口地址";
 41 import { exportExcel } from "@/util/exportExcel.js";
 42 
 43 export default {
 44   name: "tripItem",
 45   components: {
 46     AnalysisResultTable: () =>
 47       import(
 48         "@/views/decisionPlatform/intelligentDecision/components/AnalysisResultTable.vue"
 49       ),
 50   },
 51   props: {
 52     addData: {
 53       type: Object,
 54       required: true,
 55     },
 56   },
 57   data() {
 58     return {
 59       fileName: "",
 60       tableLoading: false,
 61       dataList:[],
 62       isExport: false,
 63     };
 64   },
 65   methods: {
 66     showData() {
 67       this.tableLoading = true;
 68       this.dataList = [];
 69 
 70       if(this.addData.drillDownIds.length){
 71         this.addData.drillDownIds.forEach(item=>{
 72           this.addData.data.targetTableId = item;
 73 
 74           testDown(this.addData.data)
 75           .then((res) => {
 76             const { code, data, msg } = res;
 77             if (code === 0) {
 78               this.dataList = [...this.dataList, data] || [];
 79               this.tableLoading = false;
 80             } else {
 81               this.$message.error(msg);
 82             }
 83           })
 84           .finally(() => {
 85             this.tableLoading = false;
 86           });
 87         });
 88       }
 89     },
 90     exportTable() {
 91       this.isExport = true;
 92       exportExcel({
 93         url: `接口地址`,
 94         method: "post",
 95         data: this.addData.data,
 96       }).finally(() => {
 97         this.isExport = false;
 98       });
 99     },
100     handleSubmit() {
101       this.closeModal();
102     },
103     closeModal() {
104       this.addData.state = false;
105     },
106   },
107 };
108 </script>
109 
110 <style lang="less" scoped></style>
View Code

说明在此组件中循环引用AnalysisResultTable.vue组件

使用两种方式解决此问题:

1)方法

1   components: {
2     table: () =>import("@/views/2.vue"),
3   },
View Code

2)方法

1   beforeCreate() {
2     const table = () => import("@/views/2.vue");
3     this.$options.components.table = table;
4   },
View Code

在生命周期beforeCreate()中动态插入组件

鉴定完毕,欢迎友们一起交流学习!!

 

posted @ 2023-05-17 10:15  红石榴21  阅读(110)  评论(0编辑  收藏  举报