關于android 集成weex 加載js 時報ReferenceError: window is not defined
問題描述
如題將.we 文件npm run build 后將js拷貝到android asset文件下面,按照官方的集成步驟運行android studio 頁面空白 控制臺報 04-11 11:48:50.946 3321-3345/com.weex.sample E/jsengine: ReportException :undefined:2212: ReferenceError: window is not defined 04-11 11:48:50.949 3321-3345/com.weex.sample E/jsengine: ReportException : ReferenceError: window is not defined
at Object.exports.getBaseUrl (eval at Cn ((weex):2:4300), <anonymous>:2212:21) at Yt.created (eval at Cn ((weex):2:4300), <anonymous>:104:26) at (weex):1:27665 at Array.forEach (native) at Yt.zt [as $emit] ((weex):1:27643) at new Yt ((weex):1:29107) at ln ((weex):2:98) at a ((weex):2:2870) at Object.module.exports.type (eval at Cn ((weex):2:4300), <anonymous>:67:2) at __webpack_require__ (eval at Cn ((weex):2:4300), <anonymous>:26:30)
04-11 11:48:50.950 3321-3345/com.weex.sample E/weex: reportJSException >>>> instanceId:2, exception function:createInstance, exception:ReferenceError: window is not defined
問題解答
回答1:在原生層面window對象不支持
回答2:找到答案了 是在頁面跳轉時獲取頁面uri階段出問題了 使用了window.location.host andorid 這邊不認識這個window
exports.getBaseUrl = function (bundleUrl, isnav) {
bundleUrl = new String(bundleUrl);var nativeBase;var isAndroidAssets = bundleUrl.indexOf(’file://assets/’) >= 0;var isiOSAssets = bundleUrl.indexOf(’file:///’) >= 0 && bundleUrl.indexOf(’WeexDemo.app’) > 0;if (isAndroidAssets) { nativeBase = ’file://assets/dist/’;}else if (isiOSAssets) { nativeBase = bundleUrl.substring(0, bundleUrl.lastIndexOf(’/’) + 1);}else { // var host = window.location.host; 這個在瀏覽器中可以用 var host; var matches = ///([^/]+?)//.exec(bundleUrl); if (matches && matches.length >= 2) {host = matches[1]; } // alert('er:::'+host); //此處需注意一下,tabbar 用的直接是jsbundle 的路徑,但是navigator是直接跳轉到新頁面上的. // if (typeof window === ’object’) { // nativeBase = isnav ? ’http://’ + host + ’/index.html?page=./dist/’ : ’/dist/’; // } else { // nativeBase = ’http://’ + host + ’/dist/’; // }}// alert('nativeBase: '+nativeBase);nativeBase='http://'+host+'/dist/';return nativeBase;
};
相關文章:
1. HTML5不支持frameset一般怎么解決?2. javascript - Vue的計算屬性底層依賴原理是怎么樣?3. javascript - jquery怎么給select option一個點擊時觸發的事件,如圖 如果選擇自定義觸發一個時間?4. 網頁爬蟲 - python爬蟲用BeautifulSoup爬取<s>元素并寫入字典,但某些div下沒有這一元素,導致自動寫入下一條,如何解決?5. 百度地圖api - Android百度地圖SDK,MapView上層按鈕可見卻不可觸,怎么解決?6. mysql 獲取時間函數unix_timestamp 問題?7. html5 - canvas中的mousedrag事件,為什么鼠標拖出canvas,然后再次移入canvas,drag事件還觸發8. 新入手layuiadmin,部署到tp中。想用php自已寫一個后臺管理系統。9. javascript - react 組件 使用super()報錯10. javascript - es6擴展運算符...的問題
