When I use the weex web, I set src as a variable. On the Android side, the webView cannot be loaded successfully.
we code is as follows:
<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>
The web implementation on the Android side is mainly as follows:
@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;
}
During the debugging process, I found that the setProperty
method will only be executed once, that is, when setting, when src is reassigned, the web will not refresh the url and reload it. How to realize that when src changes, the web can be reloaded,
Tried the refreshData
method and found that it will not be called.
@Override
public void refreshData(WXComponent component) {
super.refreshData(component);
component.getDomObject().getAttrs();
}
I suddenly felt that it was your writing style that was the problem. . .
src='{{src}}'
changed to:src='src'
The
src attribute accepts a string. When you need to use a variable, the corresponding syntax for vue is v-bind
src='{{src}}'
In fact, a{{src}} is passed If the
string is entered, it will definitely not be parsedso you have to use
:src='variable'
and v-bind not only accepts variables, but can also write expressions
I took a look at your code. If your onPageFinish is an event, it should be bound to v-on, which means it should be written as @onPageFinish