git clone https://github.com/Lizhhhh/miniProgram.git
地址
一种不需要下载安装即可使用的应用,它实现了应用"触手可及"的梦想,用户扫一扫或搜一下即可打开应用。也体现了"用完即走"的理念,用户不用关心是否安装太多应用的问题。应用将无处不在,随时可用,但又无序安装卸载
[微信小程序的特点] :
登录微信公众平台
一个账户对应一个小程序
企业、政府、媒体、其他组织主体可用注册50个小程序;
个体户和个人类型主体可注册5个小程序
App: 整个小程序称为 app
一个App 包含 多个pages
每个page包含多个组件
进入项目根目录: git init
将当前的项目添加到暂存区中: git add .
(注意: 最后有一个点)
将暂存区的内容放到本地仓库: git commit -m '初始化项目'
登录github , 新建远程仓库
在本地添加远程仓库的源: git remote origin https://github.com/Lizhhhh/miniProgram.git
将本地代码提交到远程: git push -u origin master
git add .
git commit -m '知识点1'
git tag 01_知识点1
git tag
来查看项目中的标签.$ git tag
01_知识点1
git log
commit 75c6d6bdfa063322ce728b98ab4dc20724efc02d (HEAD -> master, tag: 01_知识点1)
Author: 栗子好好吃 <[email protected]>
Date: Sat Feb 15 13:58:23 2020 +0800
知识点1
commit f98bf38a589ce6c696a844f41a81be9e554714ba (origin/master)
Author: 栗子好好吃 <[email protected]>
Date: Sat Feb 15 14:41:22 2020 +0800
初始化项目
找到项目初始化的id,然后进行版本回退: git reset --hard f98bf38
此时项目处于初始化的状态.你可以对项目进行修改…
修改完成后,将新的代码提交到本地:git add .
--> git commit -m '知识点2'
--> git tab 02_知识点2
此时: git tag
, 当前的代码仅在本地,而在远程中没有.下面需要将tags推到远程中
$ git tag
01_知识点1
02_知识点2
git push --tags
$ git push --tags
Enumerating objects: 14, done.
Counting objects: 100% (14/14), done.
Delta compression using up to 8 threads
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 869 bytes | 434.00 KiB/s, done.
Total 10 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), completed with 2 local objects.
To https://github.com/Lizhhhh/miniProgram.git
* [new tag] 01_知识点1 -> 01_知识点1
* [new tag] 02_知识点2 -> 02_知识点2
登录远程仓,找到克隆的地址: https://github.com/Lizhhhh/miniProgram.git
将远程仓库的代码拷贝到本地: git clone
$ git clone https://github.com/Lizhhhh/miniProgram.git
Cloning into 'miniProgram'...
remote: Enumerating objects: 26, done.
remote: Counting objects: 100% (26/26), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 26 (delta 8), reused 25 (delta 7), pack-reused 0
Unpacking objects: 100% (26/26), done.
tag 01_知识点1
的代码,可以在命令行输入:git checkout 01_知识点1
$ git checkout 01_知识点1
Note: checking out '01_知识点1'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b
HEAD is now at 4e8281e 知识点1
M app.json
M project.config.json
tag 02_知识点2
的代码,可以在命令行输入:git checkout 02_知识点2
git reset --hard
$ git reset --hard f98bf38
HEAD is now at f98bf38 初始化项目
// home.js
Page({
data:{
name: 'Codewhy',
age: '18'
}
})
// home.wxml
<view>Hello {{name}}view>
<view>我的年龄 {{age}}view>
Page({
handltBtnClick(){
console.log('按钮发生了点击')
}
})
<button bindtap="handleBtnClick">+button>
<view>当前计数: {{counter}}view>
<button bindtap="handleBtnClick">+button>
// 改变数据,要通过setData才能进行响应式变化
Page({
handleBtnClick({
this.setData({
counter: this.data.counter + 1
})
})
})
Vue中的MVVM架构:
小程序的MVVM架构:
MVVM为什么好用?
MVVM架构将我们从命令式编程转移到声明式编程
<body>
<h2 class="title">h2>
<button class="btn">按钮button>
body>
<script>
let name = '哈哈';
const $title = document.querySelector('.title');
$title.textContent = name;
const $btn = document.quertSelector('.btn');
$btn.addEventListener('click',()=>{
name='呵呵';
$btn.textContent = name;
})
script>
<body>
<div id="app">
<h2> {{title}} h2>
<button @click="btnClick"> 按钮 button>
div>
<script src="./lib/vue.js">script>
<script>
new Vue({
el: '#app',
data: {
title: '哈哈'
},
methods:{
btnClick(){
this.title= "呵呵"
}
}
})
script>
body>
小程序的很多开发需求被规定在了配置文件中
这样做的好处:
常见的配置文件:
// 允许微信的爬虫爬取小程序中的所有页面
{
"rules":[{
"action": "allow",
"page": "*"
}]
}
// 配置 path/to/page 页面被索引,其余页面不被索引
{
"rules":[{
"action": "allow",
"page": "path/to/page"
},{
"action": "disallow",
"apge:" "*"
}]
}
app.json
: 全局配置
pages
: 页面路径列表
window
: 全局的默认窗口展示,下面展示常用的
属性 | 作用 |
---|---|
navigationBarBackgroundColor | tab栏的背景色 |
navigationBarTextStyle | tab栏的字体颜色 |
navigationBarTitleText | tab栏中的标题 |
backgroundColor | tab栏下拉时候留白的背景色 |
backgroundTextStyle | 设置下拉的闪动的样式 |
enablePullDownRefresh | 是否可以向下拉动 |
tabBar:底部tab栏的展示
page.json
: 页面配置
<view>
<view>aview>
<view>bview>
<view>cview>
view>
{
name:'view',
children:[
{ name: 'view', children: [{text: 'a'}]},
{ name: 'view', children: [{text: 'b'}]},
{ name: 'view', children: [{text: 'c'}]}
]
}
<view>{{msg}}view>
<script>
{
msg: 'Hello World'
}
script>
{
name: 'view',
children:[
{ text: 'Hello World' }
]
}
通过setData把msg数据从"Hello World"变成"呵呵呵"
界面渲染整体流程:
【小结】:
小程序在启动过程中,首先会在根目录中优先加载app.json
加载完app.json之后,会去注册App
将app.json读取完毕之后,就会去找app.js
在app.js中会传入一个对象字面量给App,其中包含小程序的生命周期函数:
App({
// 小程序初始化完成时
onLaunch: function(){
// 一般发送一些网络请求
// 获取用户信息
},
// 小程序从后台显示出时
onShow: function(options){
// 判断小程序的进入场景
},
// 小程序隐藏时
onHide: function(){
},
// 小程序产生一些错误时
onError: function(msg){
},
// 加载页面但找不到时
onPageNotFound: function(){
}
})
Page({
// 页面被加载时
onLoad(){
// 一般在这里发送网络请求,得到数据
wx.request({
url: 'http://123.207.32.32:8000/recommend',
success: res=>{
console.log(res)
}
})
},
// 页面初次渲染完成时,回调
onReady(){
},
// 页面显示出来时,回调
onShow(){
},
// 页面隐藏时
onHide(){
},
// 页面卸载时,页面跳转时,返回上一级页面时触发的函数
onUnload(){
}
})
// pages/home/home.js
Page({
data:{
message:"marron",
list: [
{name: 'zs', age: 18},
{name: 'ls', age: 19}
]
}
})
<view>{{message}}view>
<view wx:for="list">
{{item.name}} - {{item.age}}
view>
<button size="mini" bindtap="handleClick">按钮button>
<view bindtap="handleClickView">viewview>
// pages/home/home.js
Page({
handleClick(){
console.log('点')
},
handleClickView(){
console.log('点点')
}
})
// pages/home/home.js
onPageScroll(obj){
conosle.log(obj)
}
// pages/home/home.js
onReachBottom(){
console.log('滚动到底部')
}
onReady
事件rerender
onHide
事件onShow
事件onUnload
事件属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
selectable | boolean | false | 否 | 文本是否可选 |
space | string | 否 | 显示连续空格 | |
decode | boolean | false | 否 | 是否解码 |
<text>Hello World\ntext>
<text>你好,小程序text>
<text selectable="{{true}}">\n可以选中的文本text>
<text selectable>\n可以选中的文本text>
<text selectable>\n可以选中的文本text>
<text selectable>\n可以选中的文本text>
<text selectable>\n可以选中的文本text>
<text selectable>\n可以选中的文本text>
<text selectable>\n可以选中的文本text>
<text>\n长按不能选中text>
<text>\n长按不能选中text>
<text>\n长按不能选中text>
<text>\n长按不能选中text>
<text>\n长按不能选中text>
<text>\n长按不能选中text>
<text>\n长按不能选中text>
<text>\n长按不能选中text>
<text>\n长按不能选中text>
<text>\nHello World \ntext>
<text space="ensp">Hello World \ntext>
<text space="emsp">Hello World \ntext>
<text space="nbsp">Hello World \ntext>
<text> 5 > 3text>
<text decode> 5 > 3text>
Button组件用于创建按钮,默认块级元素
常见属性:
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
size | string | default | 否 | 按钮的大小 |
type | string | default | 否 | 按钮的样式类型 |
plain | boolean | false | 否 | 按钮是否镂空,背景色透明 |
disabled | boolean | false | 否 | 是否禁用 |
loading | boolean | false | 否 | 名称前是否带loading图标 |
form-type | string | 否 | 用于 组件,点击会触发 组件的 submit/reset 事件 |
|
open-type | string | 否 | 微信开放能力 | |
hover-class | string | button-hover | 否 | 指定按钮按下去的样式类,当hover-class="none" 时,没有点击态效果 |
<view class="row">
<view>1.按钮的基本使用view>
<button>按钮button>
view>
<view class="row">
<view>2.size: miniview>
<button size="mini">mini按钮button>
<button size="mini">mini按钮button>
view>
<view class="row">
<view>3.type: primary、default、warnview>
<button size="mini" type="primary">mini按钮 primarybutton>
<button size="mini" type="default">mini按钮 defaultbutton>
<button size="mini" type="warn">mini按钮 warnbutton>
view>
<view class="row">
<view>4.plain: 镂空效果view>
<button size="mini" type="primary">mini按钮 primary 非镂空button>
<button size="mini" type="primary" plain>mini按钮 primary 镂空button>
view>
<view class="row">
<view>5.disable: 不可用view>
<button size="mini">按钮button>
<button size="mini" disabled>按钮禁用button>
view>
<view class="row">
<view>6.loading: 加载view>
<button size="mini">按钮button>
<button size="mini" loading="{{isLoading}}">loading 按钮button>
view>
<view class="row">
<view>7.hover-class: 点击时候的样式 view>
<button hover-class="pressed">按钮button>
view>
<view class="box">哈哈哈view>
<view>呵呵呵view>
<view class="box1" hover-class="boxhover">box1view>
<view class="box1" hover-class="boxhover" hover-stay-time="{{0}}">box1view>
<view class="box1" hover-class="boxhover" hover-start-time="{{3000}}">box1view>
<view class="father" hover-class="fatherHover">
<view class="son" hover-class="sonHover">
未阻止事件冒泡
view>
view>
<view class="father" hover-class="fatherHover">
<view class="son" hover-class="sonHover" hover-stop-propagation>
阻止事件冒泡
view>
view>
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
src | string | 否 | 图片资源地址 | |
mode | string | scaleToFill | 否 | 图片裁剪、缩放的模式 |
lazy-load | boolean | false | 否 | 图片懒加载,在即将进入一定范围(上下三屏)时才开始加载 |
binderror | eventhandle | 否 | 当错误发生时触发,event.detail = {errMsg} | |
bindload | eventhandle | 否 | 当图片载入完毕时触发,event.detail = {height, width} |
,也可以写成双标签
<image src='/assets/pics/1.jpg' />
<image src='../../assets/pics/1.jpg' />
<image src="https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg" />
<button bindtap="handleChooseAlbum">选中button>
<image src="{{imagePath}}" />
<view>--------------------view>
<image
src="https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg" bindload="handImageLoad"
lazy-load
/>
<image src="/assets/pics/17.jpg" show-menu-by-longpress />
<view>
<view>图片的适配请看官方网站view>
<view>https://developers.weixin.qq.com/miniprogram/dev/component/image.html view>
view>
// pages/image/image.js
Page({
/**
* 页面的初始数据
*/
data: {
imagePath: '',
imagePath1: '',
},
handleChooseAlbum() {
// 系统API,让用户在相册中选择图片(或者拍照)
wx.chooseImage({
success: res => {
// 1.取出路径
const path = res.tempFilePaths[0]
// 设置路径
this.setData({
imagePath: path
})
},
})
},
handleChooseAlbum1() {
// 系统API,让用户在相册中选择图片(或者拍照)
wx.chooseImage({
success: res => {
// 1.取出路径
const path = res.tempFilePaths[0]
// 设置路径
this.setData({
imagePath1: path
})
},
})
},
handImageLoad(){
console.log('图片加载完成')
}
})
<input />
<input value="Marron" />
<input type="idcard" />
<input password />
<input placeholder="请输入内容" />
<input bindinput="handleInput"
bindfocus="handleFocus"
bindblur="handleBlur"
/>
bindtap
<button size="mini" bindtap="handleGet">点击试试button>
// pages/home/home.js
Page({
handleClick(e){
console.log(e)
}
})
open-type && bindgetuserinfo
<button size="mini" open-type="getUserInfo" bindgetuserinfo="handleGetUserInfo">
获取授权
button>
Page({
handleGetUserInfo(e) {
console.log(e)
}
})
open-data
<open-data type="userNickName">open-data>
<open-data type="userAvatarUrl">open-data>
// app.js
App({
globalData:{
name: 'marron',
age: 18
}
})
// pages/home/home.js
const app = getApp();
const name = app.globalData.name;
const age = app.globalData.age;
Page({
data:{
name,
age
}
})
<view>{{name}} : {{age}}view>
注: 上述的globalData可以是任意的非保留字符串,如改为globalData111
[场景] : 在home.js中,发送网络请求,但是没有在微信小程序控制台设置允许访问的域名,可以在微信提供的IDE中选择详情 -> 勾选不校验合法域名、web-view(业务域名)、LTS版本以及HTTPS证书
// home.js
Page({
// 页面被加载时触发的生命周期函数
onLoad(){
// 一般在这里发送网络请求
wx.request({
url: 'http://123.207.32.32:8000/recommend',
success: res=>{
console.log(res)
}
})
}
})
[场景] : 数据的变化没有对应的改变视图,需要使用微信提供的setData
方式,让数据的变化影响到视图
// home.js
Page({
onLoad(){
wx.request({
url: 'http://123.207.32.32:8000/recommend',
success: res=>{
this.setData({
list: data
})
}
})
}
})
[场景] : 将数组循环显示到视图
<view wx:for="{{list}}">
{{item.name}} - {{item.age}}
view>
[场景] : 手机下拉刷新获取数据
// pages/home/home.json
{
"enablePullDownRefresh": true
}
// pages/home/home.js
Page({
onPullDownRefresh(){
console.log('下拉刷新的事件')
}
})
<button bindtap="handleChooseAlbum">选中button>
<image src="{{imagePath}}" />
// pages/image/image.js
Page({
data:{
imagePath: ''
},
handleChooseAlbum(){
// 系统API,让用户在相册中选择图片或者拍照
wx.chooseImage({
success: res=>{
const path = res.tempFilePaths[0];
this.setData({
imagePath: path;
})
}
})
}
})