All Projects → yinhaijiao → XcxComponentsDemo

yinhaijiao / XcxComponentsDemo

Licence: other
微信小程序组件化方案示例

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to XcxComponentsDemo

Wuss Weapp
🐳wuss-weapp 一款高质量,组件齐全,高自定义的微信小程序UI组件库
Stars: ✭ 338 (+2500%)
Mutual labels:  wxss, wxml
Mina Webpack
🍱 Mina single-file-component meets Webpack
Stars: ✭ 77 (+492.31%)
Mutual labels:  wxss, wxml
Weapp
🐧 微信小程序组件和功能封装,基于微信Component自定义组件开发
Stars: ✭ 235 (+1707.69%)
Mutual labels:  wxss, wxml
Thorui
ThorUI组件库,微信小程序项目代码分享,组件文档地址:https://www.thorui.cn/doc 。 最近更新时间:2021-01-19
Stars: ✭ 817 (+6184.62%)
Mutual labels:  wxss, wxml
Tina
💃 一款轻巧的渐进式微信小程序框架
Stars: ✭ 1,153 (+8769.23%)
Mutual labels:  wxss, wxml
Wxapp.vim
提供微信小程序开发全方位支持的 vim 插件
Stars: ✭ 405 (+3015.38%)
Mutual labels:  wxss, wxml
Toilet Webapp
微信版小明找厕所V2.0,升级为本地微信自带的地图路径规划、在地图上显示所有marker点、添加关于页面。主要功能包括:1.可以在地图上面展示所有厕所的位置,并且标记,2.直接在小程序端进行路径规划,3.ios和android一样默认选择步行方式,4.制作关于界面
Stars: ✭ 240 (+1746.15%)
Mutual labels:  wxss, wxml
Wxapp toutiaonews
📰微信小程序--头条新闻
Stars: ✭ 119 (+815.38%)
Mutual labels:  wxss, wxml
Wemark
微信小程序Markdown渲染库
Stars: ✭ 1,159 (+8815.38%)
Mutual labels:  wxss, wxml
Weapp demos
持续更新中的微信小程序和小游戏的源码案例库。目前涵盖了120多个微信小程序或小游戏。
Stars: ✭ 2,466 (+18869.23%)
Mutual labels:  wxss, wxml
Wxapp Redux Starter
微信小程序,集成redux,并且提供了方便快捷的开发环境;内置好奇心日报Demo...
Stars: ✭ 205 (+1476.92%)
Mutual labels:  wxss, wxml
Autocomplete Wx
一款atom插件,支持微信语法高亮和代码补全
Stars: ✭ 47 (+261.54%)
Mutual labels:  wxss, wxml
Oncelove
婚礼请柬小程序
Stars: ✭ 209 (+1507.69%)
Mutual labels:  wxss, wxml
Hswiper Wx
微信小程序swiper插件
Stars: ✭ 167 (+1184.62%)
Mutual labels:  wxss, wxml
Mpvue Loader
mpvue loader
Stars: ✭ 47 (+261.54%)
Mutual labels:  wxss, wxml
Wxss
微信签到小程序,包含照片、地点、人脸验证
Stars: ✭ 135 (+938.46%)
Mutual labels:  wxss
Alaweb
一套 Vue 代码,多端可用(H5、小程序、苹果App、安卓App、头条等)。系统含150+页面,200+组件(5端通用),30+元件(每个终端独立完成)
Stars: ✭ 837 (+6338.46%)
Mutual labels:  wxss
Vscode Miniapp Helper
微信小程序开发助手 for VSCode
Stars: ✭ 137 (+953.85%)
Mutual labels:  wxml
Weui Wxss
A UI library by WeChat official design team, includes the most useful widgets/modules.
Stars: ✭ 14,061 (+108061.54%)
Mutual labels:  wxss
M Mall
🐶 微信小程序-小商城前台(基于 WeUI.wxss、ES6 前端技术开发...)
Stars: ✭ 1,484 (+11315.38%)
Mutual labels:  wxss

微信小程序组件化方案示例

  • 由于官方已经出了组件化方案,所以此库作废。

由于微信小程序中只提供了template,而template仅仅是视图模板,我们其实想要的组件是包含视图(wxml和wxss)和逻辑(js)的。网上有第三方框架可以组件化,但是看了看,好复杂。而且结合到我们项目中,由于有1M的限制,实在无法过多使用第三方框架了。所以,有了下文。。。如果哪位大神有更好的方案,欢迎交流。

其实原理很简单,就是合并。做法分三步:

  • 子组件的wxml作为模板include到父容器中
  • 子组件的wxss import到父容器的wxss中
  • 把父容器的data和方法与子组件的data和方法合并(注意:合并的data及方法名不能重名)

看看效果:

在子组件的input框中输入内容,父容器中显示input的内容,父容器中点击清空按钮后子组件input内容清空。

运行截图

详细说明

组件部分

1.js部分把data及方法export出去供调用侧使用。

module.exports = {
    data: {
        childInputVal: ''
    },
    inputChange: function(event) {
        let inputVal = event.detail.value;
        this.setData({
            childCompVal: inputVal,
            childInputVal: inputVal
        });
    }
}

详细代码: components/myComponent/index.js

2.wxml部分。

<view class="component-container">
    <view class="desc">子组件</view>
    <input 
        value="{{childInputVal}}"
        placeholder="输入内容后自动更新父容器文本内容"
        bindinput="inputChange" />
</view>

详细代码: components/myComponent/index.wxml

3.wxss部分

.component-container{
    width: 90%;
    height: 30%;
    border: 1px solid #cdcdcd;
    border-radius: 15px;
    padding: 5px;
    margin-top: 70px;
}
.desc{
    margin-top: 10px;
    text-align: center;
}
input{
  width: 100%;
  margin-top: 20px;
}

详细代码: components/myComponent/index.wxss

父容器部分

1.容器中wxml部分

在wxml中include上面的wxml

<view class="container">
  <text>父容器</text>
  <view class="button-container">
    <button bindtap="clearInput" type="primary" size="default" >清空</button>
  </view>
  <view class="value-container">
    {{childCompVal}}
  </view>
  <!-- 引入子组件wxml -->
  <include src="../../components/myComponent/index.wxml" />
</view>

详细代码: pages/index/index.wxml

2.容器中wxss部分

在wxss中import上面的wxss文件

.button-container {
  margin-top: 10px;
  width: 90%;
}

.value-container {
  margin-top: 30px;
  width: 90%;
  padding: 5px;
  border: 1px solid #cdcdcd;
  border-radius: 15px;
  color: #333;
}

/** 引入子组件样式 **/
@import "../../components/myComponent/index.wxss"

详细代码: pages/index/index.wxss

3.容器中js部分

1.引入子组件的js文件

var myComponent = require('../../components/myComponent/index');

2.把原来Page({...})中的代码移出

    // 原来
	Page({
		data:{
			data1: 'data1',
			data2: 'data2'
		},
		func1: function() {...}
	});

    // 改成
    var pageObj = {
		data:{
			data1: 'data1',
			data2: 'data2'
		},
		func1: function() {...}
	};
    Page(pageObj);

3.在调用Page(pageObj)之前,把引入的共通内容合并到进pageObj中

    for (let compKey in compObj) {
        if (compKey == 'data') {
            // 合并页面中的data
            let data = compObj[compKey];
            for(let dataKey in data) {
                pageObj.data[dataKey] = data[dataKey];
            }
        } else {
            // 合并页面中的方法
            pageObj[compKey] = compObj[compKey];
        }
    }
	Page(pageObj);

详细代码: pages/index/index.js

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].