vuejs前端使用proto

1.下载protoc.exe(bin目录下)并配置环境变量[https://link.juejin.im/?target=https%3A%2F%2Fgithub.com%2Fgoogle%2Fprotobuf%2Freleases]
2.在vue项目中使用node.js安装google-protobuf

npm install google-protobuf

3.准备好自己的.proto文件,注意要

//写明版本号,否则会在编译时前两行报错illegal token''
syntax = "proto3";//protobuf版本


//编译出现未定义错误时,注意import自己需要的proto文件
//如 google,protobuf.any is not defined
import "google/protobuf/Any.proto";

4.将myproto.proto文件放在protoc.exe的目录下,在此处cmd打开命令行,执行

protoc --js_out=import_style=commonjs,binary:. myproto.proto

同目录下生成.js包

5.在vue中使用

import proto from "@/proto/document_pb"
//在script中
let d =new proto.Document();
posted @ 2021-07-15 17:47  Acezhang  阅读(758)  评论(1编辑  收藏  举报