简体   繁体   English

属性不存在

[英]Property doesn't exist

ok may be a simple one but this has me puzzled好的可能是一个简单的,但这让我很困惑

i have an SFC defined using我有一个使用定义的 SFC

export default defineComponent({
    setup(): { args: Args; preview: Point[] } {
        const args = new Args();
        const preview: Point[] = [];
        args.values.push(...args.shape.shapeArgs.map((d) => d.default));
        return { args, preview: preview };
    },
    computed: {
        previewSize() {
            return {
                width: (Math.max(1, ...this.preview.map((p) => p.x)) + 40) * 10,
                height: (Math.max(1, ...this.preview.map((p) => p.y)) + 40) * 10,
            };
        },
...

however it wont run because但是它不会运行,因为

TS2339: Property 'preview' does not exist on type 'ComponentPublicInstance<{} | {}, {}, {}, {}, {}, EmitsOptions, {} | {}, {}, false, ComponentOptionsBase<{} | {}, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, EmitsOptions, string, {}>>'.
  Property 'preview' does not exist on type '{ $: ComponentInternalInstance; $data: {}; $props: {} | {}; $attrs: Record<string, unknown>; $refs: Record<string, unknown>; $slots: Readonly<InternalSlots>; ... 7 more ...; $watch(source: string | Function, cb: Function, options?: WatchOptions<...> | undefined): WatchStopHandle; } & ... 4 more ... & ComponentCustom...'.
    56 |        previewSize() {
    57 |            return {
  > 58 |                width: (Math.max(1, ...this.preview.map((p) => p.x)) + 40) * 10,
       |                                            ^^^^^^^                          

as preview clearly is defined i'm puzzled as to why it doesn't exist由于明确定义了预览,我很困惑为什么它不存在

This is from the Vue3 typescript documentation about computed这是来自关于计算的 Vue3 typescript 文档

import { defineComponent, ref, computed } from 'vue'

export default defineComponent({
  name: 'CounterButton',
  setup() {
    let count = ref(0)

    // read-only
    const doubleCount = computed(() => count.value * 2)

    const result = doubleCount.value.split('') // => Property 'split' does not exist on type 'number'
  }
})

You may try to do something like:您可以尝试执行以下操作:

export default defineComponent({
    setup(): { args: Args; preview: Point[] } {
        const args = new Args();
        const preview: Point[] = [];
        args.values.push(...args.shape.shapeArgs.map((d) => d.default));
        const previewSize = computed(() => ({
                width: (Math.max(1, ...this.preview.map((p) => p.x)) + 
                40) * 10,
                height: (Math.max(1, ...this.preview.map((p) => p.y)) + 
                40) * 10,
            }))
        return { args, preview: preview, previewSize: previewSize };
    }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM