【问题标题】:VueJS: Obtaining dynamic component information in method run via v-bindVueJS:通过v-bind获取方法run中的动态组件信息
【发布时间】:2018-10-12 02:19:16
【问题描述】:

我相信这是一个相对独特的问题,因此我很难尝试解决它。​​

我正在 Vue 中创建类似文件管理器的解决方案,并且我正在寻找某些文件夹/文件以显示唯一的缩略图(在我的示例中,如果找到“Creative Cloud”文件夹,则显示 Creative Cloud 徽标)。在我的应用中,我使用一个组件来表示每个文件。

file-grid Vue 文件的读取方式是这样的(对不起,混乱,我一直在尝试集成多种不同的解决方案,看看有什么效果):

<template>
  <div id="localMain">
    <div id="filesGrid">
      <File :fileName="file"
      :imageAddress="findImage($event)" 
      id="file" 
      v-for="file in files" 
      :key="file.id"></File>
    </div>
  </div>
</template>

<script>
import File from './LocalMain/File';

export default {
  data() {
    return {
      creativeCloud: 'static/logos/creative-cloud.svg',
      blankThumb: 'static/code.svg',
      files: [
        'Creative Cloud',
        'Documents',
        ...
      ],
    };
  },
  components: {
    File,
  },
  methods: {
    findImage: function findImage(e) {
      /* Get the name of the file/folder, and choose a thumbnail accordingly */
      const name = e.target.dataset.fileName;
      let image = this.blankThumb;
      if (name === 'Creative Cloud') {
        image = this.creativeCloud;
      } else {
        image = this.blankThumb;
      }
      return image;
    },
  },
};
</script>

<style scoped>
/* styling */
</style>

文件组件本身如下所示:

<template>
  <div id="file">
    <img :src="imageAddress" alt="Logo" id="fileImg" />
    <h3 v-if="display">{{ fileName }}</h3>
  </div>
</template>

<script>
export default {
  data() {
    return {
      display: false,
    };
  },
  props: {
    fileName: String,
    imageAddress: String,
  },
};
</script>

<style scoped>
/* styling */
</style>

对于这个问题的含糊之处,我深表歉意,但我很困惑。

【问题讨论】:

  • 所以...问题是??
  • 事件从何而来?
  • @JacobGoh 抱歉,我有点匆忙把这些都打完了,漏掉了一个关键细节。我收到关于“$event”的错误。
  • @CUGreen 我在网上看到的东西。再次,我很抱歉造成混乱,因为老实说我也不知道我在这里做什么。

标签: javascript vue.js vuejs2 vue-component vue-directives


【解决方案1】:

我可能遗漏了一些东西,但为什么不直接用文件名作为参数 v-bind 方法呢?

例如。

父模板

<File :fileName="file"
  :imageAddress="findImage(file)" 
  id="file" 
  v-for="file in files" 
  :key="file.id"></File>

父 Javascript

findImage: function findImage(name) {

  var image = this.blankThumb;
  if (name === 'Creative Cloud') {
    image = this.creativeCloud;
  }
  return image;
},  

【讨论】:

  • 我对自己没有想到这个简单的解决方案感到失望。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-02
  • 2021-04-25
  • 2016-12-14
  • 2016-06-16
  • 2022-08-03
  • 2018-06-25
  • 2020-06-17
相关资源
最近更新 更多