色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

圖文詳解vue中proto文件的函數調用

瀏覽:8日期:2023-02-10 18:17:12
1、編譯proto

在src文件夾下新建proto文件夾用以存放所有的.proto文件。在proto文件夾下打開終端,輸入如下命令:

//進入proto文件夾執行下列編譯,將helloworld.proto替換為當前的.proto文件名protoc -I=. helloworld.proto --js_out=import_style=commonjs,binary:. --grpc-web_out=import_style=commonjs,mode=grpcwebtext:.

一個.proto文件(helloworld.proto)編譯后生成2個js文件:

helloworld_pb.js helloworld_grpc_web_pb.js2、編譯后的proto文件中變量及函數

.proto中函數的結構,主要由函數及參數2部分組成:

service Greeter{ rpc AddEmployee(Employee) returns (EmployeeID) {} // 提交員工信息一元消息}//發送請求的數據類型結構message Employee{ string name = 1; int32 age = 2;}//返回函數處理結果的類型結構message EmployeeID{ int32 id = 1;}

函數部分

編譯之后,名稱為“service Greeter”的服務及函數AddEmployee的定義在helloworld_grpc_web_pb.js文件中:

圖文詳解vue中proto文件的函數調用

圖文詳解vue中proto文件的函數調用

參數部分

Employee及EmployeeID的參數定義在helloworld_pb.js中:

1、發送請求的參數Employee

Employee的第一個參數name 函數形式如下(此處是請求參數,使用set格式):

圖文詳解vue中proto文件的函數調用

Employee的第二個參數age函數形式如下(此處是請求參數,使用set格式):

圖文詳解vue中proto文件的函數調用

2、返回結果參數EmployeeID

EmployeeID返回結果只有id這一個參數,函數結構如下(此處是返回參數,使用get格式):

圖文詳解vue中proto文件的函數調用

調用proto中的函數

一個簡單的調用示例如下(點擊button按鈕,產生一個單擊事件get_helloworld):

<el-button type='primary' @click='get_helloworld'> hello_world</el-button>

get_helloworld() { this.client = new GreeterClient('http://192.168.10.102:8181', null, null); // 創建請求參數并賦值 var request = new Employee(); request.setName('World'); request.setAge(11); // 調用客戶端相應的grpc方法,發送grpc請求,并接受后臺發送回來的返回值 this.client.addEmployee(request, {'my-service-header': 'test_service'}, (err, response) => {if (err) { console.log(`Unexpected error for addEmployee: code = ${err.code}` +`, message = '${err.message}'` );} else { console.log(response.getId()); // 打印返回的信息} });},

此時可以在控制臺中看到夠返回的ID數值。

將返回結果顯示在界面中

函數的返回結果都要以合適的形式展示在界面的控件中,此處以:

1、table控件

table控件是使用比較頻繁的數據展示控件,此處示例proto代碼如下(返回列表數據格式,且包含枚舉變量):

rpc SelectAllCameras(SelectAllCamerasRequest) returns(SelectAllCamerasResponse){}// 查詢所有攝像機設備message SelectAllCamerasRequest{ int32 page_index = 1; int32 page_size = 2; string condition = 3;}//返回查詢結果,返回一個CameraInfo 的數組,CameraInfo 中又包含枚舉類型CameraBrandmessage SelectAllCamerasResponse{ CodeErr enumErrorNo = 1; repeated CameraInfo cameraArray = 2;}// 攝像機信息message CameraInfo{ string szCameraUID = 1; // uid string szName=2; // 名稱 東門口攝像機 CameraBrand enumCameraBrand=3; // 品牌}// 攝像機品牌enum CameraBrand { DEFAULT_CAMERA_BRAND = 0; HIKI_VISION= 1; DAHUA = 2; UNIVIEW = 3;}

1、導入頭文件

import { device_register_serviceClient } from '../proto/device_manage_grpc_web_pb';import { SelectAllCamerasRequest,} from '../proto/device_manage_pb';

<el-table :data='caminfoTable' ref='caminfoTable' > <el-table-column type='index' :index='table_index' label='序號' width='50'></el-table-column> <el-table-column prop='UID' label='UID' align='center'> <template slot-scope='scope'> <span>{{scope.row.getSzcamerauid()}}</span> </template> </el-table-column> <el-table-column prop='szName' label='相機名' align='center'> <template slot-scope='scope'> <span>{{scope.row.getSzname()}}</span> </template> </el-table-column> <el-table-column prop='enumCameraBrand' label='相機品牌' align='center'> <template slot-scope='scope'> <span>{{CameraBrand[scope.row.getEnumcamerabrand()].label}}</span> </template> </el-table-column></el-table>

//將返回結果賦值給一個數組變量caminfoTable:[],//攝像機品牌,這里的CameraBrand是用在添加相機信息時,下拉框選項內容的填充,此處也用來顯示具體數據CameraBrand: [ {value:0, label:'默認'}, { value: 1, label: '海*' }, { value: 2, label: '大*' }, { value: 3, label: '宇*' },],

//獲取相機設備的信息get_camerainfo_data(){ this.client = new device_register_serviceClient('http://192.168.10.102:8181', null, null); var request_selectallCam = new SelectAllCamerasRequest(); request_selectallCam.setPageIndex(this.Pagination_queryInfo.page_index); request_selectallCam.setPageSize(this.Pagination_queryInfo.per_page); this.client.selectAllCameras(request_selectallCam,{'my-service-header': 'dev_manage_service'},(err,response)=>{ if(err){console.log( `Unexpected error for selectAllCameras: code = ${err.code}` + `, message = '${err.message}'` ); }else{ var caminfoList = response.getCameraarrayList(); this.Pagination_total_pages=caminfoList.length; //求取頁碼總數 this.caminfoTable = caminfoList; //將返回結果賦值給table數據表變量 } }); //調整頁碼的顯示為第一頁 this.Pagination_queryInfo.page_index=1; },

圖文詳解vue中proto文件的函數調用

總結

到此這篇關于vue中proto文件函數調用的文章就介紹到這了,更多相關vue proto文件函數調用內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Vue
相關文章:
主站蜘蛛池模板: 久草视频精品 | 亚洲欧美高清 | 欧美一级免费 | 免费成年人在线观看视频 | 亚洲图片在线视频 | 日韩中文字 | 99视频精品全国在线观看 | 在线视频免费观看a毛片 | 国产三级日本三级美三级 | 免费观看呢日本天堂视频 | 嫩小性性性xxxxbbbb | 99精品视频在线 | 久久久久久福利 | 国产成人综合网在线播放 | 中文字幕一区二区三区 精品 | 国产区亚洲区 | 成人免费午间影院在线观看 | 欧美视频自拍偷拍 | 久久久久免费 | 欧美成人免费观看 | 欧美jlzz18性欧美 | 免费中国一级啪啪片 | 欧美一级欧美一级在线播放 | 亚洲人成网站在线观看播放 | 亚洲精品国产成人专区 | 国产成人精品日本亚洲专 | 欧美一级级毛片 | 欧美一级三级 | 日本理论片免费高清影视在线观看 | 看成年女人免费午夜视频 | 免费一级毛片女人图片 | 在线看欧美日韩中文字幕 | 精品一久久香蕉国产线看观 | 国产一级在线 | 国产麻豆一级在线观看 | 美女被免费网站在线视频软件 | 久久爱一区 | 久久极品 | 日本免费a级片 | 精品视频 久久久 | 日本精品一区二区三区视频 |