I searched about this but I didn't find anything specific for what I need. If there is one, please, share here.
I'm trying to create a generic service to be called in various components. Since it's a function that requests data from an external source, I need to treat it as an asynchronous function. Problem is, the editor returns the message "'await' has no effect on the type of this expression". And the app indeed crashes since there is no …
我正在构建一个 Node 和 React 应用程序,两者都使用 TypeScript。其目录树如下:
我的问题:
由于我对两个堆栈使用相同的语言,并且将来也将使用 TypeScript 添加 React Native,所以我想知道如何创建一组用于所有堆栈的类。
为什么我想这样做:
DRY(不要重复):我的目的是充分利用在所有层中使用相同的编程语言,因此创建两个相同的类是没有意义的。
到目前为止我已经尝试过:
我创建了第三个名为“util”的文件夹,并放置了一个通用类,只是为了使用它来测试 Node 和 React。像这样:
在 Node.js 中,我使用以下命令导入它:
import Person from "../../util/person.class";
Run Code Online (Sandbox Code Playgroud)
在 React.js 中,我使用相同的逻辑来导入它:
import Person from "../../util/person.class";
Run Code Online (Sandbox Code Playgroud)
正如我所预料的,两者都拒绝使用各自根文件夹之外的文件:
我还在互联网上搜索了相关内容,发现了一些“弹出”命令,一旦使用,就无法返回。我想避免这种方式。有什么对我有利的方法吗?
我还想提一下,我使用“tsc --init”为后端创建了一个 tsconfig.json,并将 rootDir 设置为“./src/”,将 outputDir 设置为“./dist/”。
谢谢。
如何循环 Axios 请求以填充对象数组?
我正在尝试创建一个使用名为 swapi 的星球大战 API 的 Web 界面。可以在此链接中访问:https://swapi.co/
我可以搜索人物、星舰、行星、物种和电影。因此,例如,如果我想搜索 Luke Skywalker,我会添加/api/people/1/like https://swapi.co/api/people/1/。
到现在为止还挺好。
现在,如果我想要每个星球大战角色,我只需删除数字 1,网址将是https://swapi.co/api/people/,我将拥有 87 个字符中的前十个字符,我想。
但我不想要 10 个。我想要所有这些……这让我做了一个多次请求来获取所有字符数据。问题是,如果我使用循环来发出请求,假设是一个 while 循环,它会在第一个请求将数据带到我的代码以对其进行处理之前完成所有搜索。
我正在通过 Redux 使用全局状态,并通过componentDidMount(). 它工作正常,实际上,前十个字符数据被检索,它们填充我在 React 中的渲染方法。但是如何在没有在这些请求之前结束的循环的情况下发出多个请求?
reactjs ×3
javascript ×2
api ×1
async-await ×1
axios ×1
json ×1
loops ×1
node.js ×1
typescript ×1