typescript - vue 3 中的 Prop 在类基组件中不可访问

标签 typescript vue.js

我正在迁移到 vue 3。我无法访问我类的 Prop 值,而是它给了我 0 而不是在控制台中打印我在 Prop 上使用的最新值。在模板上它工作正常,但我想要我类(class)中 titleComponent 的最新值

import { Options, Vue } from "vue-class-component";
    
@Options({
  props: {
    titleComponent: Number
  }
})
    
export default class StorePin extends Vue {
  private titleComponent = 0;
    
  mounted()
  {
    console.log(this.propValue);
  }
 
  get propValue()
  {
    return this.titleComponent;
  }
   
  incremnt()
  {
    this.titleComponent ++ ;
  }
  
}

最佳答案

创建一个声明 Prop 的 Prop 类,然后使用 .with() 方法将其添加到组件中!这将为您提供强大的 TypeScript 支持,您甚至不必使用 “@vue/runtime-core” 库中的 PropType 接口(interface)。

<template>
  <p>Hello world</p>
</template>

<script lang="ts">
import { Vue, Options, prop } from 'vue-class-component';

class Props {
  array: string[] = prop({
    required: true,
  });
}

@Options({})
export default class MyComponent extends Vue.with(Props) {
}
</script>

<style lang="scss" scoped>
</style>

关于typescript - vue 3 中的 Prop 在类基组件中不可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65365232/

相关文章:

javascript - 更改 Angular 8 和 Angular Material 中的日期时间格式

typescript - 将您的类型与现有的 Typescript 合并

reactjs - typescript 中的 ChartJs 图表 : Object is possibly undefined in useEffect

javascript - vue.js 将数据从父单文件组件传递给子组件

javascript - Vue.js - v-show 和 v-for 不响应更改

typescript - 字符串或数字字典类型参数

Angular 2 - 一个组件触发页面上另一个组件的刷新

vue.js - 如何在 vue 中为外部后端 api 和 devserver api 设置 url

javascript - 将图像源传递到 Vue.js 中的作用域槽

ruby-on-rails - Rails + webpacker + vue : "You are using the runtime-only build of Vue where the template compiler is not available."