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();