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即可查看效果

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp是一款基于Vue.js框架的跨平台开发工具,它可以将一个代码库编译为多个平台的应用,如iOS、Android、H5等。在UniApp中,组件封装是非常重要的一部分,它可以帮助我们实现代码的复用和模块化,提高开发效率。 下面是一个简单的示例,展示了如何封装一个Button组件: 1. 创建一个Button组件的文件,比如Button.vue。 ```javascript <template> <button class="custom-button" @click="handleClick"> <slot></slot> </button> </template> <script> export default { methods: { handleClick() { // 处理按钮点击事件 this.$emit('click'); } } } </script> <style scoped> .custom-button { /* 自定义样式 */ } </style> ``` 2. 在需要使用Button组件的地方,比如Home.vue,引入并使用Button组件。 ```javascript <template> <div> <Button @click="handleButtonClick">Click Me</Button> </div> </template> <script> import Button from '@/components/Button.vue'; export default { components: { Button }, methods: { handleButtonClick() { // 处理按钮点击事件 } } } </script> <style> /* 其他样式 */ </style> ``` 通过以上步骤,我们成功封装了一个Button组件,并在其他页面中使用它。在Button组件中,我们使用了Vue的插槽(slot)来实现内容的插入,使用了事件($emit)来实现对外部事件的派发。 这只是一个简单的示例,你可以根据具体的业务需求和设计风格来封装更复杂的组件。同时,UniApp还提供了丰富的API和生命周期钩子函数,可以帮助你更好地封装和管理组件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值