js c++ vue方法與數(shù)據(jù)交互通信示例
引言
最近寫了一個小需求,其中涉及到js調用vue中函數(shù)、js與c++相互調用、數(shù)據(jù)傳輸?shù)葐栴},希望這里可以幫助到有需要的小伙伴兒,個人記錄,僅供參考。
js調用vue中methods內的方法
在vue的鉤子函數(shù)mounted中將需要在js中調用的函數(shù)賦值給window,暴露給js
mounted() {
let that = this;
// 將Vue方法傳到全局對象window中
window.initDataAction = that.initDataAction
},
methods: {
// 獲取數(shù)據(jù)源
initDataAction(data ) {
console.log(data)
}
}
在js中直接調用initDataAction函數(shù),這里的函數(shù)中也可以傳遞對應數(shù)據(jù)。
<script type="text/javascript"> let data = [] // 調用獲取數(shù)據(jù)源函數(shù) initDataAction(data) </script>
js與c++間的通信
js 請求 c++,觸發(fā)c++吊起js中的函數(shù)并傳遞參數(shù)
// 獲取文檔類型數(shù)據(jù)源
initDataAction() {
// js 請求 c++
if (window.chrome.webview) {
window.chrome.webview.postMessage(`getEmplates`);
}
}c++ 觸發(fā)js函數(shù),并傳遞res參數(shù)
(function(window, document, undefined) {
"use strict";
window.DomUtils = {
// c++ 觸發(fā) res為返回數(shù)據(jù)
getEmplates: function(res) {
// 這里調用js中的函數(shù)
}
})(window, document)js與vue間數(shù)據(jù)傳輸
js與vue間數(shù)據(jù)傳輸可以使用本地存儲localStorage.setItem,不過這里要注意監(jiān)聽setItem值的變化及時刷新內存或是頁面數(shù)據(jù)。另一種是js中調用vue暴露給window的函數(shù)傳參,這種傳參不需要特別監(jiān)聽數(shù)據(jù)變化,只要調用方法就可以實現(xiàn)新數(shù)據(jù)的傳輸。
mounted() {
let that = this;
window.addEventListener("setItemEvent",function(e){
//e.key : 是值發(fā)生變化的key
//例如 e.key==="token";
//e.newValue : 是可以對應的新值
if(e.key==="token"){
const templateData = e.newValue
}
})
},以上就是js c++ vue方法與數(shù)據(jù)交互通信示例的詳細內容,更多關于js c++ vue方法數(shù)據(jù)交互的資料請關注腳本之家其它相關文章!
相關文章
JavaScript?API?ResizeObserver使用示例
這篇文章主要為大家介紹了JavaScript?API?ResizeObserver的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
詳解requestAnimationFrame和setInterval該如何選擇
這篇文章主要為大家介紹了requestAnimationFrame和setInterval該如何選擇示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>2023-03-03
document 和 document.all 分別什么時候用
document 和 document.all 分別什么時候用...2006-06-06

