ค้นหาเว็บไซต์

javascript - Nuxt.js กับ TypeScript: คุณสมบัติ 'x' ไม่มีอยู่ในประเภท 'y'


ฉันเป็นคนใหม่เกี่ยวกับ Vue และ Nuxt กำลังทำบทเรียนด้วย TypeScript อยู่ในขณะนี้ มันแสดงข้อผิดพลาดหลายข้อให้ฉันเกี่ยวกับ Property 'x' does not exist on type 'y'. ตัวอย่างด้านล่าง;

ERROR in components/AboutMe.vue:56:27
TS2339: Property 'routes' does not exist on type '{ components: { CButton: any; }; props: { user: { type: ObjectConstructor; default: undefined; }; }; data(): { expand: boolean; showTitle: boolean; showReadMore: boolean; routes: string[]; compiledBio: string; }; beforeMount(): void; mounted(): void; }'.
    54 |   },
    55 |   mounted() {
  > 56 |     this.showTitle = this.routes.every((r) => this.$route.name !== r)
       |                           ^^^^^^
    57 |     if (this.$route.name === `users-userSlug`) {
    58 |       this.expand = true
    59 |       this.showReadMore = false

อื่น ๆ ได้แก่ Property 'showTitle' does not exist, 'expand' และอื่น ๆ โดยพื้นฐานแล้วทุกอย่างที่มี this. จะทำให้เกิดข้อผิดพลาด

นี่คือบล็อก <script ของคอมโพเนนต์ AboutMe.vue

<script lang="ts">
import { CButton } from '@chakra-ui/vue'

export default {
  components: {
    CButton,
  },
  props: {
    user: {
      type: Object,
      default: undefined,
    },
  },
  data() {
    return {
      expand: false,
      showTitle: false,
      showReadMore: true,
      routes: [`users-userSlug-posts`, `users-userSlug`],
      compiledBio: ``,
    }
  },
  mounted() {
    this.showTitle = this.routes.every((r) => this.$route.name !== r)
    if (this.$route.name === `users-userSlug`) {
      this.expand = true
      this.showReadMore = false
    }
  },
}
</script>

ฉันทำอะไรผิด?

ฉันกำลังใช้ nuxt.js (เวอร์ชัน 2.14.6)

แก้ไข 1 สำหรับ @BoussadjraBrahim

ฉันได้เพิ่ม Vue.extend({}) และตอนนี้ข้อผิดพลาดส่วนใหญ่หายไปแล้ว แต่บางส่วนยังคงมีอยู่

ERROR in components/Description.vue:138:10
TS2339: Property 'showAboutUs' does not exist on type 'CombinedVueInstance<Vue, unknown, unknown, unknown, Readonly<{ post: any; }>>'.
    136 |   },
    137 |   mounted() {
  > 138 |     this.showAboutUs = this.$route.name !== `users-userSlug-posts`
        |          ^^^^^^^^^^^
    139 |   },
    140 |   methods: {
    141 |     open() {

ERROR in components/Description.vue:142:12
TS2339: Property 'isOpen' does not exist on type 'CombinedVueInstance<Vue, unknown, unknown, unknown, Readonly<{ post: any; }>>'.
    140 |   methods: {
    141 |     open() {
  > 142 |       this.isOpen = true
        |            ^^^^^^
    143 |     },
    144 |     close() {
    145 |       this.isOpen = false
export default Vue.extend({
  data() {
    const showAboutUs: Boolean = false
    const isOpen: Boolean = false

    return {
      showAboutUs,
      isOpen,
    }
  },
  mounted() {
    this.showAboutUs = this.$route.name !== `users-userSlug-posts`
  },
  methods: {
    open() {
      this.isOpen = true
    },
    close() {
      this.isOpen = false
    },
  },
})

คำตอบ #1

ในการรับการคาดเดาประเภท คุณควรสร้างคอมโพเนนต์โดยใช้ Vue.extend({}):

<script lang="ts">
import { CButton } from '@chakra-ui/vue'

import  Vue from "vue"

export default Vue.extend({
  components: {
    CButton,
  },
  props: {
    user: {
      type: Object,
      default: undefined,
    },
  },
  data() {
    return {
      expand: false,
      showTitle: false,
      showReadMore: true,
      routes: [`users-userSlug-posts`, `users-userSlug`],
      compiledBio: ``,
    }
  },
  mounted() {
    this.showTitle = this.routes.every((r) => this.$route.name !== r)
    if (this.$route.name === `users-userSlug`) {
      this.expand = true
      this.showReadMore = false
    }
  },
})
</script>

ฉันแนะนำให้กำหนดประเภทของข้อมูลของคุณเพื่อบังคับใช้การกำหนดประเภทของคอมโพเนนต์:

 data() {
    const routes:Array<string>: [`users-userSlug-posts`, `users-userSlug`];
  // do the same thing with the other properties
    return {
      expand: false,
      showTitle: false,
      showReadMore: true,
      routes,
      compiledBio: ``,
    }
  },

คำตอบ #2

ลองใช้ Vue.extend({})

ตัวอย่าง :

<script lang="ts">
import { CButton } from '@chakra-ui/vue'
import  Vue from "vue"

export default Vue.extend({
  components: {
    CButton,
  },
  props: {
    user: {
      type: Object,
      default: undefined,
    },
  },
...
</script>

หวังว่านี่จะช่วยคุณได้

บทความที่เกี่ยวข้อง