我在使用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();
}
为情所困2017-06-05 11:13:08
src属性接受一个字符串,当你需要用变量的时候,对应vue的语法是v-bindsrc='{{src}}'
实际上是传了一个{{src}}
的字符串进去,肯定解析不了src='{{src}}'
实际上是传了一个{{src}}
的字符串进去,肯定解析不了
所以要用:src='variable'
所以要用:src='variable'
而且v-bind不止接受变量,还可以写表达式
我看了一下你的代码你那个onPageFinish如果是事件的话应该是要绑定v-on的,也就是应该写成@onPageFinish