我在使用weex的web時,把src設為一個變量,在Android端,webView無法載入成功。
we程式碼如下:
<template>
<tm-navpage title='{{title}}' listenning_rigth_button_click=true>
<web id='toomao-web' src='{{src}}' style='width: 750; height: 1206;' onpagefinish='pagefinish'></web>
<tm-loading if='loading'></tm-loading>
</tm-navpage>
</template>
<script>
data: {
src: '', // web加载链接
url: '', // url参数
userInfo: '',
title: '',
loading: true,
canGoBack: false, // 记录当前webView的加载信息
},
created() {
this.url = decodeURIComponent(getUrlParams(this, 'url'));
}
</script>
Android端的web實作主要如下:
@Deprecated
public WXWeb(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, String instanceId, boolean isLazy) {
this(instance,dom,parent,isLazy);
}
public WXWeb(WXSDKInstance instance, WXDomObject dom, WXVContainer parent, boolean isLazy) {
super(instance, dom, parent, isLazy);
createWebView();
}
protected void createWebView(){
mWebView = new WXWebView(getContext());
}
@Override
protected View initComponentHostView(@NonNull Context context) {
mWebView.setOnErrorListener(new IWebView.OnErrorListener() {
@Override
public void onError(String type, Object message) {
fireEvent(type, message);
}
});
mWebView.setOnPageListener(new IWebView.OnPageListener() {
@Override
public void onReceivedTitle(String title) {
if (getDomObject().getEvents().contains(Constants.Event.RECEIVEDTITLE)) {
Map<String, Object> params = new HashMap<>();
params.put("title", title);
fireEvent(Constants.Event.RECEIVEDTITLE, params);
}
}
@Override
public void onPageStart(String url) {
if ( getDomObject().getEvents().contains(Constants.Event.PAGESTART)) {
Map<String, Object> params = new HashMap<>();
params.put("url", url);
fireEvent(Constants.Event.PAGESTART, params);
}
}
@Override
public void onPageFinish(String url, boolean canGoBack, boolean canGoForward) {
if ( getDomObject().getEvents().contains(Constants.Event.PAGEFINISH)) {
Map<String, Object> params = new HashMap<>();
params.put("url", url);
params.put("canGoBack", canGoBack);
params.put("canGoForward", canGoForward);
fireEvent(Constants.Event.PAGEFINISH, params);
}
}
});
return mWebView.getView();
}
@Override
public void destroy() {
super.destroy();
getWebView().destroy();
}
@Override
protected boolean setProperty(String key, Object param) {
switch (key) {
case Constants.Name.SHOW_LOADING:
Boolean result = WXUtils.getBoolean(param,null);
if (result != null)
setShowLoading(result);
return true;
case Constants.Name.SRC:
String src = WXUtils.getString(param,null);
if (src != null)
setUrl(src);
return true;
}
return super.setProperty(key,param);
}
@WXComponentProp(name = Constants.Name.SHOW_LOADING)
public void setShowLoading(boolean showLoading) {
getWebView().setShowLoading(showLoading);
}
@WXComponentProp(name = Constants.Name.SRC)
public void setUrl(String url) {
if (TextUtils.isEmpty(url) || getHostView() == null) {
return;
}
if (!TextUtils.isEmpty(url)) {
loadUrl(getInstance().rewriteUri(Uri.parse(url), URIAdapter.WEB).toString());
}
}
public void setAction(String action) {
if (!TextUtils.isEmpty(action)) {
if (action.equals(GO_BACK)) {
goBack();
} else if (action.equals(GO_FORWARD)) {
goForward();
} else if (action.equals(RELOAD)) {
reload();
}
}
}
private void fireEvent(String type, Object message) {
if (getDomObject().getEvents().contains(Constants.Event.ERROR)) {
Map<String, Object> params = new HashMap<>();
params.put("type", type);
params.put("errorMsg", message);
fireEvent(Constants.Event.ERROR, params);
}
}
private void loadUrl(String url) {
getWebView().loadUrl(url);
}
private void reload() {
getWebView().reload();
}
private void goForward() {
getWebView().goForward();
}
private void goBack() {
getWebView().goBack();
}
private IWebView getWebView() {
return mWebView;
}
在調試的過程中,發現setProperty
方法只會走一次,就是在設定的時候, 當src從新賦值之後,web並不會去刷新url從新載入。求如何實現當src變化 時,web可以去從新加載,
嘗試過refreshData
方法,發現也不會呼叫。
@Override
public void refreshData(WXComponent component) {
super.refreshData(component);
component.getDomObject().getAttrs();
}
我突然怎麼覺得是你的寫法問題。 。 。
src='{{src}}'
改為:src='src'
src屬性接受一個字串,當你需要用變數的時候,對應vue的語法是v-bind
src='{{src}}'
其實是傳了一個{{src}}
的字串進去,肯定解析不了所以要用
:src='variable'
而且v-bind不止接受變量,還可以寫表達式
我看了一下你的程式碼你那個onPageFinish如果是事件的話應該是要綁定v-on的,也就是應該寫成@onPageFinish