飞书小程序 注意事项 大致目录结构 ├── pages │ │── home │ │ ├── home.ttml //页面结构(必须) │ │ ├── home.js //页面逻辑(必须) │ │ ├── home.json //页面结构 │ │ └── home.ttss //页面样式表 │ └── user │ ├── user.ttml │ └── user.js ├── app.js //小程序入口逻辑(必须) ├── app.json //小程序公共设置,例如:所有页面路径等(必须) ├── app.ttss //小程序公共样式(必须) └── project.config.json
飞书小程序
注意事项
- 小程序框架的逻辑层并非运行在浏览器中,因此 JavaScript 在 web 中一些能力都无法使用,如 window,document 等
- 所有组件与属性都是小写,以连字符`-`连接
- 事件传参时 以 data- 开头,多个单词由连字符-连接,不能有大写(大写会自动转成小写)如 data-element-type,最终在 event.currentTarget.dataset 中会将连字符转成驼峰 elementType。
- 在 properties 定义中,属性名采用驼峰写法(propertyName);在 ttml 中,指定属性值时则对应使用连字符写法(my-component property-name="value"),应用于数据绑定时采用驼峰写法(attr="{{propertyName}}")
大致目录结构
├── pages
│ │── home
│ │ ├── home.ttml
│ │ ├── home.js
│ │ ├── home.json
│ │ └── home.ttss
│ └── user
│ ├── user.ttml
│ └── user.js
├── app.js
├── app.json
├── app.ttss
└── project.config.json
基本介绍
程序与页面
- App
pages里配置的第一个值,就是小程序应用的打开时看到的第一个页面。
{
"pages":[
"pages/home/home",
"pages/index/index"
]
}
- 当小程序启动之后,会触发
app.js里定义的onLaunch方法
App({
onLaunch: function () {
}
})
- Page
Page({
data: {
},
viewTap: function() {
this.setData({
text: 'Set some data for updating view.'
}, function() {
})
},
onLoad: function (options) {
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})
父子组件
父子传值
{
"usingComponents": {
"child":"/components/child/child"
}
}
.....
<child parmasss="传递的参数" bindtoParmas="getParmas"></child>
.....
Page({
getParmas(e){
console.log(e);
}
})
.....
<view class="my-custom-component">
<view class="content">
<view>{{parmasss}}</view>
<button bindtap="toPramas">给父组件传值</button>
</view>
</view>
.....
Component({
properties: {
parmasss:{
type:String,
value:'properties',
observer(newval,oldVal, changedPath){
console.log(newval,oldVal, changedPath)
}
}
},
methods: {
toPramas(){
this.triggerEvent('toParmas',111111, { bubbles: false });
}
}
})
插槽slot
{
"usingComponents": {
"child":"/components/child/child"
}
}
.....
<child>
<view>slot的值</view>
<view slot="aa">name为aa的slot的值</view>
<view slot="bb">name为bb的slot的值</view>
</child>
.....
.....
<view class="my-custom-component">
<view class="content">
<slot></slot>
<slot name="aa"></slot>
<slot name="bb"></slot>
</view>
</view>
.....
使用模板
<import src="../../template/test-template.ttml" />
<template is="test" data="{{a:'传给模板的a',b:'传给模板的b'}}"/>
<template is="test1"/>
<include src="../../template/test-template.ttml" />
<template name="test">
<view>我是test的模板</view>
<view>{{a}}</view>
<view>{{b}}</view>
</template>
<template name="test1">
<view>我是test1的模板</view>
<view>{{b}}</view>
</template>
<view>我是没有template标签包裹的元素</view>
使用sjs
<sjs module="test" src="../../sjs/test-sjs.sjs" />
<view>{{test.fullName(1,2)}}</view>
<view>{{test2.fullName(1,'2')}}</view>
<view>{{fullName(1,2)}}</view>
<view>双大括号不能使用js定义的方法</view>
<sjs module="test2">
function fullName(a, b) {
return a + b;
}
module.exports = { fullName: fullName }
</sjs>
Page({
fullName(a, b) {
return a + b;
},
})
function fullName(a, b) {
return a + b;
}
module.exports = {
fullName: fullName
};