如何使用打字稿文件

时间:2017-08-11 03:03:42

标签: typescript vuejs2 vue-component

我正在从angular转向vue并试图将'service'作为一个简单的typescript类实现。我想知道我会怎么做,目前我有:

import axios from 'axios'
import keys from 'libs/keys/api-keys'

export default class Google {

    textSearch(query: string, radius = 5000) {
        let url = `https://maps.googleapis.com/maps/api/place/textsearch/json?radius=${radius}&query=${query}` +
            `&key=${keys.googleApiKey}`

        return axios.get(url)
    }
    getPhoto(photoReference: string, maxwidth = 1600) {
        let url = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=${maxwidth}` +
            `&photoreference=${photoReference}&key=${keys.googleApiKey}`

        return axios.get(url)
    }
}

作为我的班级。然后我尝试将其导入我的vue组件:

import google from 'src/libs/location/google'
google.textSearch(params.location)

但是我收到了错误:

Property 'textSearch' does not exist on type 'typeof Google'

所以我尝试在课前抛出一个默认界面,但仍然得到同样的错误:

import axios from 'axios'
import keys from 'libs/keys/api-keys'

export default interface Google {
    textSearch(query: string, radius?: number): void
}

export default class Google {

    textSearch(query: string, radius = 5000) {
        let url = `https://maps.googleapis.com/maps/api/place/textsearch/json?radius=${radius}&query=${query}` +
            `&key=${keys.googleApiKey}`

        return axios.get(url)
    }
    getPhoto(photoReference: string, maxwidth = 1600) {
        let url = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=${maxwidth}` +
            `&photoreference=${photoReference}&key=${keys.googleApiKey}`

        return axios.get(url)
    }
}

这样做的正确方法是什么?类型是否必须在外部.d.ts文件中?如果是这样,打字稿将如何在导入时推断出类型。

0 个答案:

没有答案