组件之间的传递参数传递(常用父向子传递)

发布于:2025-09-04 ⋅ 阅读:(15) ⋅ 点赞:(0)

现在,有子组件

            <MdsWxSourceDetail
              ref="mdsWx"
              :rank-obj="activeRankObj"
              :media-name="activeObj.mediaName"  
              :error-info="activeErrorInfo"       
            ></MdsWxSourceDetail>

以上代码在MdsIndexRankDetail,这个里面, 如果, 想传递一些参数, 比如某个变量的值

MdsWxSourceDetail其中的父组件是MdsIndexRankDetail

然后组件如何接受啊

  props: {
    rankObj: Object,
    mediaName: {
      type: String, 
      required: true,
    },
    errorInfo: {
      type: String,
      required: true,
    },
  },

然后组件如何用啊:

        const exportWordVo = {
          imageMap: imageMap ,
          areaGroup: this.areaGroup,
          areaGroupTop5: this.areaGroupTop5,
          gendersData: this.gendersData,
          agesData: this.agesData,
          buckData: this.buckData,
          basicData: basicData,
          accountData: accountData  
        };
        Util.httpDownloadJson(
          this.api.exportWordByVx,
          exportWordVo, 
          function (err) {
            if (err) {
              console.log('下载错误:', err);
            } else {
              console.log('Word 文档下载成功');
            }
          }
        );

这次遇到的问题:
async 这种异步的需要等到请求结束, 期间遇到了, 因为没有用到这种一部的, 数据没有返回

      const updatedImageMap = await this.exportChart(chartTypes);
      const mergedImageMap = { ...this.imageMap, ...updatedImageMap };

这…可以用this,复制数据时是没有问题的
vue2:

  this.$message.warning("导出正在进行中,请耐心等待。");
  this.$message.success("Word 文档下载成功"); // 下载成功提示
  this.$message.error("导出失败,请重试。"); // 导出失败提示

还有就是

   async saveSingleWord() {
      // 检查是否正在导出
      if (this.isExportLoading) {
        this.$message.warning("导出正在进行中,请耐心等待。");
        return; // 如果请求正在进行,直接返回
      }

      this.isExportLoading = true; // 设置为加载中

      try {
        // 提取完读率
        const completionRate = this.statisticInfo2.a.data.a3.value;

        const titleData = [
          {
            titleReport: this.mediaTitle,
            completionRate: completionRate  // 文章标题
          }
        ];

        // 基础数据(a.data)
        const aDataEntries = this.statisticInfo2.a.data;
        const baseData = Object.keys(aDataEntries).map(key => ({
          label: aDataEntries[key].label,
          value: aDataEntries[key].value,
        }));

        // 互动数据(b.data)
        const bDataEntries = this.statisticInfo2.b.data;
        const interData = Object.keys(bDataEntries).map(key => ({
          label: bDataEntries[key].label,
          value: bDataEntries[key].value,
        }));

        const imageData = { ...this.imageMap }; // 复制原有的 imageMap
        const chartTypes = ['spreadsource', 'geodis'];
        const updatedImageMap = await this.exportChart(chartTypes);
        const mergedImageMap = { ...this.imageMap, ...updatedImageMap };

        const SingleWordVo = {
          imageMap: mergedImageMap,
          statisticsData: this.statisticsData,
          spreadData: this.spreadData,
          dailyData: this.dailyData,
          interData: interData,
          baseData: baseData,
          titleData: titleData,
          reprintTableData: this.reprintData,
          area5Data: this.area5Data,
          mediaType: this.mediaType,
          hotWords: this.hotWords
        };
        // 使用 Promise 包装 httpDownloadJson
        await new Promise((resolve, reject) => {
          Util.httpDownloadJson(
            this.api.exportSingleWord,
            SingleWordVo,
            function (err) {
              if (err) {
                reject(err); // 触发错误
              } else {
                resolve(); // 成功
              }
            }
          );
        });

        this.$message.success("Word 文档下载成功"); // 下载成功提示
      } catch (err) {
        this.$message.error("导出失败,请重试。"); // 导出失败提示
      } finally {
        this.isExportLoading = false; // 请求结束,重置状态
      }
    },

关于浏览器的vue工具没有显示组件,vue2的版本是不能是最新的,最新的是vue3

AI补充部分:
这段文章已经提供了一个清晰的示例,展示了如何在 Vue 组件中传递和接收 props。以下是一些建议,可以帮助你补充和完善内容:

1. 代码示例的完整性

确保代码示例可以独立运行,添加相关的上下文和背景信息。例如,提供父组件 MdsIndexRankDetail 的部分代码,说明它的结构和用途。

2. 详细说明 Props

对于 props 的使用,可以更详细地解释每个 prop 的作用,以及如何在 MdsWxSourceDetail 中使用这些 props。例如:

computed: {
  // 使用 props 中的 mediaName
  formattedMediaName() {
    return this.mediaName.toUpperCase(); // 示例转换
  }
}

3. 处理 Props 的方法

展示如何在子组件中使用这些 props,比如在模板中渲染内容:

<template>
  <div>
    <h1>{{ mediaName }}</h1>
    <p>{{ errorInfo }}</p>
    <div v-if="rankObj">{{ rankObj.someProperty }}</div>
  </div>
</template>

4. 事件和数据流

如果有需要,说明如何通过 $emit 向父组件发送事件,以及如何处理这些事件。比如:

methods: {
  handleClick() {
    this.$emit('someEvent', { data: this.rankObj });
  }
}

5. 异步操作示例

exportWordVo 的使用上下文中,可以增加如何从 API 获取数据的示例,解释数据流和响应处理的逻辑。

6. 代码注释

在代码中添加注释,解释每一部分的目的和功能,使读者更容易理解。

7. 错误处理

在处理 HTTP 请求时,可以详细说明如何优雅地处理错误,并给出用户反馈的示例。

8. 组件的分离与重用

强调组件的分离和重用性,说明如何将 MdsWxSourceDetail 组件在其他地方复用。

示例补充

以下是一个补充的示例,展示了如何在子组件中使用 props 和事件:

<template>
  <div>
    <h1>{{ mediaName }}</h1>
    <p v-if="errorInfo">{{ errorInfo }}</p>
    <button @click="handleClick">触发事件</button>
  </div>
</template>

<script>
export default {
  props: {
    rankObj: Object,
    mediaName: {
      type: String,
      required: true,
    },
    errorInfo: {
      type: String,
      required: true,
    },
  },
  methods: {
    handleClick() {
      this.$emit('someEvent', { data: this.rankObj });
    },
  },
};
</script>

通过这些补充,文章将更加全面和易于理解。


网站公告

今日签到

点亮在社区的每一天
去签到