微信小程序的学习总结

小程序包含一个描述整体程序的 app 和多个描述各自页面的 page

一.微信小程序的文件结构:

一个小程序主体部分由三个文件组成,必须放在项目的根目录,分别是app.js,app.json,app.wxss;

一个小程序页面由四个文件组成,分别是:js文件,wxml文件,json文件,wxss文件;各自的作用依次是页面逻辑、

页面结构、页面配置、页面样式表;

二.微信小程序模板的使用:

WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用。

那么问题来了,如何使用模板呢?

1.定义模板:使用 name 属性,作为模板的名字。然后在<template/>内定义代码片段,如:

<!--
  index: int
  msg: string
  time: string
-->
<template name="msgItem">
  <view>
    <text> {{index}}: {{msg}} </text>
    <text> Time: {{time}} </text>
  </view>
</template>

2.使用模板:使用 is 属性,声明需要的使用的模板,然后将模板所需要的 data 传入,如:

<template is="msgItem" data="{{...item}}"/>

下面是js中代码:

Page({
  data: {
    item: {
      index: 0,
      msg: 'this is a template',
      time: '2016-09-15'
    }
  }
})

注意:上面是教你如何定义和使用代码,但是在使用模板之前我们需要进行引用。

3.引用模板:WXML 提供两种文件引用方式importinclude

首先来说说import:

import可以在该文件中使用目标文件定义的template,如:

在 item.wxml 中定义了一个叫itemtemplate

<!-- item.wxml -->
<template name="item">
  <text>{{text}}</text>
</template>



在 index.wxml 中引用了 item.wxml,就可以使用item模板:

<import src="item.wxml"/>
<template is="item" data="{{text: 'forbar'}}"/>

include:

include 可以将目标文件除了 <template/> <wxs/> 外的整个代码引入,相当于是拷贝到 include 位置,如:

<!-- index.wxml -->
<include src="header.wxml"/>
<view> body </view>
<include src="footer.wxml"/>


<!-- header.wxml -->
<view> header </view>


<!-- footer.wxml -->
<view> footer </view>

WXSS使用template模板的方式:

可以直接通过@import进行引用;如:

@import "文件路径";

三.弹性盒子模型:

可以在<view>组件中定义一个class属性,然后定义它的wxss代码中把display定义为"display:flex;"就行了;如:

.post-cntainer{
/**弹性盒子模型**/
display: flex;
flex-direction: column;
margin-top: 20rpx;
margin-bottom: 40rpx;
background-color: #fff;
/**上下边距**/
border-bottom: 1px solid #ededed;
border-top: 1px solid #ededed;
}

猜你喜欢

转载自blog.csdn.net/coder150806/article/details/84202177
今日推荐