将组件道具传递给 Vue Typescript 中的对象

时间:2021-07-13 15:46:52

标签: typescript vue.js

我正在尝试将 vue 组件中的 props 值作为索引传递给对象,就像下面的代码一样。

export default defineComponent({
  props:{
    pollId:{type: String}
  },
  data(){
    return{
      poll: polls[this.pollId]
    }
  }
})

但是当我这样做时,我得到了这样的错误: error

编译器一直假设我的 props 是 undefined,即使我在上面指定了它。

有没有办法解决这个问题?不牺牲

1 个答案:

答案 0 :(得分:0)

其实就像在 props 中添加 required 属性一样简单

export default defineComponent({
  props:{
    pollId:{required:true,type: String}
  },
  data(){
    return{
      poll: polls[this.pollId]
    }
  }
})