如何在 Typescript 中使用 Ziggy 包

Anw*_* Bo 4 routes node.js laravel typescript axios

因此,我想创建一个使用 axios 检索数据的 Typescript 类,但我无法找到使用route()ziggy 在我的 Typescript 类中提供的函数的方法,因为它不是 npm 包。我对 Node.js 还是很陌生。任何帮助是极大的赞赏。

import axios from 'axios';

export class Action {
    action: object;

    constructor(action_id: number) {
        axios.get(route('route_name', {action: action_id})).then(response => {
            this.action = response.data;
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

这就是我正在努力实现的目标。Typescript 给出一个错误,指出该route()函数无法识别。

小智 8

只需在导出类之前声明该函数即可:

import axios from 'axios';

declare function route(name:string, params?: any);

export class Action {
    action: object;

    constructor(action_id: number) {
        axios.get(route('route_name', {action: action_id})).then(response => {
            this.action = response.data;
        });
    }

}
Run Code Online (Sandbox Code Playgroud)