因此,我設置了一個頁面,並且希望使用應用程序與網頁進行交互。Nativescript WebView,防止在Android上導航時重新加載
我能夠通過偵聽某些URL從頁面獲取數據,並阻止該頁面從Web瀏覽它們。
當我得到這些數據時,我導航到一個帶有處理列表的新頁面。
然後,當我點擊返回按鈕返回瀏覽器頁面時,瀏覽器重新加載,即使其URL已設置。
很簡單的設置:
XML:
<Page loaded="loaded" class="page">
<Page.actionBar>
<ActionBar title="Cost" icon="" class="action-bar">
</ActionBar>
</Page.actionBar>
<WebView id="mywebview" />
</Page>
JS:
var checkPageLoad = function(event) {
console.log(event.url);
if (event.url.indexOf("mydomain") < 0) {
if (app.ios) {
event.object.ios.stopLoading();
} else if (app.android) {
event.object.android.stopLoading();
}
}
if (event.url.indexOf('custom://mydomain.com') === 0) {
farm(event.url.replace("custom://mydomain.com", ""));
}
};
exports.loaded = function(args) {
page = args.object;
var web = page.getViewById("mywebview");
web.on(webView.WebView.loadStartedEvent, checkPageLoad);
if(web.android) { // in IOS android will be undefined
web.android.setInitialScale(1);
web.android.getSettings().setLoadWithOverviewMode(true);
web.android.getSettings().setUseWideViewPort(true);
}
if (web.src != "http://myurl.com") {
web.src = "http://myurl.com";
}
};
function farm(item) {
var navigationOptions={
moduleName:'./list/list',
context:{info: item},
animated: true,
transition: {
name: "slide",
duration: 380,
curve: "easeInOut"
}
};
var topmost = frameModule.topmost();
topmost.navigate(navigationOptions);
}
所以,當我按下從 「列表」 頁面後退按鈕,web視圖是雖然白重載。
任何想法?它是否與frameModule導航有關?
============================================== ==
更新17年6月29日:
我實現了一個變通針對此問題。它不是最好的,但它使它工作令人滿意。
解決方法:使用NativeScripts page
的showModal()
函數。 http://docs.nativescript.org/core-concepts/navigation#modal-pages
這會創建一個彈出窗口,當按下後退時,它將關閉彈出窗口而不重新加載webview。
爲了實施,我不得不改變我的頁面設置方式。
只有一個模式頁面可以一次打開,因此每個視圖都需要與webview頁面進行通信。
頁面還有一個事件onShownModally
,它使框架模塊的上下文略有不同。
您必須存儲您發送到模態頁面的回調,並在模態頁面關閉時調用它。
儘管如此,仍然想用一種方式使用框架模塊設置。
你已經找到了一個解決方案? –
不,只是我周圍的工作更新了帖子。 –
您可能想閱讀:https://github.com/NativeScript/NativeScript/issues/4615#issuecomment-320937348:D –