weex組件封裝

組件封裝調用:

1、實際開發應用中,會有很多可複用的 weex 文件,這時候可以封裝成 weex 組件。比如可以直接創建一個名爲foo1.we的文件,<foo1>就是組件名

<!--foo1.we-->

<template>
 <div class="container">
     <div class="cell">
   <image class="thumb" src="{{image}}"></image>
   <text class="title">{{title}}</text>
  </div>
 </div>
</template>

<style>
 .cell { margin-top: 10; margin-left: 10; flex-direction: row; }
 .thumb { width: 200; height: 200; }
 .title { text-align: center; flex: 1; color: grey; font-size: 50; }
</style>

<script>
 module.exports = {
  data:{
   title:null,
   image:null
  }
 }
</script>

 

2、test1.we

<!--test1.we-->

<template>
     <foo1 title="JavaScript" image="http://t.cn/RGE3AJt"></foo1>
</template>

 

3、說明:foo1.we 的也包含 <template><style><script>,定義好了後,直接用<foo1>標籤即可, 注意這裏test1.wefoo1.we是在同目錄下哦

4、切換到文件目錄,命令:weex test1.we即可查看效果

 

 

組件嵌套調用:

1、第一層:仍使用上面的foo1.we

2、第二層:foo2.we

<!--foo2.we-->

<template>
 <div class="container">
  <div class="ceil">
   <text>{{description}}</text>
   <foo1 repeat="{{list}}" title="{{name}}" image="{{img}}"></foo1>
  </div>
 </div>
</template>

<style>
 .cell { margin-top: 10; margin-left: 10; flex-direction: row; }
</style>

<script>
 module.exports = {
  data:{
   description:'',
   list:[]
  }
 }
</script>

3、第三層:test2.we

<!--test2.we-->
<template>
   <foo2 list="{{list}}"></foo2>
</template>

<script>
 module.exports = {
   data:{
    list:[
        {name:'JavaScript',img:'http://t.cn/RGE3AJt'},
        {name:'java',img:'http://t.cn/RGE3uo9'},
        {name:'Objective C',img:'http://t.cn/RGE31hq'}
   ]
  }
 }
</script>

4、說明:test2.we中嵌套了<foo2>,foo2.we中嵌套了<foo1>,注意這裏test2.we、foo2.we、foo1.we都是在同目錄下

5、切換到文件目錄,命令:weex test2.we即可查看效果

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章