typescript 具有 Angular 6 的 GRPC Web 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51857225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:35:17  来源:igfitidea点击:

GRPC web client with angular 6

angulartypescriptgrpcgrpc-web

提问by Asad Shah

I have worked with grpc .net client and a grpc server created with java, how can i implement grpc web client on angular 6 with typescript? Also how can i create proto files and it's typing's for typescript? I am following thisrepo but not able to generate proto files.

我已经使用过 grpc .net 客户端和用 java 创建的 grpc 服务器,我如何使用打字稿在 angular 6 上实现 grpc web 客户端?另外,我如何创建 proto 文件并为打字稿打字?我正在关注repo,但无法生成 proto 文件。

采纳答案by Asad Shah

After spending sometime i was able to create proto files for typescript by following steps:

花了一些时间后,我能够通过以下步骤为打字稿创建原型文件:

  1. Download protobuf for windows from this link. After extracting files set the path variable for protoc.exe
  2. install npm packages npm install google-protobuf @types/google-protobuf grpc-web-client ts-protoc-gen --save
  3. After installing it generate the typescript files by using the command: protoc --plugin="protoc-gen-ts=absolute-path-to-your-project\node_modules\.bin\protoc-gen-ts.cmd" --js_out="import_style=commonjs,binary:${OUT_DIR}" --ts_out="service=true:${OUT_DIR}" your.proto
  4. Finally consume it like as mentioned in this repo.
  1. 从此链接下载适用于 Windows 的 protobuf 。解压文件后为 protoc.exe 设置路径变量
  2. 安装 npm 包 npm install google-protobuf @types/google-protobuf grpc-web-client ts-protoc-gen --save
  3. 安装后,使用以下命令生成打字稿文件: protoc --plugin="protoc-gen-ts=absolute-path-to-your-project\node_modules\.bin\protoc-gen-ts.cmd" --js_out="import_style=commonjs,binary:${OUT_DIR}" --ts_out="service=true:${OUT_DIR}" your.proto
  4. 最后像这个repo 中提到的那样使用它。

回答by Kim T

You can also get it to work by running:

你也可以通过运行来让它工作:

npm install google-protobuf protoc ts-protoc-gen

Then add a compile script to your package.json:

然后在你的 package.json 中添加一个编译脚本:

"compile": "./node_modules/protoc/protoc/bin/protoc --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --js_out=import_style=commonjs,binary:src/app/proto-gen --ts_out=service=true:src/app/proto -I ./src/app/proto ./src/app/proto/**/*.proto",

now you can compile the .proto files to a Service using:

现在您可以使用以下命令将 .proto 文件编译为服务:

npm run compile

You can see the approach working here:

您可以在此处查看该方法:

https://github.com/kmturley/angular-nest-grpc

https://github.com/kmturley/angular-nest-grpc

回答by Wenbo Zhu

grpc/grpc-web is available at https://github.com/grpc/grpc-web

grpc/grpc-web 可在https://github.com/grpc/grpc-web 获得

回答by Tomá? Moskala

Cannot use on on Windows

无法在 Windows 上使用

"compile": "./node_modules/protoc/protoc/bin/protoc
--plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --js_out=import_style=commonjs,binary:src/app/proto-gen --ts_out=service=true:src/app/proto -I ./src/app/proto ./src/app/proto/**/*.proto",

I get '.' is not recognized as an internal or external command

我得到'.' 不被识别为内部或外部命令

This works fine: package.json

这工作正常:package.json

"scripts": {
"compile": "node_modules\protoc\protoc\bin\protoc --plugin=protoc-gen-ts=node_modules\.bin\protoc-gen-ts.cmd --js_out=import_style=commonjs,binary:./proto/generated --ts_out=service=grpc-web:./proto/generated -I ./proto ./proto/*.proto"
}

npm run compile

npm 运行编译