简体   繁体   English

无法读取未定义的属性,Typescript

[英]Cannot read property of undefined, Typescript

I'm having issues with creating a class function with optional parameters.我在使用可选参数创建 class function 时遇到问题。 I'm plagued with the following error and since I'm a new comer to typescript I'm not really sure as to what I have to change in my code to fix it.我被以下错误所困扰,因为我是 typescript 的新手,所以我不确定我必须在我的代码中更改什么来修复它。 This code works perfectly fine in an earlier project written in JS only.此代码在仅用 JS 编写的早期项目中运行良好。

It specifically highlights the getRequestOptions, when I go check it in base-service.ts in the browser error.它特别突出了 getRequestOptions,当我 go 在浏览器错误的 base-service.ts 中检查它时。

08:32:12.755 client.ts:22 [vite] connecting...
08:32:13.067 client.ts:52 [vite] connected.
08:32:17.043 locales-service.ts:7 getting locales
08:32:17.048 base-service.ts:19 get /Api/GetLocales/ undefined undefined undefined
08:32:17.288 base-service.ts:21 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getRequestOptions')
    at request (base-service.ts:21)
    at getLocales (locales-service.ts:9)
    at setup (locale-selector.vue:22)
    at callWithErrorHandling (runtime-core.esm-bundler.js:6737)
    at setupStatefulComponent (runtime-core.esm-bundler.js:6346)
    at setupComponent (runtime-core.esm-bundler.js:6302)
    at mountComponent (runtime-core.esm-bundler.js:4224)
    at processComponent (runtime-core.esm-bundler.js:4199)
    at patch (runtime-core.esm-bundler.js:3791)
    at mountChildren (runtime-core.esm-bundler.js:3987)

And my code还有我的代码

base-service.ts基础服务.ts

    import axios from '@/plugins/axios'

    export default class BaseService {
    getRequestOptions(url:any, method:any, data?:any, params?:any , customHeaders?:any) {
      const headers = {
        ...customHeaders
      }

      return {
        url:url,
        method:method,
        data: data,
        params: params,
        headers: headers,
      }
    }

    async request(method:any, url:any, data?:any, params?:any, headers?:any) {
        console.log(method,url,data,params,headers)

        const options = this.getRequestOptions(method, url, data, params, headers)

        try {
            console.log(options)
            const response = await axios(options)
            console.log("Getting response from api")
            console.log(response)
            if (response.status === 200) {
                return response.data
            }
        } catch (error) {
            console.log(error)
        }
    }
}

locales-service.ts语言环境-service.ts

import BaseService from '../base-service'
//?Import models in the future
//import {  } from '@/common/models'

class LocaleServ extends BaseService {
    async getLocales() {
        console.log("getting locales")

        let result = await super.request(
                'get',
                '/Api/GetLocales/'
                )
        
        console.log(result)
        return result
    }
}

export const LocaleService = new LocaleServ()

Any help would be greatly appreciated任何帮助将不胜感激

Edit:编辑:

<script lang="ts">
import { ref } from 'vue'
import { defineComponent } from 'vue'
import CountryFlag from 'vue-country-flag-next'
import { LocaleService } from '@/services'

export default defineComponent({
    name: 'LocaleSelector',
    components: {
        CountryFlag
    },

    setup () {
        const { getLocales } = LocaleService
        const data = getLocales()

        return {
            data:data
    }
  }
})
</script>

And then in the component I call it with {{data}}然后在组件中我用 {{data}} 调用它

The issue lied in the way I called the destructured getLocales() method from LocaleService in locale-service.vue问题在于我从 locale-service.vue 中的 LocaleService 调用解构的getLocales()方法的方式

I noticed that this was undefined after attaching a debugger.我注意到附加调试器后thisundefined的。 According to this article , destructuring it caused it to lose it's context.根据这篇文章,解构它会导致它失去它的上下文。 Therefore when I called getLocales() in base-service.ts, this was undefined .因此,当我在 base-service.ts 中调用getLocales()时, thisundefined

Simple work around to fix the, caused due to inexperience, problem was to turn.简单的解决方法来解决,由于缺乏经验,问题是转过来的。

const { getLocales } = LocaleService
    const data = getLocales()

into进入

const data = LocaleService.getLocales()

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

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