加载中...

1. Weex 快速上手教程


我们将使用Weex编写一个简单的列表,类似的列表经常能在电商类移动应用中见到。

开始

我们先编写一个列表项。
  1. <template>
  2. <div class="container" >
  3. <div class="cell">
  4. <image class="thumb" src="http://t.cn/RGE3AJt"></image>
  5. <text class="title">JavaScript</text>
  6. </div>
  7. </div>
  8. </template>
  9.  
  10. <style>
  11. .cell{margin-top:10 ; margin-left:10 ; flex-direction: row; }
  12. .thumb {width: 200; height: 200; }
  13. .title {text-align: center ; flex: 1; color: grey; font-size: 50; }
  14. </style>
请创建一个名为 tech_list.we 的文件( .we 是Weex推荐的后缀名 ) ,请复制粘贴以上代码于其中。
因为Weex工具链使用Node.js构建,在进行后续步骤前,你需要先安装 Node.js, 在Node.js安装成功后,你可以执行下面的命令来安装Weex命令行程序 Weex Toolkit 。
  1. npm install -g weex-toolkit
在安装结束后,你能通过在命令行窗口执行 weex 命令来检查工具是否安装正确。仅仅输入weex并敲击回车后,你应该看到如下内容显示:
  1. Usage: weex foo/bar/your_next_best_weex_script_file.we [options]
  2.  
  3. Options:
  4. --qr display QR code for native runtime,
  5. -o,--output transform weex we file to JS Bundle, output path (single JS bundle file or dir)
  6. -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
  7. ......
  8. --help Show help               
如果一切正常, 请在命令行中切换工作目录到刚才存储 tech_list.we 所用目录并输入如下命令:
  1. weex tech_list.we
你系统默认浏览器的窗口将自动打开以显示如下内容。
(请使用 weex --version 命令检查你的weex-toolkit版本是否大于 0.1.0)
  1. weex html5 render

语法概念

现在我们来了解下一些简单的语法概念。如 tech_list.we所示,Weex代码由三部分构成: template (模板), style (样式)  和 script (脚本) 。这三个概念之于Weex就如 html,css,javascript 之于Web。

模板部分赋予Weex以骨架,由标签以及标签包围的内容构成。Weex中的标签分为开放标签(eg: <weex>)和闭合标签(eg: </weex>)两种,我们把每一对开放&闭合标签称为一组Weex标签。标签中能添加 属性 ,不同的 属性 有不同的含义,例如 class属性让同样的样式可以作用于多组Weex标签, onclick 属性让标签能对用户点击事件作出回应。

样式部分描述Weex标签如何显示。和你一样,我们喜欢CSS,所以Weex中的样式尽量和CSS标准一致。Weex支持很多CSS中的特性: margin, padding, fixed...... 更好的是, flexbox布局模型在Weex中有着很好的支持。

脚本部分为Weex标签添加数据与逻辑,在这里你能方便的访问本地和远程的数据并更新标签。你还能定义方法并让这些方法响应不同的事件。Weex脚本的组织方式基本遵循于CommonJS module规范。

添加更多的列表项

单独一个列表项称不上”列表” , 所以让我们来添加更多的列表项。打开刚才的tech_list.we文件,更新其中的内容如下:
  1. <template>
  2. <div class="container">
  3. <div class="cell">
  4. <image class="thumb" src="http://t.cn/RGE3AJt"></image>
  5. <text class="title">JavaScript</text>
  6. </div>
  7. <div class="cell">
  8. <image class="thumb" src="http://t.cn/RGE3uo9"></image>
  9. <text class="title">Java</text>
  10. </div>
  11. <div class="cell">
  12. <image class="thumb" src="http://t.cn/RGE31hq"></image>
  13. <text class="title">Objective C</text>
  14. </div>
  15. </div>
  16. </template>
  17.  
  18. <style>
  19. .cell{ margin-top:10 ; margin-left:10 ; flex-direction: row; }
  20. .thumb { width: 200; height: 200; }
  21. .title { text-align: center ; flex: 1; color: grey; font-size: 50; }
  22. </style>
现在,让我们来尝试使用Weex Native渲染器来渲染这个文件。打开终端,切换到保存该文件的目录,执行:
  1. weex tech_list.we --qr
终端中将会出现一个二维码,类似如下这样:



这个二维码需要配合 Weex Playground App工作。下载安装后点击App中的扫码图标,然后用你的手机摄像头扫描终端中的二维码。一个漂亮的列表将出现在你的手机中。



这里我需要强调,这个列表是完全由native view(不是Webkit)来进行渲染的,相比Webkit渲染的界面,你的App能获得更快的页面加载速度和更少的内存开销。

现在你能尝试变更一些 tech_list.we中的内容,在保存变更内容之后, Weex Playground 将会立即在界面上反映出这些变化,这个特性常被称为 Hot-Reload ,希望能帮助你更方便的进行Weex开发。

添加内置组件

除了自己动手从最基础的标签开始编写,Weex还提供很多内置组件。Slider(滑动器)在移动App和页面中很常见,所以我们提供了一个内置的Slider组件让你能在自己的界面里轻松的添加一个滑动器。打开 tech_list.we,把里面的内容变更如下:
  1. <template>
  2. <div style="flex-direction: column;">
  3. <slider class="slider" interval="{{intervalValue}}" auto-play="{{isAutoPlay}}" >
  4. <div class="slider-pages" repeat="{{itemList}}" onclick="goWeexSite" >
  5. <image class="thumb" src="{{pictureUrl}}"></image>
  6. <text class="title">{{title}}</text>
  7. </div>
  8. </slider>
  9.  
  10. <div class="container" onclick="goWeexSite" >
  11. <div class="cell">
  12. <image class="thumb" src="http://t.cn/RGE3AJt"></image>
  13. <text class="title">JavaScript</text>
  14. </div>
  15. <div class="cell">
  16. <image class="thumb" src="http://t.cn/RGE3uo9"></image>
  17. <text class="title">Java</text>
  18. </div>
  19. <div class="cell">
  20. <image class="thumb" src="http://t.cn/RGE31hq"></image>
  21. <text class="title">Objective C</text>
  22. </div>
  23. </div>
  24. </template>
  25.  
  26. <style>
  27. .cell { margin-top:10 ; margin-left:10 ; flex-direction: row; }
  28. .thumb { width: 200; height: 200; }
  29. .title { text-align: center ; flex: 1; color: grey; font-size: 50; }
  30. .slider {
  31. margin: 18;
  32. width: 714;
  33. height: 230;
  34. }
  35. .slider-pages {
  36. flex-direction: row;
  37. width: 714;
  38. height: 200;
  39. }
  40. </style>
  41.  
  42. <script>
  43. module.exports = {
  44. data: {
  45. intervalValue:"1000",
  46. isShowIndicators:"true",
  47. isAutoPlay:"true",
  48. itemList: [
  49. {title: 'Java', pictureUrl: 'http://t.cn/RGE3uo9'},
  50. {title: 'Objective C', pictureUrl: 'http://t.cn/RGE31hq'},
  51. {title: 'JavaScript', pictureUrl: 'http://t.cn/RGE3AJt'}
  52. ]
  53. },
  54. methods: {
  55. goWeexSite: function () {
  56. this.$openURL('http://alibaba.github.io/weex/')
  57. }
  58. }
  59. }
  60. </script>
在终端中同一目录再次运行这个命令:
  1. weex tech_list.we
一个漂亮的滑动器将会添加到我们之前编写列表的顶部。


第 1-1 条, 共 1 条.
力美5778 2018-06-14 13:28:59
我用npm命令 weex priview tech_list.we之后,在google chrome出来了一个带手机框的空白界面,并没有像教程所见的那样的页面,我是初学者回复