typescript - 如何使用 typescript 在 vuejs 中向我的数据添加 "File"类型?

标签 typescript vue.js

我的代码:

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 界面或对象中。

最佳答案

您可以为数据本身编写一个接口(interface)(或一个类型),然后只向它添加文件属性。

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,
  };
},

关于typescript - 如何使用 typescript 在 vuejs 中向我的数据添加 "File"类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61670153/

相关文章:

vue.js - 如何在保存时自动设置vscode格式Vue模板?

vue.js - 在 SASS 中使用 Vuetify 类

typescript 枚举值作为数组

Angular 5 将 ElementRef 转换回组件

vue.js - 如果我刷新我的网页,Vuex 存储为空

javascript - vue.js:带有组件的 TreeView

javascript - Vue.js 试图通过计算中的 ID 获取 DOM 元素返回 null

javascript - JS : Searching a list of objects inside another

javascript - 如何为对象中的数组赋值

javascript - 基于字符串创建 NGRX 操作的工厂