2016-09-28 36 views
1

我是protobuf的新手。 我已經安裝了npm google-protobuf。如何導入protoc生成的javascript?

以下是我.proto文件

syntax = "proto3"; 
package com.sixdee; 
message Student{ 
     string name = 1; 
     int32 id = 2; 
} 

這該是多麼我已經生成的js文件

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

我已經貼在我的項目所產生的testproto_pb.js。 我無法構建protobuf數據包。 我試圖

var student = new Student(); 
student.setName("Ankith"); 
student.setId(24); 

我得到Uncaught ReferenceError: Student is not defined

我剛纔提到link。沒有什麼似乎爲我工作。 任何幫助深表謝意。

+0

如果你使用CommonJS風格的導入,那麼我認爲你會想要做這樣的事情:var testproto_pb = require('./ testproto_pb.js'); var student = new testproto_pb.Student(); ... 那樣有用嗎? –

回答