- 1. 微信小程序介绍
- 2. 环境准备
- 3. 第一个微信小程序
- 4. 微信开发者工具介绍
- 5. 小程序结构目录
- 6. 小程序配置文件
- 7. 模板语法
- 8. 小程序事件的绑定
- 9. 样式 WXSS
- 10. 常见组件
- 11. 自定义组件
- 12. 小程序生命周期
传智播客-黑马程序员
Mini Program
Mini Program
传智播客-黑马程序员
建议使用全新的邮箱,没有注册过其他小程序或者公众号的。
传智播客-黑马程序员
APPID
APPID
????
传智播客-黑马程序员
传智播客-黑马程序员
vs code
vs code
微信小程序编辑工具
微信小程序编辑工具
vs code
vs code
微信编辑工具
微信编辑工具
传智播客-黑马程序员
传智播客-黑马程序员
传智播客-黑马程序员
WXML
WXML
WXSS
WXSS
JavaScript
JavaScript
传智播客-黑马程序员
app.json
app.json
page.json
page.json
注意:配置文件中不能出现注释
app.json
app.json
app.json
app.json
传智播客-黑马程序员
pages
pages
window
window
page.json
page.json
page.json
page.json
app.json
app.json
window
window
app.json
app.json
window
window
{
"pages":[
"pages/index/index",
"pages/logs/logs"
],
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle":"black"
}
}
传智播客-黑马程序员
#000000
#000000
black
black
white
white
dark
dark
light
light
true
true
app.json
app.json
sitemap.json
sitemap.json
<view> {{ message }} </view>
Page({
data: {
message: 'Hello MINA!'
}
})
<view id="item-{{id}}"> </view>
Page({
data: {
id: 0
}
})
<checkbox checked="{{false}}"> </checkbox>
传智播客-黑马程序员
item
item
wx:for
wx:for
item
item
index
index
wx:for
wx:for
index
index
wx:key
wx:key
wx:key
wx:key
string
string
<view hidden="{{flag ? true : false}}"> Hidden </view>
<view> {{a + b}} + {{c}} + d </view>
Page({
data: {
a: 1,
b: 2,
c: 3
}
})
<view wx:if="{{length > 5}}"> </view>
<view>{{"hello" + name}}</view>
Page({
data:{
name: 'MINA'
}
})
传智播客-黑马程序员
*this
*this
item
item
wx:if="{{condition}}"
wx:if="{{condition}}"
wx:if
wx:if
hidden
hidden
list:[{id:0,name:"炒饭"},{id:1,name:"炒面"}]
wx:key="id"
list:[1,2,3,4,5]
wx:key="*this"
<view wx:for="{{array}}" wx:key="id">
{{index}}: {{item.message}}
</view>
Page({
data: {
array: [{
id:0,
message: 'foo',
}, {
id:1,
message: 'bar'
}]
}
})
<block wx:for="{{[1, 2, 3]}}" wx:key="*this" >
<view> {{index}}: </view>
<view> {{item}} </view>
</block>
<view wx:if="{{false}}">1</view>
<view wx:elif="{{true}}">2</view>
<view wx:else>3</view>
<view hidden="{{condition}}"> True </view>
传智播客-黑马程序员
wx:if
wx:if
bindtap
bindtap
bindinput
bindinput
bindchange
bindchange
value
value
WeiXin Style Sheets
WeiXin Style Sheets
WXML
WXML
rpx
rpx
<input bindinput="handleInput" />
Page({
// 绑定的事件
handleInput: function(e) {
console.log(e);
console.log("值被改变了");
}
})
<input bindinput="handleInput(100)" />
<input bindinput="handleInput" data-item="100" />
handleInput: function(e) {
// {item:100}
console.log(e.currentTarget.dataset)
// 输入框的值
console.log(e.detail.value);
}
传智播客-黑马程序员
rpx
rpx
750rpx
750rpx
iPhone6
iPhone6
375px
375px
750rpx = 375px = 750物理像
750rpx = 375px = 750物理像
素
素
1rpx = 0.5px = 1物理像素
1rpx = 0.5px = 1物理像素
750rpx = pageWidth px
750rpx = pageWidth px
1px=750rpx/pageWidth
1px=750rpx/pageWidth
px
px
750/pageWidth rpx
750/pageWidth rpx
@import
@import
*
*
/** common.wxss **/
.small-p {
padding:5px;
}
/** app.wxss **/
@import "common.wxss";
.middle-p {
padding:15px;
}
*{
margin:0;
padding:0;
box-sizing:border-box;
}
传智播客-黑马程序员
.intro
.intro
#firstname
#firstname
view
view
view,
view,
checkbox
checkbox
view::after
view::after
view::before
view::before
less
less
wepy
wepy
mpvue
mpvue
taro
taro
vscode
vscode
easy less
easy less
less
less
index.less
index.less
view,text,rich
view,text,rich
text,button,image,navigator,icon,swiper,radio,checkbox。
text,button,image,navigator,icon,swiper,radio,checkbox。
div
div
"less.compile": {
"outExt": ".wxss"
}
<view hover-class="h-class">
点击我试试
</view>
传智播客-黑马程序员
<text selectable="{{false}}" decode="{{false}}">
普 通
</text>
传智播客-黑马程序员
微信内置轮播图组件
传智播客-黑马程序员
传智播客-黑马程序员
滑块
导航组件 类似超链接标签
富文本标签
v
v
html
html
传智播客-黑马程序员
// 1 index.wxml 加载 节点数组
<rich-text nodes="{{nodes}}" bindtap="tap"></rich-text>
// 2 加载 字符串
<rich-text nodes='<img
src="https://developers.weixin.qq.com/miniprogram/assets/images/[email protected]
ng" alt="">'></rich-text>
// index.js
Page({
data: {
nodes: [{
name: 'div',
attrs: {
class: 'div_class',
style: 'line-height: 60px; color: red;'
},
children: [{
type: 'text',
text: 'Hello World!'
}]
}]
},
tap() {
console.log('tap')
}
})
传智播客-黑马程序员
nodes
nodes
字符串
字符串
标签节点数组
标签节点数组
nodes
nodes
String
String
rich
rich
text
text
attrs
attrs
name
name
HTML
HTML
img
img
<button
type="default"
size="{{defaultSize}}"
loading="{{loading}}"
plain="{{plain}}"
>
default
</button>
传智播客-黑马程序员
传智播客-黑马程序员
appid
appid
appid
appid
客服
客服
微信
微信
传智播客-黑马程序员
Page({
data: {
iconSize: [20, 30, 40, 50, 60, 70],
iconType: [
'success', 'success_no_circle', 'info', 'warn', 'waiting', 'cancel',
'download', 'search', 'clear'
],
iconColor: [
'red', 'orange', 'yellow', 'green', 'rgb(0,255,255)', 'blue', 'purple'
],
}
})
<view class="group">
<block wx:for="{{iconSize}}">
<icon type="success" size="{{item}}"/>
</block>
</view>
<view class="group">
<block wx:for="{{iconType}}">
<icon type="{{item}}" size="40"/>
</block>
</view>
<view class="group">
<block wx:for="{{iconColor}}">
<icon type="success" size="40" color="{{item}}"/>
</block>
</view>
传智播客-黑马程序员
可以通过 color属性来修改颜色
可以通过 color属性来修改颜色
类似vue或者react中的自定义组件
类似于页面,一个自定义组件由
json
json
wxml
wxml
wxss
wxss
js
js 4个文件组成
components/myHeader
components/myHeader
myHeader
myHeader
传智播客-黑马程序员
json
json
myHeader.json
myHeader.json
wxml
wxml
wxss
wxss
slot
slot
myHeader.wxml
myHeader.wxml
wxss
wxss
注意:在组件wxss中不应使用ID选择器、属性选择器和标签名选择器。
myHeader.wxss
myHeader.wxss
js
js
Component()
Component()
myHeader.js
myHeader.js
{
"component": true
}
<!-- 这是自定义组件的内部WXML结构 -->
<view class="inner">
{{innerText}}
<slot></slot>
</view>
/* 这里的样式只应用于这个自定义组件 */
.inner {
color: red;
}
Component({
properties: {
// 这里定义了innerText属性,属性值可以在组件使用时指定
innerText: {
// 期望要的数据是 string类型
type: String,
传智播客-黑马程序员
json
json
index.wxml
index.wxml
Component
Component
Component
Component
value: 'default value',
}
},
data: {
// 这里是一些组件内部数据
someData: {}
},
methods: {
// 这里是一个自定义方法
customMethod: function(){}
}
})
{
// 引用声明
"usingComponents": {
// 要使用的组件的名称 // 组件的路径
"my-header":"/components/myHeader/myHeader"
}
}
<view>
<!-- 以下是对一个自定义组件的引用 -->
<my-header inner-text="Some text">
<view>用来替代slot的</view>
</my-header>
</view>
传智播客-黑马程序员
properties
properties
setData
setData
{{tabs}}
{{tabs}}
tabItems
tabItems
onMyTab
onMyTab
bindmytap
bindmytap
mytap
mytap
triggerEvent
triggerEvent
事件名
事件名
detail
detail
传智播客-黑马程序员
this.selectComponent("#tabs")
this.selectComponent("#tabs")
// page.wxml
<tabs tabItems="{{tabs}}" bindmytap="onMyTab" >
内容-这里可以放插槽
</tabs>
// page.js
data: {
tabs:[
{name:"体验问题"},
{name:"商品、商家投诉"}
]
},
onMyTab(e){
console.log(e.detail);
},
// com.wxml
<view class="tabs">
<view class="tab_title" >
<block wx:for="{{tabItems}}" wx:key="{{item}}">
<view bindtap="handleItemActive" data-index="{{index}}">{{item.name}}</view>
</block>
</view>
<view class="tab_content">
<slot></slot>
</view>
</view>
// com.js
Component({
properties: {
tabItems:{
type:Array,
value:[]
}
},
/**
* 组件的初始数据
*/
data: {
},
/**
* 组件的方法列表
*/
methods: {
handleItemActive(e){
this.triggerEvent('mytap','haha');
}
}
})
传智播客-黑马程序员
myHeader.js
myHeader.js
innerText
innerText
关于小程序前后台的定义和小程序的运行机制,请参考运行机制章节。
传智播客-黑马程序员