A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Simpon 中级黑马   /  2016-7-27 17:14  /  10384 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Simpon 于 2016-10-19 11:36 编辑

WEEX 是阿里推送的一款基于Node.js,轻量级的移动端跨平台动态性技术解决方案,用于构建原生的速度的跨平台APP.
1. 搭建WEEX环境1.1 首先下载安装Node.js,MAC版下载
1.2 安装 weex-toolkit(WEEX工具包)
终端执行
[AppleScript] 纯文本查看 复制代码
 sudo npm install -g weex-toolkit 
执行后需要输入你电脑的密码.
1.3 验证是否安装成功
终端执行
[AppleScript] 纯文本查看 复制代码
weex 
显示
[AppleScript] 纯文本查看 复制代码
info 
Usage: weex foo/bar/we_file_or_dir_path  [options]
Usage: weex init

选项:
  --qr          display QR code for native runtime, default action     [boolean]
  -o, --output  transform weex we file to JS Bundle, output path must specified
                (single JS bundle file or dir)
                [for create sub cmd]it specified we file output path
                                                  [默认值: "no JSBundle output"]
  --watch       using with -o , watch input path , auto run transform if change
                happen
  -s, --server  start a http file server, weex .we file will be transforme to JS
                bundle on the server , specify local root path using the option
                                                                        [string]
  --port        http listening port number ,default is 8081         [默认值: -1]
  --wsport      websocket listening port number ,default is 8082    [默认值: -1]
  --np          do not open preview browser automatic                  [boolean]
  -f, --force   [for create sub cmd]force to replace exsisting file(s) [boolean]
  --help        显示帮助信息                                           [boolean]
  -h, --host                                               [默认值: "127.0.0.1"]

for example & more information visit [url=https://www.npmjs.com/package/weex-toolkit]https://www.npmjs.com/package/weex-toolkit[/url]
表明安装成功.

2. 安装weex的编辑工具
如果使用Xcode编辑weex,没有格式也没有高亮和提示,我们需要使用一个编辑工具来快速的编写weex的代码.
2.1 去sublime Text官网下载安装sublineText3 然后双击安装.
2.2 下载 weex高亮脚本
2.3 添加高亮脚本 sublime Text 导航栏里选择Tools->Developer->New Syntax
2.4 下载好的weex高亮脚本文件打开

把内容拷贝到新建的文件中,覆盖原有的内容. 然后cmd + s保存,把名称命名为Plain we.sublime-syntax,请注意文件名称必须命名为Plain we.sublime-syntax,否则没有高亮.
2.5 开启weex语法高亮

3. 来一个简单的Demo玩玩.
3.1 制作一个简单的weex文件
我们来做一个列表,现在只包含一个Cell,cell内部只有一个图片,图片右边是一个文本.
[AppleScript] 纯文本查看 复制代码
<template>
  <div class="container">
    <div class="cell">
        <image class="thumb" src="http://t.cn/RGE3AJt"></image>
        <text class="title">JavaScript</text>
    </div>
  </div>
</template>

<style>
  .cell { margin-top: 8; margin-left: 8; flex-direction: row; }
  .thumb { width: 100; height: 100; }
  .title { text-align: center; flex: 1; color: grey; font-size: 25; }
</style>
把上面的文本拷贝到编译器中,cmd + s 保存你想保存的位置,命名为list.we,其中`we`是`weex`文件的后缀.######
3.2 预览
打开终端,`cd` 到 `list.we` 所在的目录,执行
[AppleScript] 纯文本查看 复制代码
weex list.we 
`` weex 工具会启动服务器,把list.we`转换为html5的页面,然后在浏览器中把它打开. 效果如图

你可以改变上面的元素来查看不同的效果.
3.2 weex 语法简介
一个WEEX文件由三部分组成,分别为template,style,script;其中template是骨架,类似于HTML标签,style负责渲染,类似于css, script负责交互事件,类似于javascript
  • template 由标签组成,标签用于包裹内容.weex包含两种类型的标签,分别为开放标签和闭合标签 开放标签由一对标签组成如<text>内容</text> 闭合标签,只有一个标签如 <image src="http://t.cn/RGE3AJt"/> 标签上可以有多个属性,不同的属性代表不同的含义.比如:class属性用于定义标签的样式. onclick 属性让标签响应点击事件.
  • Style 用于描述标签如何展示,语法与css类似,weex支持大部分css的特征,比如margin,padding,fixed等,更令人兴奋的是weex支持flexbox的布局.
  • Script 用于个标签设置数据和添加逻辑,让我们更加简单的绑定本地或远程的数据和更新标签. 我们可以定义函数来处理tag上的事件. Script 类似于通用的CommonJS的模型.

更多weex语法,参考Syntax chapter

精华推荐:


2 个回复

倒序浏览
棒!!!
回复 使用道具 举报
多谢分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马