gpt4 book ai didi

angular - 如何在 firebase 云函数和 angular 之间共享 typescript 类

转载 作者:行者123 更新时间:2023-12-04 13:00:16 26 4
gpt4 key购买 nike

我创建了一个 Angular 项目,在其中初始化 Firebase Cloud Functions。我的文件夹变成这样:

project-root
node_modules
src
app
(angular components)
functions
node_modules
src
index.ts

我希望我的一些 typescript 类可以在 Cloud Functions 和 Angular 中使用,而无需在两个文件夹中进行复制,但我不知道该怎么做。

如果可能,我还希望他们共享相同的节点模块。

最佳答案

我找到了一个适合我需要的解决方案。

而不是在 functions 上方创建文件夹和 Angular app ,只需使用 functions通常并让 Angular 使用 functions 中的文件文件夹。

在 Angular 的 tsconfig.json

{
"compilerOptions": {
"paths": {
...
"@functions/*":["functions/src/*"]
},
...
}

在 Angular 的 ts 文件中
import { YourClass } from '@functions/your-class.ts'

最后的要求是让 functions使用 Angular 的 npm 依赖项。

转至 Angular 的 package.json , 将你需要的依赖复制到 functions/package.json .
//functions/package.json

{
"dependencies": {
...
"rxjs": "~6.4.0"
},
...
}

现在你可以使用像 rxjs 这样的依赖项在没有 npm install 的 Angular 中的云函数中它里面 functions .
//functions/src/index.ts

import { Observable, of } from 'rxjs';

关于angular - 如何在 firebase 云函数和 angular 之间共享 typescript 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58801733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com