'props' is not defined.

发布时间 2023-04-21 10:46:27作者: yjxQWQ

这个错误表明在组件中,变量 props 没有被定义。通常情况下,我们需要在组件选项中指定 props,并传递它们给 setup() 函数:

'props' is not defined.

export default defineComponent({
  name: "message-out",
  props: {
    image: String,
    time: Number,
    text: String,
    type: String,
    file: Object as () => Filepic,
    beforeRead: Boolean,
  },
  setup(props) { // 注意这里需要传递props
    const { t } = useI18n();
    const srcList = [props.file.url];
    return {
      formatTime,
      t,
      srcList,
    };
  }
});

在这个例子中,我们将 props 作为 setup() 函数的参数传递。这样可以让我们在组件中使用 props 中声明的属性。另外,由于使用了 TypeScript 的类型注解,我们需要将 Object 类型转换为 () => Filepic,以确保 file 属性正确地传递到组件中。

请注意,在 Vue.js 3.x 版本中,为了确保类型安全性和更好的开发体验,建议使用 TypeScript。另外,defineComponent 函数用来替代 Vue 2.x 中的 Vue.extend,用于定义组件。