vue3 类组件访问道具

问题描述

我在打字稿中使用带有类组件的 vue3 我的类看起来像:

import {Options,Vue} from "vue-class-component";

@Options({
  props: {
    result: Object
  }
})


export default class imageResult extends Vue {
  currentimage = 0;

  getSlides(){
    console.log('result',this.$props.result); // not working
    console.log('result',this.result); // not working too
  }

我的问题是,我如何在班级中访问和使用该属性

this.resultthis.$props.result 都会给我一个错误

有人可以帮我吗? 提前致谢

解决方法

我对您的建议是遵循使用类组件在 vue 中使用 Typescript 的文档:enter link description here

为了修复您的代码,我认为这应该可行:

import {Vue} from "vue-class-component";
import {Component} from "vue-class-component";

// Define the props by using Vue's canonical way.
const ImageProps = Vue.extend({
  props: {
    result: Object
  }
})

// Use defined props by extending GreetingProps.
@Component
export default class ImageResult extends ImageProps {
  get result(): string {
    console.log(this.result);
    // this.result will be typed
    return this.result;
  }
}