首页 > 解决方案 > 如何使用打字稿在 vuejs 中的数据中添加“文件”类型?

问题描述

我的代码:

interface Item {
  title: string;
  type: string;
  description: string;
}

data() {
  return {
    id: "",
    item: {} as Item,
    file: //Here
  };
}

您好,我正在寻找向我的 vuejs 数据添加文件类型。

我看到了这个可行的解决方案:

interface Item {
  title: string;
  type: string;
  description: string;
  file: File;
}

data() {
  return {
    id: "",
    item: {} as Item
  };
}

但我不想将我的数据文件添加到我的 Item 界面或对象中。

标签: typescriptvue.js

解决方案


您可以将interface( 或type) 写入数据本身,然后将file属性添加到其中。

interface Item {
  title: string;
  type: string;
  description: string;
}

interface State {
  id: string;
  item: Item;
  file: null | File;
}

data(): State {
  return {
    id: '',
    item: {
      title: '',
      type: '',
      description: '',
    },
    file: null,
  };
},

推荐阅读