简体   繁体   English

Weex中跨设备本地资产的位置

[英]Cross-device local assets location in Weex

Working with Weex 0.16 I generated the default "WeexDemo" project for Web, iOS and Android, and the default index files works without issue. 使用Weex 0.16,我为Web,iOS和Android生成了默认的“ WeexDemo”项目,并且默认的索引文件可以正常工作。

I'm trying to figure out the folder location where I can put image files and other assets. 我正在尝试找出可以放置图像文件和其他资产的文件夹位置。 I changed the "logoUrl" from an http url to a relative path ('bg.png') 我将“ logoUrl”从http网址更改为相对路径('bg.png')

  • Serving on the browser over 8080 port web root is the project root folder, so if I copy bg.png to "dist" folder I can see it when logoUrl is "/dist/bg.png" 在浏览器上通过8080端口Web根提供的服务是项目根文件夹,因此,如果将bg.png复制到“ dist”文件夹,则在logoUrl为“ /dist/bg.png”时可以看到它
  • On the iOS project "/dist/bg.png" does not work, since the root seems to be the "dist" folder itself, if I change to logoUrl "bg.png", it works. 在iOS项目上,“ / dist / bg.png”不起作用,因为根目录似乎是“ dist”文件夹本身,如果我将logoUrl更改为“ bg.png”,则它可以工作。
  • On the Android project, both "/dist/bg.png" or "bg.png" fail. 在Android项目上,“ / dist / bg.png”或“ bg.png”均失败。 I can see in the folder "/platforms/android/app/src/main/assets/dist" the bg.png file has been copied but I don't seem to able to access it. 我可以在文件夹“ / platforms / android / app / src / main / assets / dist”中看到bg.png文件已被复制,但是我似乎无法访问它。

Is there any solution where I can use the same folder to drop the assets and have it accessible for all devices? 有什么解决方案可以使我使用同一文件夹删除资产并使所有设备均可访问它?

Do as follows : 进行如下操作:

In your application class : 在您的应用程序类中:

 WXSDKEngine.registerComponent("yourImage", YourImage.class);

create class YourImage.java and it should look like this : 创建类YourImage.java,它应如下所示:

public class YourImage extends WXComponent<ImageView> {

public YourImage(WXSDKInstance instance, WXDomObject dom, WXVContainer parent) {
    super(instance, dom, parent);
}

@Override
protected ImageView initComponentHostView(@NonNull Context context) {
    ImageView imageView = new ImageView(context);
    return imageView;
}

@WXComponentProp(name = Constants.Name.SRC)
public void setSrc(String src) {

    if (src == null) {
        return;
    }

    if(null != getHostView()) {       
      Picasso.with(WXEnvironment.getApplication()).load(src).into(getHostView());
    }
  }
}

In your vue/js file 在您的vue / js文件中

<yourImage class="image" :src="url"> </yourImage>

In your WeexUIFragment/Activity : while loading page data through map, add one key and value as follows: 在您的WeexUIFragment / Activity中:通过地图加载页面数据时,添加一个键和值,如下所示:

    HashMap<String, Object> map = new HashMap<>();

    map.add("image_url", "android local asset url")
    map.put("PAGENAME", "");
    mWXSDKInstance = new WXSDKInstance(YourCurrentActivity.this);
    mWXSDKInstance.registerRenderListener(YourCurrentActivity.this);
    mWXSDKInstance.render(pageName, weexJSUrl, map, null, WXRenderStrategy.APPEND_ASYNC);

In JS file in script/data block to retrieve asset local image url: 在脚本/数据块中的JS文件中,以检索资产本地图片网址:

const url = _.get(weex,'config.image_url')

If you need more help: You can look into Component Extend section in below link : https://weex.incubator.apache.org/guide/extend-android.html 如果您需要更多帮助:您可以查看以下链接中的“组件扩展”部分: https : //weex.incubator.apache.org/guide/extend-android.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM