微信小程序开发successthen(微信小程序开发工具)

小程序开发 1336
本篇文章给大家谈谈微信小程序开发successthen,以及微信小程序开发工具对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 本文目录一览: 1、微信小程序如何开发呢,有没有知道的

本篇文章给大家谈谈微信小程序开发successthen,以及微信小程序开发工具对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

微信小程序如何开发呢,有没有知道的

开发前准备:

注册小程序帐号 绑定开发者

登录微信公众平台小程序,进入用户身份- 开发者,新增绑定开发者。

已认证的小程序可以绑定不多于20个开发者。未认证的小程序可以绑定不多于10个开发者。

获取AppID下载并安装开发者工具

下载完成后,使用管理员或者绑定的开发者微信号扫码登录。

一个微信小程序

创建项目

我们需要通过开发者工具,来完成小程序创建和代码编辑。

开发者工具安装完成后,打开并使用微信扫码登录。选择创建“项目”,填入上文获取到的 AppID ,设置一个本地项目的名称(非小程序名称),比如“我的第一个项目”,并选择一个本地的文件夹作为代码存储的目录,点击“新建项目”就可以了。

为方便初学者了解微信小程序的基本代码结构,在创建过程中,如果选择的本地文件夹是个空文件夹,开发者工具会提示,是否需要创建一个 quick start 项目。选择“是”,开发者工具会帮助我们在开发目录里生成一个简单的 demo。

项目创建成功后,我们就可以点击该项目,进入并看到完整的开发者工具界面,点击左侧导航,在“编辑”里可以查看和编辑我们的代码,在“调试”里可以测试代码并模拟小程序在微信客户端效果,在“项目”里可以发送到手机里预览实际效果。

编写代码创建小程序实例

点击开发者工具左侧导航的“编辑”,我们可以看到这个项目,已经初始化并包含了一些简单的代码文件。最关键也是必不可少的,是 app.js、app.json、app.wxss 这三个。其中,.js后缀的是脚本文件,.json后缀的文件是配置文件,.wxss后缀的是样式表文件。微信小程序会读取这些文件,并生成小程序实例。

//App() 函数用来注册一个小程序。接受一个 object 参数,其指定小程序的生命周期函数等。

App({

onLaunch: function() {

// Do something initial when launch.

},

onShow: function() {

// Do something when show.

},

onHide: function() {

// Do something when hide.

},

globalData: 'I am global data'

})

app.js是小程序的脚本代码。我们可以在这个文件中监听并处理小程序的生命周期函数、声明全局变量。调用框架提供的丰富的 API。

//app.js

App({

onLaunch: function() {

//调用API从本地缓存中获取数据

var logs = wx.getStorageSync('logs') || []

logs.unshift(Date.now())

wx.setStorageSync('logs', logs)

},

getUserInfo: function(cb) {

var that = this;

if (this.globalData.userInfo) {

typeof cb == "function" cb(this.globalData.userInfo)

} else {

//调用登录接口

wx.login({

success: function() {

wx.getUserInfo({

success: function(res) {

that.globalData.userInfo = res.userInfo;

typeof cb == "function" cb(that.globalData.userInfo)

}

})

}

});

}

},

globalData: {

userInfo: null

}

})

app.json 是对整个小程序的全局配置。我们可以在这个文件中配置小程序是由哪些页面组成,配置小程序的窗口背景色,配置导航条样式,配置默认标题。注意该文件不可添加任何注释。

{

"pages": [

"pages/index/index",

"pages/logs/logs"

],

"window": {

"backgroundTextStyle": "light",

"navigationBarBackgroundColor": "#fff",

"navigationBarTitleText": "WeChat",

"navigationBarTextStyle": "black"

}

}

app.wxss 是整个小程序的公共样式表。我们可以在页面组件的 class 属性上直接使用 app.wxss 中声明的样式规则。

/**app.wxss**/

.container {

height: 100%;

display: flex;

flex-direction: column;

align-items: center;

justify-content: space-between;

padding: 200rpx 0;

box-sizing: border-box;

}

创建一个人品计算器小页面

在这个教程里,我们有1个页面,即欢迎页,他们都在 pages 目录下。微信小程序中的每一个页面的【路径+页面名】都需要写在 app.json 的 pages 中,且 pages 中的第一个页面是小程序的首页。

每一个小程序页面是由同路径下同名的四个不同后缀文件的组成,如:index.js、index.wxml、index.wxss、index.json。.js后缀的文件是脚本文件,.json后缀的文件是配置文件,.wxss后缀的是样式表文件,.wxml后缀的文件是页面结构文件。

index.wxml 是页面的结构文件:

!--index.wxml--

text class='85bcc6af4cd61eb4 title'人品查看器/text

text class='28225b67681e98dd hint'为您计算当下人品/text

button bindtap="setScore" class='5b67681e98ddc340 check'点击查询/button

view class="681e98ddc340a4f4 container"

view bindtap="bindViewTap" class="98ddc340a4f4136b userinfo"

image class="c340a4f4136b91d6 userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"/image

text class="a4f4136b91d6de34 userinfo-nickname"{{userInfo.nickName}}/text

text class='681e98ddc340a4f4 score'{{score}}/text

text class='98ddc340a4f4136b info'{{info}}/text

/view

/view

本例中使用了view/、image/、text/来搭建页面结构,绑定数据和交互处理函数。

index.js 是页面的脚本文件,在这个文件中我们可以监听并处理页面的生命周期函数、获取小程序实例,声明并处理数据,响应页面交互事件等。

//index.js

//获取应用实例

var app = getApp()

Page({

data: {

score: 0,

userInfo: {}

},

//事件处理函数

setScore: function() {

var score = 60+parseInt(Math.random()*40);

var infos = [

'哇,你当下神仙附体,快去勾搭妹子',

'太阳天空照,花儿对我笑',

'喂,你是猪吗?离我远点'

];

var info;

if(score90){

info=infos[0];

}else if(score75){

info=infos[1];

}else{

info=infos[2];

}

this.setData({

score:score,

info:info

})

},

onLoad: function () {

console.log('onLoad')

var that = this

//调用应用实例的方法获取全局数据

app.getUserInfo(function(userInfo){

//更新数据

that.setData({

userInfo:userInfo

})

})

}

})

index.wxss 是页面的样式表:

/**index.wxss**/

.title{

font-size: 30px;

display: block;

padding: 10px;

font-weight: bold;

text-align: center;

}

.hint{

display: block;

padding: 10px 20px;

color:#999;

text-align: center;

}

.check{

width: 100px;

}

.userinfo {

display: flex;

flex-direction: column;

align-items: center;

}

.userinfo-avatar {

width: 128rpx;

height: 128rpx;

margin: 20rpx;

border-radius: 50%;

}

.userinfo-nickname {

color: #aaa;

text-align: center;

display: block

}

页面的样式表是非必要的。当有页面样式表时,页面的样式表中的样式规则会层叠覆盖 app.wxss 中的样式规则。如果不指定页面的样式表,也可以在页面的结构文件中直接使用 app.wxss 中指定的样式规则。

index.json 是页面的配置文件:

页面的配置文件是非必要的。当有页面的配置文件时,配置项在该页面会覆盖 app.json 的 window 中相同的配置项。如果没有指定的页面配置文件,则在该页面直接使用 app.json 中的默认配置。

运行结果如下:

手机预览

开发者工具左侧菜单栏选择"项目",点击"预览",扫码后即可在微信客户端中体验。

手机端效果

微信小程序如何实现消息提示框

微信小程序开发中toast也是重要的消息提示方式.

提示框:

wx.showToast(OBJECT)

显示消息提示框

OBJECT参数说明:

示例代码:

?

12345

wx.showToast({ title:'成功', icon:'success', duration: 2000})

wx.hideToast()

隐藏消息提示框

?

123456789

wx.showToast({ title:'加载中', icon:'loading', duration: 10000}) setTimeout(function(){ wx.hideToast()},2000)

wx.showModal(OBJECT)

显示模态弹窗

OBJECT参数说明:

示例代码:

?

123456789

wx.showModal({ title:'提示', content:'这是一个模态弹窗', success:function(res) { if(res.confirm) { console.log('用户点击确定') } }})

wx.showActionSheet(OBJECT)

显示操作菜单

OBJECT参数说明:

success返回参数说明:

示例代码:

?

12345678

wx.showActionSheet({ itemList: ['A','B', 'C'], success:function(res) { if(!res.cancel) { console.log(res.tapIndex) } }})

设置导航条

view提示:{{tip}}/view

button type="default" bindtap="showModal"点击我弹出modal对话框/button

view

modal title="modal对话框" hidden="{{modalHidden}}" confirm-text="确定" cancel-text="取消"

bindconfirm="modalBindaconfirm" bindcancel="modalBindcancel"您好,我是modal对话框/modal

/view

Page({

data:{

// text:"这是一个页面"

tip:'',

buttonDisabled:false,

modalHidden:true,

show:false

},

showModal:function(){

this.setData({

modalHidden:!this.data.modalHidden

})

},

modalBindaconfirm:function(){

this.setData({

modalHidden:!this.data.modalHidden,

show:!this.data.show,

tip:'您点击了【确认】按钮!',

buttonDisabled:!this.data.buttonDisabled

})

},

modalBindcancel:function(){

this.setData({

modalHidden:!this.data.modalHidden,

tip:'您点击了【取消】按钮!'

})

}

})

wx.setNavigationBarTitle(OBJECT)

动态设置当前页面的标题。

OBJECT参数说明:

示例代码:

?

123

wx.setNavigationBarTitle({ title:'当前页面'})

wx.showNavigationBarLoading()

在当前页面显示导航条加载动画。

wx.hideNavigationBarLoading()

隐藏导航条加载动画。

页面跳转:

wx.navigateTo(OBJECT)

保留当前页面,跳转到应用内的某个页面,使用wx.navigateBack可以返回到原页面。

OBJECT参数说明:

示例代码:

?

123

wx.navigateTo({ url:'test?id=1'})

?

123456

//test.jsPage({ onLoad:function(option){ console.log(option.query) }})

注意:为了不让用户在使用小程序时造成困扰,我们规定页面路径只能是五层,请尽量避免多层级的交互方式。

wx.redirectTo(OBJECT)

关闭当前页面,跳转到应用内的某个页面。

OBJECT参数说明:

示例代码:

?

123

wx.redirectTo({ url:'test?id=1'})

wx.navigateBack(OBJECT)

关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages()) 获取当前的页面栈,决定需要返回几层。

OBJECT参数说明:

动画:

wx.createAnimation(OBJECT)

创建一个动画实例animation。调用实例的方法来描述动画。最后通过动画实例的export方法导出动画数据传递给组件的animation属性。

注意: export 方法每次调用后会清掉之前的动画操作

OBJECT参数说明:

?

123456

var animation = wx.createAnimation({ transformOrigin:"50% 50%", duration: 1000, timingFunction:"ease", delay: 0})

animation

动画实例可以调用以下方法来描述动画,调用结束后会返回自身,支持链式调用的写法。

样式:

旋转:

缩放:

偏移:

倾斜:

矩阵变形:

动画队列

调用动画操作方法后要调用 step() 来表示一组动画完成,可以在一组动画中调用任意多个动画方法,一组动画中的所有动画会同时开始,一组动画完成后才会进行下一组动画。step 可以传入一个跟 wx.createAnimation() 一样的配置参数用于指定当前组动画的配置。

示例:

?

1

viewanimation="{{animationData}}"style="background:red;height:100rpx;width:100rpx"/view

?

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849

Page({ data: { animationData: {} }, onShow:function(){ varanimation = wx.createAnimation({ duration: 1000, timingFunction:'ease', }) this.animation = animation animation.scale(2,2).rotate(45).step() this.setData({ animationData:animation.export() }) setTimeout(function() { animation.translate(30).step() this.setData({ animationData:animation.export() }) }.bind(this), 1000) }, rotateAndScale:function () { // 旋转同时放大 this.animation.rotate(45).scale(2, 2).step() this.setData({ animationData:this.animation.export() }) }, rotateThenScale:function () { // 先旋转后放大 this.animation.rotate(45).step() this.animation.scale(2, 2).step() this.setData({ animationData:this.animation.export() }) }, rotateAndScaleThenTranslate:function () { // 先旋转同时放大,然后平移 this.animation.rotate(45).scale(2, 2).step() this.animation.translate(100, 100).step({ duration: 1000 }) this.setData({ animationData:this.animation.export() }) }})

wx.hideKeyboard()

收起键盘。

微信小程序开发中遇到的坑及解决办法

taro单独为某个项目切换taro版本环境

单独为某一个项目升级#这样做的好处是全局的 Taro 版本还是 1.x 的,多个项目间的依赖不冲突,其余项目依然可以用旧版本开发。 如果你的项目里没有安装 Taro CLI,你需要先装一个:

# 如果你使用 NPM

$ npm install --save-dev @tarojs/cli@2.x

# 如果你使用 Yarn

$ yarn add -D @tarojs/cli@2.x

echarts在小程序中滑动卡顿

由于微信小程序中,echarts的层级最高,无论设置多大层级也无法遮住echarts。而且小程序中好像只能用echarts吧。所以为了解决这个bug,我只能委屈求全了。打开ec-canvas.wxml文件,将touchStart、touchMove和touchEnd去掉了,直接删除就好啦。这三个事件应该是做缩放的吧,我们也没有这个缩放的需求。所以就去掉了。虽然暂时满足的需求,还是没有真正的解决问题。

原:

bindinit="init"

bindtouchstart="{{ ec.disableTouch ? '' : 'touchStart' }}"

bindtouchmove="{{ ec.disableTouch ? '' : 'touchMove' }}"

bindtouchend="{{ ec.disableTouch ? '' : 'touchEnd' }}"

现:

bindinit="init"

echarts在小程序中无法跟随页面滑动

在卡顿问题中能与echarts交互少的,可以直接使用图片代替cannvas,即在echarts渲染完毕后将它替换为一张图片。

如果我更新了数据,那么就重新放出echarts,等它渲染完毕后,再次替换为一张图片。

chart.on('finished', () = {

getCurrentInstance().page.selectComponent(id).canvasToTempFilePath({

success: res = {

console.log('res.tempFilePath====',res.tempFilePath)

this.setState({

echartImgSrc: res.tempFilePath

      })

},

    fail: res =console.log('转换图片失败', res)

});

})

render:

this.state.echartImgSrc =='' ?

  ref={this.refChart}

id={this.state.id}

canvas-id="mychart-area"

  force-use-old-canvas="true"

  ec={this.state.ec}

/

:

CoverImage src={this.state.echartImgSrc}/CoverImage

微信小程序开发successthen的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于微信小程序开发工具、微信小程序开发successthen的信息别忘了在本站进行查找喔。

扫码二维码