Weex内置组件的使用——Weex的学习之路(二)

最近公司项目不是很忙,就和组内成员商量利用时间学学新技术,最终在Weex和Flutter做选择。Weex是由阿里的大牛们开发出来语言,Flutter是由Google公司开发的。各种网上查找得出的评论都是Flutter更有前景,但是最终我们还是敲定学Weex。为什么呢?是因为任性?我也不敢问,但是我敢说啊,哈哈哈。因为我们会JS,然而Flutter用的是Dart语言,我不会。

下面我们来看看Weex的内置组件:

这些就是weex自带的内置组件,通过这些组件我们使用Flex 布局基本可以绘制我们想要的界面,当然这些组件满足不了我们的需求时,那么我们就要自定义组件了。同时学习weex还要具备基础的js和css基础,所以还不了解js的小伙伴可以去W3School先做最基本的了解(http://www.w3school.com.cn/jsref/jsref_obj_number.asp)。

这一次我们先来学习<a>、<div>和<image>标签

1.<a>标签

<a> 组件用于实现页面间的跳转。

这个标签不可以直接在内部添加文本,需要使用<text>标签:

<a href="http://dotwe.org/raw/dist/a5e3760925ac3b9d68a3aa0cc0298857.bundle.wx">
  <text>Jump</text>
</a>

href 为string类型,里面的内容为待跳转的页面 URL,待跳转页面需要是一个 Weex 页面,否则会是一个未定义行为

其中具体适用的示例是:

<template>
  <div class="wrapper">
    <a class="button" href="http://g.tbcdn.cn/amte-fe/amte-resource/0.0.8/fast/show_1.js">
      <text class="text">去主会场</text>
    </a>
  </div>
</template>

<style scoped>
  .wrapper {
    align-items: center;
    justify-content: center;
  }
  .button {
    width: 350px;
    padding-top: 20px;
    padding-bottom: 20px;
    border-radius: 5px;
    background-color: #3eaf7c;
  }
  .text {
    font-size: 48px;
    color: #fff;
    text-align: center;
  }
</style>

2.<div>标签

<div> 是通用容器,它支持各种类型的子元素,包括 <div> 自己。

这个标签会在vue里面频繁使用到,同时有以下几点需要注意:

  • 不要在 <div> 中直接添加文本,而要使用 <text> 组件。
  • 在 Weex 中,<div> 不可滚动。
  • 要控制 <div> 的层级,建议不要超过14层,否则会很影响页面性能

上面的示例中就使用到了<div>标签。

3.<text>标签

<text> 是 Weex 内置的组件,用来将文本按照指定的样式渲染出来,但是<text> 不支持子组件。同时要特别注意了<text> 里直接写文本头尾空白会被过滤,如果需要保留头尾空白字符,暂时只能通过数据绑定的方式,见下面动态文本:

动态文本片段可以实现文字内容和JS变量的绑定

<template>
  <div>
    <text >{{content}}</text>
  </div>
</template>
<script>
  module.exports = {
    data: function(){
      return {
          content: "Weex is an cross-platform development solution that builds high-performance, scalable native applications with a Web development experience. Vue is a lightweight and powerful progressive front-end framework."
      }
    }
}
</script>

其中.text {
    lines:3;
    color: #666666;
    font-size: 32px;
  }设置其相应属性

4.<image>标签

<image> 用于在界面中显示单个图片。在代码中请使用 <image> 标签, <img> 的存在只是因为兼容性原因,以后新版本中可能删除该属性,同时它不支持子组件,有以下两点需要大家注意:

  • <image> 必须指定样式中的宽度和高度。
  • <image> 不支持内嵌子组件。
<image style="width:500px;height:500px" src="https://vuejs.org/images/logo.png"></image>

以上代码在Weex Studio中有一个坑,上面的写法会报错,必须这么写才行:

<image style="width:500px;height:500px" src="https://vuejs.org/images/logo.png"/>

其完整的使用法是:

<template>
  <div>
    <div title = 'image-onload' :padding-body='0'>
      <div style='flex-direction:row'>
        <image class='mr-base' style="width: 300px;height: 300;" src="https://gw.alicdn.com/tps/TB1bEMYKXXXXXaLaXXXXXXXXXXX-360-388.png" @load="onload"></image>
        <image class='mr-base' style="width: 300;height: 300;border-width:2px;" src="https://cn.vuejs.org/images/logo.png1" @load="onloadFailed"></image>
      </div>
      <div>
        <text test-id='imgSize' style="font-size:30px">size : {{size}}</text>
        <text test-id='download' style="font-size:30">{{download}}</text>
      </div>
    </div>
  </div>
</template>
<script>
  module.exports = {
    data : {
      size:"-1,-1",
      download:'success'
    },
    methods : {
      onload : function(e) {
        nativeLog(JSON.stringify(e))
        this.size = e.size.naturalWidth + ',' + e.size.naturalHeight;
      },
      onloadFailed:function(e) {
        if (e.success){
          this.download = 'success'; 
        }else {
          this.download ='failed';
        }
      }
    }
  }
</script>

<image>有以下几个属性:

placeholder占位图的 URL,在图片下载过程中将展示占位图,图片下载完成后将显示src中指定的图片;

resize:

  • contain:缩放图片以完全装入<image>区域,可能背景区部分空白。
  • cover:缩放图片以完全覆盖<image>区域,可能图片部分看不见;
  • stretch默认值. 按照<image>区域的宽高比例缩放图片。

其中resize属性和background-size的理念很相似。

src:要显示图片的 URL,该属性是 <image> 组件的强制属性;

autoBitmapRecycle:一个布尔标志位控制当图片滚出屏幕时是否回收相关内存。

  • true 当图片不可见时回收图片内存
  • false 当图片不可见时不回收图片内存,这样做会占用更高的内存,但是会提供更好的用户体验。
  • 默认值是true

<image>标签上增加 ref 属性 :

<image ref="poster" src="path/to/image.png"></image>

获取组件引用并使用 save 方法:

const $image = this.$refs.poster
$image.save(result => {
  if (result.success) {
    // Do something to hanlde success
  } else {
    console.log(result.errorDesc)
    // Do something to hanlde failure
  }
})

<image>标签的load 事件,在 <image> 标签上绑定 load 事件:

<image @load="onImageLoad" src="path/to/image.png"></image>

增加事件处理函数:

export default {
  methods: {
    onImageLoad (event) {
      if (event.success) {
        // Do something to hanlde success
      }
    }
  }
}

以上就是weex的<a>、<div>、<text>和<image>基本使用和只有属性的用法。日积月累,weex上手很快的,加油!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值