小程序的基本使用知識點(非常全面,推薦?。?/h1>
更新時間:2021年06月23日 11:33:47 作者:像大前端挺進
開發(fā)一個小程序在如今來講是較為簡單的,但其實還是有很多的知識點需要大家記住,這篇文章主要給大家介紹了關(guān)于微信小程序基本使用的相關(guān)資料,需要的朋友可以參考下
注冊App時做什么呢?
- 判斷小程序的進入場景
- 監(jiān)聽生命周期函數(shù),在生命周期中執(zhí)行對應(yīng)的業(yè)務(wù)邏輯,比如在某個生命周期函數(shù)中獲取微信用戶的信息。
- 因為App()實例是全局共享的(單例對象),所以我們可以將一些共享數(shù)據(jù)放在這里。
注冊page頁面時,我們一般需要做什么呢?
- 在生命周期中發(fā)送網(wǎng)絡(luò)請求,從服務(wù)器獲取數(shù)據(jù);
- 初始化一些數(shù)據(jù),以方便被wxml引用展示
- 監(jiān)聽wxml中的一些事件,綁定對應(yīng)的事件函數(shù)
- 其他一些監(jiān)聽,(比如頁面滾動,上拉刷新,下拉加載更多等)
關(guān)于wxml的template和include
在wxml中是不能直接調(diào)用Page/Component中定義函數(shù)的
在wxml文件中定義template標簽,template標簽在沒有調(diào)用的情況下是不會進行任何渲染
name屬性和is屬性對應(yīng);可以使用{{}}語法
<template name="compoent">
<view>哈哈哈</view>
<text>嘿嘿嘿</text>
<text>{{name}}</text>
<text>{{age}}</text>
</template>
<template is="compoent"/>
<template is="compoent"/>
<template is="compoent"/>
<template is="compoent" data= "{{name: '小屏', age:'123'}}" />
關(guān)于wxml的導(dǎo)入有兩種方式:
import導(dǎo)入:
1:主要是導(dǎo)入template
2:特點是 不能進行遞歸導(dǎo)入
include引入:
1:將公共的組件抽取到一個文件中
特點:不能導(dǎo)入template/wxs/可以進行遞歸導(dǎo)入
wxs模塊
wxs模塊是小程序的一套腳本語言結(jié)合wxml,可以構(gòu)建出頁面的結(jié)構(gòu)
wxs與javascript是不同的語言,有自己的語法,并不和JavaScript一致。(不過基本一致)
在wxml中是不能直接調(diào)用Page/Component中定義函數(shù)的
但是某些情況下,我們希望使用函數(shù)來處理wxml中的數(shù)據(jù)(類似Vue在的過濾器),這個時候就使用wxs了
wxl使用的限制和特點:
WXS的運行環(huán)境和其他的JavaScript代碼是隔離的,wxs中不能調(diào)用其他Javascript文件中定義的函數(shù),也不能調(diào)用小程序中的API
wxs不能作為數(shù)組的函數(shù)回調(diào)。
由于運行環(huán)境的差異,在ios設(shè)備上的小程序內(nèi)的wxs會比JavaScript快2~20.倍,在android設(shè)備上二者運行效率無差異。
小程序組件化相關(guān)
小程序組件和調(diào)用該組件的頁面樣式互不影響。
調(diào)用組件需要在頁面的json文件中加入組件名稱和路徑
{
"usingComponents": {
"my-cut":"/pages/compent/my-cut" }
}
在有需要是頁面和組件間的樣式相互影響的時候,可以利用options的 styleIsolation屬性
在組件的js文件Component({})下面
// 在組件里面設(shè)置options的 styleIsolation: "apply-shared"會是頁面設(shè)置的樣式屬性影響組件的樣式
// 在組件里面設(shè)置options的 styleIsolation: "shared"會是頁面設(shè)置的樣式屬性影響組件的樣式
Component({
options:{
styleIsolation: "apply-shared"
},
})
組件間的動態(tài)轉(zhuǎn)值使用properties屬性
在組件中可以利用externalClasses這個屬性來動態(tài)設(shè)置設(shè)置css的樣式
Component({
properties: {
// title: String
title:{
type: String,
value:"我是默認值",
//監(jiān)聽新值和舊值
observer: function(newVal,oldVal){
console.log(newVal,oldVal)
}
},
},
// 在組件中可以利用這個屬性來動態(tài)設(shè)置設(shè)置css的樣式
externalClasses:["tltleclass"]
})
在頁面中調(diào)用屬性
<my-cut title="哈哈" tltleclass="red"></my-cut>
<my-cut title="嘿嘿" tltleclass="green" />
<my-cut/>
頁面css文件
.red{
color: red;
}
.green{
color: green;
}
組件到頁面間的函數(shù)調(diào)用
在頁面使用組件的時候,我們有時候會需要修改組件的數(shù)據(jù),這就需要我們在頁面的js文件調(diào)用到組件的data。
在小程序中有個selectComponent('.class/#xxx')可以拿到組件對象;
組件wxml
<view class="contention">
<block wx:for="{{titled}}" wx:key="index">
<view class="tit"> <text class="intext {{index == increat? 'active' : ''}}" data-count="{{index}}" bindtap="targetTap">{{item}}</text> </view>
</block>
</view>
組件js
methods: {
targetTap(e){
const index = e.currentTarget.dataset.count
this.setData({
increat:index
})
this.triggerEvent("targetCount" , {})
},
amend(){
this.setData({
titled: ["政治", "數(shù)學(xué)", "英語"]
})
}
}
頁面wxml
<switch titled="{{list}}" bind:targetCount="targetCount" class="sw-class"></switch>
頁面js
targetCount(){
//獲取組件對象
const content = this.selectComponent('.sw-class')
console.log(content)
// content.setData({
// titled:["政治", "數(shù)學(xué)", "英語"]
// })
// 開放規(guī)范一般不推薦這種直接在函數(shù)的修改方法,建議在組件內(nèi)使用methods內(nèi)部以方法的形式修改,在頁面調(diào)用即可
content.amend()
},
小程序組件插槽的使用
單插槽
組件wxml
<view>
我是一個標題
</view>
<slot></slot>
<view>
我是一個內(nèi)容
</view>
頁面wxml使用
<my-slot>
<button size="mini"> 按鈕</button>
</my-slot>
<my-slot>
<image src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.woyaogexing.com%2F2021%2F04%2F26%2F1b402c98095d452486508b8250b21f3f%21360x640.jpeg&refer=http%3A%2F%2Fimg2.woyaogexing.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625711145&t=d9d310d5e9c878a9d4b7bc7057f2b754"/>
</my-slot>
<my-slot>
<slider value="20"></slider>
</my-slot>
多個插槽的使用
需要給每一個插槽取一個名字使用name屬性
必須在component對象下面的options對象下面的multipleSolts屬性設(shè)置為true
組件wxml
<view>我是多插槽的標題</view>
<view> <slot name="slot1" ></slot> </view>
<view> <slot name="slot2" ></slot> </view>
<view> <slot name="slot3" ></slot> </view>
<view>我是多插槽的尾部</view>
組件js文件
Component({
/**
* 組件的屬性列表
*/
options:{
multipleSlots:true
}
)}
頁面的使用
<!-- 多插槽的使用 -->
<!-- 注意事項:
需要給每一個插槽取一個名字
必須在component對象下面的options對象下面的multipleSolts屬性設(shè)置為true -->
<my-mslot>
<button slot="slot3">415461</button>
<slider slot="slot1" value="20"></slider>
<text slot="slot2">呵呵呵呵呵</text>
</my-mslot>
小程序組件的Component構(gòu)造器有那些作用
1 properties 讓使用者可以給組件傳入數(shù)據(jù)
properties:{
title: string,
content:{
type: array,
value:[1,2.3],
observer:function(newVal,oldVal){
console.log(newVal,oldVal)
}
}
}
2.data 定義一些組件的初始化數(shù)據(jù)
data:{
counter:0
}
3 methods 用于定義組件內(nèi)部的函數(shù)
methods:{
foo(){
}
}
4 options 定義組件的配置選項
options:{
//在需要使用多插槽時設(shè)置為true
multipleSlots:true,
//設(shè)置樣式的隔離方式
styleIsolation: "apply-shared", //頁面設(shè)置的樣式屬性影響組件的樣式
//styleIsolation: "shared" 頁面設(shè)置的樣式屬性影響組件的樣式
}
5 externalClasses 定義外部傳入額外的樣式
externalClasses:["on","tre"]
6 observers可以監(jiān)聽properties/data的改變
observers:{
counter: function(newVal){
console.log(newVal)
}
}
7 監(jiān)聽所在頁面的生命周期
在組件js文件
pageLifetimes:{
show(){
console.log("監(jiān)聽組件所在頁面顯示出來的")
},
hide(){
console.log("監(jiān)聽組件所在頁面隱藏起來的時候")
},
resize(){
console.log("監(jiān)聽頁面尺寸的改變")
}
}
監(jiān)聽組件內(nèi)生命周期
lifetimes:{
created(){
console.log("組件被創(chuàng)建")
},
attached(){
console.log("組件被添加到頁面中")
},
ready(){
console.log("組件被渲染出來")
},
moved(){
console.log("組件被移動到節(jié)點樹的另一個位置")
},
detached(){
console.log("組件")
}
}
小程序網(wǎng)絡(luò)請求
onReady: function () {
wx.request({
url: 'http://httpbin.org/post',
method:'post',
data:{
name:"wangshuai",
age:19
},
success:function(res){
console.log(res)
}
})
},

比較關(guān)鍵的幾個屬性
- url: 必傳
- data: 請求參數(shù)
- method:請求的方式
- success:成功時的回調(diào)
- fail:失敗時的回調(diào)
一般情況下為了降低網(wǎng)絡(luò)請求和wx.request的耦合度,我們會Promise的方法獲取回調(diào)結(jié)果
使用promise封裝
export default function request(option) {
return new Promise( (resolve, reject) => {
wx.request({
url: option.url,
method: option.method || 'get',
data: option.data || {},
success: function (res) {
resolve(res)
},
fail: function (res) {
reject(res)
}
})
})
}
頁面調(diào)用
onReady: function () {
//小程序原生網(wǎng)絡(luò)請求
// wx.request({
// url: 'http://httpbin.org/post',
// method:'post',
// data:{
// name:"wangshuai",
// age:19
// },
// success:function(res){
// console.log(res)
// }
// })
request({url: 'http://httpbin.org/post',method:"post"}).then(res=>{
console.log(res)
}).catch(err =>{
console.log(err)
})
},
小程序中的彈窗展示
頁面wxml
<button size="mini" bindtap="showToast">showToast</button>
<button size="mini" bindtap="showModal">showModal</button>
<button size="mini" bindtap="showLoading">showLoading</button>
<button size="mini" bindtap="showAction">showAction</button>
<button size="mini" open-type="share">分享</button>
頁面js文件
Page({
showToast(){
wx.showToast({
title: '我是showToast',
})
},
showModal(){
wx.showModal({
title:'是否刪除',
cancelColor: 'cancelColor',
success:function (params) {
console.log(params)
if (params.cancel) {
console.log("點擊了取消刪除")
}
}
})
},
showLoading(){
wx.showLoading({
title: '等待中',
mask: true //給一個遮罩,防止其他操作
})
setTimeout(()=>{
wx.hideLoading({
success: (res) => {},
})
},1500)
},
showAction(){
wx.showActionSheet({
itemList: ['拍照','文件'],
})
}
})
小程序頁面分享
小程序的分享方式有兩種,一種是點擊按鈕分享,另一種是點擊右上角的菜單選項選擇分享。
我們分享小程序的時候需要通過onShareAppMessage來展示分享信息
監(jiān)聽用戶點擊頁面內(nèi)轉(zhuǎn)發(fā)按鈕(button 組件 open-type=“share”)或右上角菜單“轉(zhuǎn)發(fā)”按鈕的行為,并自定義轉(zhuǎn)發(fā)內(nèi)容。

注意:只有定義了此事件處理函數(shù),右上角菜單才會顯示“轉(zhuǎn)發(fā)”按鈕
此事件處理函數(shù)需要 return 一個 Object,用于自定義轉(zhuǎn)發(fā)內(nèi)容,返回內(nèi)容如下:

關(guān)于小程序的跳轉(zhuǎn)
navigator標簽
關(guān)于navigator跳轉(zhuǎn)使用url.
<navigator url="/pages/home/home">跳轉(zhuǎn)home</navigator>
跳轉(zhuǎn)屬性open-type有如下取值
redirect:關(guān)閉當前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個頁面。但是不允許跳轉(zhuǎn)到tabBer頁面,并且不能返回
switchTab:跳轉(zhuǎn)到tabBer頁面,并關(guān)閉其他非tabber頁面(需要在tabBer中定義)
reLaunch:關(guān)閉所有頁面,打開某個頁面(直接展示某個頁面,斌切可以跳轉(zhuǎn)到tabBer頁面)
<navigator url="/pages/home/home">跳轉(zhuǎn)home</navigator>
<navigator url="/pages/home/home" open-type="redirect">跳轉(zhuǎn)home(redirect)</navigator>
<navigator url="/pages/home/home" open-type="reLaunch">跳轉(zhuǎn)home(reLaunch)</navigator>
<navigator url="/pages/home/home" open-type="switchTab">跳轉(zhuǎn)home(switchTab)</navigator>
總結(jié)
到此這篇關(guān)于小程序的基本使用的文章就介紹到這了,更多相關(guān)小程序基本使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
-
JS統(tǒng)計Flash被網(wǎng)友點擊過的代碼
JS統(tǒng)計Flash被網(wǎng)友點擊過的代碼... 2007-05-05
-
PHP實現(xiàn)基于Redis的MessageQueue隊列封裝操作示例
這篇文章主要介紹了PHP實現(xiàn)基于Redis的MessageQueue隊列封裝操作,結(jié)合實例形式分析了Redis的PHP消息隊列封裝與使用相關(guān)操作技巧,需要的朋友可以參考下 2019-02-02
-
JavaScript計算兩個日期時間段內(nèi)日期的方法
這篇文章主要介紹了JavaScript計算兩個日期時間段內(nèi)日期的方法,涉及javascript操作時間的技巧,需要的朋友可以參考下 2015-03-03
-
利用JavaScript實現(xiàn)的10種排序算法總結(jié)
這篇文章主要介紹了利用JavaScript實現(xiàn)的十種排序算法,主要介紹了冒泡,選擇,插入,希爾,歸并,快速,堆排,計數(shù),桶排和基數(shù),有感興趣的小伙伴可以參考閱讀本文 2023-05-05
最新評論
注冊App時做什么呢?
- 判斷小程序的進入場景
- 監(jiān)聽生命周期函數(shù),在生命周期中執(zhí)行對應(yīng)的業(yè)務(wù)邏輯,比如在某個生命周期函數(shù)中獲取微信用戶的信息。
- 因為App()實例是全局共享的(單例對象),所以我們可以將一些共享數(shù)據(jù)放在這里。
注冊page頁面時,我們一般需要做什么呢?
- 在生命周期中發(fā)送網(wǎng)絡(luò)請求,從服務(wù)器獲取數(shù)據(jù);
- 初始化一些數(shù)據(jù),以方便被wxml引用展示
- 監(jiān)聽wxml中的一些事件,綁定對應(yīng)的事件函數(shù)
- 其他一些監(jiān)聽,(比如頁面滾動,上拉刷新,下拉加載更多等)
關(guān)于wxml的template和include
在wxml中是不能直接調(diào)用Page/Component中定義函數(shù)的
在wxml文件中定義template標簽,template標簽在沒有調(diào)用的情況下是不會進行任何渲染
name屬性和is屬性對應(yīng);可以使用{{}}語法
<template name="compoent"> <view>哈哈哈</view> <text>嘿嘿嘿</text> <text>{{name}}</text> <text>{{age}}</text> </template> <template is="compoent"/> <template is="compoent"/> <template is="compoent"/> <template is="compoent" data= "{{name: '小屏', age:'123'}}" />
關(guān)于wxml的導(dǎo)入有兩種方式:
import導(dǎo)入:
1:主要是導(dǎo)入template
2:特點是 不能進行遞歸導(dǎo)入
include引入:
1:將公共的組件抽取到一個文件中
特點:不能導(dǎo)入template/wxs/可以進行遞歸導(dǎo)入
wxs模塊
wxs模塊是小程序的一套腳本語言結(jié)合wxml,可以構(gòu)建出頁面的結(jié)構(gòu)
wxs與javascript是不同的語言,有自己的語法,并不和JavaScript一致。(不過基本一致)
在wxml中是不能直接調(diào)用Page/Component中定義函數(shù)的
但是某些情況下,我們希望使用函數(shù)來處理wxml中的數(shù)據(jù)(類似Vue在的過濾器),這個時候就使用wxs了
wxl使用的限制和特點:
WXS的運行環(huán)境和其他的JavaScript代碼是隔離的,wxs中不能調(diào)用其他Javascript文件中定義的函數(shù),也不能調(diào)用小程序中的API
wxs不能作為數(shù)組的函數(shù)回調(diào)。
由于運行環(huán)境的差異,在ios設(shè)備上的小程序內(nèi)的wxs會比JavaScript快2~20.倍,在android設(shè)備上二者運行效率無差異。
小程序組件化相關(guān)
小程序組件和調(diào)用該組件的頁面樣式互不影響。
調(diào)用組件需要在頁面的json文件中加入組件名稱和路徑
{ "usingComponents": { "my-cut":"/pages/compent/my-cut" } }
在有需要是頁面和組件間的樣式相互影響的時候,可以利用options的 styleIsolation屬性
在組件的js文件Component({})下面
// 在組件里面設(shè)置options的 styleIsolation: "apply-shared"會是頁面設(shè)置的樣式屬性影響組件的樣式 // 在組件里面設(shè)置options的 styleIsolation: "shared"會是頁面設(shè)置的樣式屬性影響組件的樣式 Component({ options:{ styleIsolation: "apply-shared" }, })
組件間的動態(tài)轉(zhuǎn)值使用properties屬性
在組件中可以利用externalClasses這個屬性來動態(tài)設(shè)置設(shè)置css的樣式
Component({ properties: { // title: String title:{ type: String, value:"我是默認值", //監(jiān)聽新值和舊值 observer: function(newVal,oldVal){ console.log(newVal,oldVal) } }, }, // 在組件中可以利用這個屬性來動態(tài)設(shè)置設(shè)置css的樣式 externalClasses:["tltleclass"] })
在頁面中調(diào)用屬性
<my-cut title="哈哈" tltleclass="red"></my-cut> <my-cut title="嘿嘿" tltleclass="green" /> <my-cut/>
頁面css文件
.red{ color: red; } .green{ color: green; }
組件到頁面間的函數(shù)調(diào)用
在頁面使用組件的時候,我們有時候會需要修改組件的數(shù)據(jù),這就需要我們在頁面的js文件調(diào)用到組件的data。
在小程序中有個selectComponent('.class/#xxx')可以拿到組件對象;
組件wxml
<view class="contention"> <block wx:for="{{titled}}" wx:key="index"> <view class="tit"> <text class="intext {{index == increat? 'active' : ''}}" data-count="{{index}}" bindtap="targetTap">{{item}}</text> </view> </block> </view>
組件js
methods: { targetTap(e){ const index = e.currentTarget.dataset.count this.setData({ increat:index }) this.triggerEvent("targetCount" , {}) }, amend(){ this.setData({ titled: ["政治", "數(shù)學(xué)", "英語"] }) } }
頁面wxml
<switch titled="{{list}}" bind:targetCount="targetCount" class="sw-class"></switch>
頁面js
targetCount(){ //獲取組件對象 const content = this.selectComponent('.sw-class') console.log(content) // content.setData({ // titled:["政治", "數(shù)學(xué)", "英語"] // }) // 開放規(guī)范一般不推薦這種直接在函數(shù)的修改方法,建議在組件內(nèi)使用methods內(nèi)部以方法的形式修改,在頁面調(diào)用即可 content.amend() },
小程序組件插槽的使用
單插槽
組件wxml
<view> 我是一個標題 </view> <slot></slot> <view> 我是一個內(nèi)容 </view>
頁面wxml使用
<my-slot> <button size="mini"> 按鈕</button> </my-slot> <my-slot> <image src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.woyaogexing.com%2F2021%2F04%2F26%2F1b402c98095d452486508b8250b21f3f%21360x640.jpeg&refer=http%3A%2F%2Fimg2.woyaogexing.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625711145&t=d9d310d5e9c878a9d4b7bc7057f2b754"/> </my-slot> <my-slot> <slider value="20"></slider> </my-slot>
多個插槽的使用
需要給每一個插槽取一個名字使用name屬性
必須在component對象下面的options對象下面的multipleSolts屬性設(shè)置為true
組件wxml
<view>我是多插槽的標題</view> <view> <slot name="slot1" ></slot> </view> <view> <slot name="slot2" ></slot> </view> <view> <slot name="slot3" ></slot> </view> <view>我是多插槽的尾部</view>
組件js文件
Component({ /** * 組件的屬性列表 */ options:{ multipleSlots:true } )}
頁面的使用
<!-- 多插槽的使用 --> <!-- 注意事項: 需要給每一個插槽取一個名字 必須在component對象下面的options對象下面的multipleSolts屬性設(shè)置為true --> <my-mslot> <button slot="slot3">415461</button> <slider slot="slot1" value="20"></slider> <text slot="slot2">呵呵呵呵呵</text> </my-mslot>
小程序組件的Component構(gòu)造器有那些作用
1 properties 讓使用者可以給組件傳入數(shù)據(jù)
properties:{ title: string, content:{ type: array, value:[1,2.3], observer:function(newVal,oldVal){ console.log(newVal,oldVal) } } }
2.data 定義一些組件的初始化數(shù)據(jù)
data:{ counter:0 }
3 methods 用于定義組件內(nèi)部的函數(shù)
methods:{ foo(){ } }
4 options 定義組件的配置選項
options:{ //在需要使用多插槽時設(shè)置為true multipleSlots:true, //設(shè)置樣式的隔離方式 styleIsolation: "apply-shared", //頁面設(shè)置的樣式屬性影響組件的樣式 //styleIsolation: "shared" 頁面設(shè)置的樣式屬性影響組件的樣式 }
5 externalClasses 定義外部傳入額外的樣式
externalClasses:["on","tre"]
6 observers可以監(jiān)聽properties/data的改變
observers:{ counter: function(newVal){ console.log(newVal) } }
7 監(jiān)聽所在頁面的生命周期
在組件js文件
pageLifetimes:{ show(){ console.log("監(jiān)聽組件所在頁面顯示出來的") }, hide(){ console.log("監(jiān)聽組件所在頁面隱藏起來的時候") }, resize(){ console.log("監(jiān)聽頁面尺寸的改變") } }
監(jiān)聽組件內(nèi)生命周期
lifetimes:{ created(){ console.log("組件被創(chuàng)建") }, attached(){ console.log("組件被添加到頁面中") }, ready(){ console.log("組件被渲染出來") }, moved(){ console.log("組件被移動到節(jié)點樹的另一個位置") }, detached(){ console.log("組件") } }
小程序網(wǎng)絡(luò)請求
onReady: function () { wx.request({ url: 'http://httpbin.org/post', method:'post', data:{ name:"wangshuai", age:19 }, success:function(res){ console.log(res) } }) },
比較關(guān)鍵的幾個屬性
- url: 必傳
- data: 請求參數(shù)
- method:請求的方式
- success:成功時的回調(diào)
- fail:失敗時的回調(diào)
一般情況下為了降低網(wǎng)絡(luò)請求和wx.request的耦合度,我們會Promise的方法獲取回調(diào)結(jié)果
使用promise封裝
export default function request(option) { return new Promise( (resolve, reject) => { wx.request({ url: option.url, method: option.method || 'get', data: option.data || {}, success: function (res) { resolve(res) }, fail: function (res) { reject(res) } }) }) }
頁面調(diào)用
onReady: function () { //小程序原生網(wǎng)絡(luò)請求 // wx.request({ // url: 'http://httpbin.org/post', // method:'post', // data:{ // name:"wangshuai", // age:19 // }, // success:function(res){ // console.log(res) // } // }) request({url: 'http://httpbin.org/post',method:"post"}).then(res=>{ console.log(res) }).catch(err =>{ console.log(err) }) },
小程序中的彈窗展示
頁面wxml
<button size="mini" bindtap="showToast">showToast</button> <button size="mini" bindtap="showModal">showModal</button> <button size="mini" bindtap="showLoading">showLoading</button> <button size="mini" bindtap="showAction">showAction</button> <button size="mini" open-type="share">分享</button>
頁面js文件
Page({ showToast(){ wx.showToast({ title: '我是showToast', }) }, showModal(){ wx.showModal({ title:'是否刪除', cancelColor: 'cancelColor', success:function (params) { console.log(params) if (params.cancel) { console.log("點擊了取消刪除") } } }) }, showLoading(){ wx.showLoading({ title: '等待中', mask: true //給一個遮罩,防止其他操作 }) setTimeout(()=>{ wx.hideLoading({ success: (res) => {}, }) },1500) }, showAction(){ wx.showActionSheet({ itemList: ['拍照','文件'], }) } })
小程序頁面分享
小程序的分享方式有兩種,一種是點擊按鈕分享,另一種是點擊右上角的菜單選項選擇分享。
我們分享小程序的時候需要通過onShareAppMessage來展示分享信息
監(jiān)聽用戶點擊頁面內(nèi)轉(zhuǎn)發(fā)按鈕(button 組件 open-type=“share”)或右上角菜單“轉(zhuǎn)發(fā)”按鈕的行為,并自定義轉(zhuǎn)發(fā)內(nèi)容。
注意:只有定義了此事件處理函數(shù),右上角菜單才會顯示“轉(zhuǎn)發(fā)”按鈕
此事件處理函數(shù)需要 return 一個 Object,用于自定義轉(zhuǎn)發(fā)內(nèi)容,返回內(nèi)容如下:
關(guān)于小程序的跳轉(zhuǎn)
navigator標簽
關(guān)于navigator跳轉(zhuǎn)使用url.
<navigator url="/pages/home/home">跳轉(zhuǎn)home</navigator>
跳轉(zhuǎn)屬性open-type有如下取值
redirect:關(guān)閉當前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個頁面。但是不允許跳轉(zhuǎn)到tabBer頁面,并且不能返回
switchTab:跳轉(zhuǎn)到tabBer頁面,并關(guān)閉其他非tabber頁面(需要在tabBer中定義)
reLaunch:關(guān)閉所有頁面,打開某個頁面(直接展示某個頁面,斌切可以跳轉(zhuǎn)到tabBer頁面)
<navigator url="/pages/home/home">跳轉(zhuǎn)home</navigator> <navigator url="/pages/home/home" open-type="redirect">跳轉(zhuǎn)home(redirect)</navigator> <navigator url="/pages/home/home" open-type="reLaunch">跳轉(zhuǎn)home(reLaunch)</navigator> <navigator url="/pages/home/home" open-type="switchTab">跳轉(zhuǎn)home(switchTab)</navigator>
總結(jié)
到此這篇關(guān)于小程序的基本使用的文章就介紹到這了,更多相關(guān)小程序基本使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS統(tǒng)計Flash被網(wǎng)友點擊過的代碼
JS統(tǒng)計Flash被網(wǎng)友點擊過的代碼...2007-05-05PHP實現(xiàn)基于Redis的MessageQueue隊列封裝操作示例
這篇文章主要介紹了PHP實現(xiàn)基于Redis的MessageQueue隊列封裝操作,結(jié)合實例形式分析了Redis的PHP消息隊列封裝與使用相關(guān)操作技巧,需要的朋友可以參考下2019-02-02JavaScript計算兩個日期時間段內(nèi)日期的方法
這篇文章主要介紹了JavaScript計算兩個日期時間段內(nèi)日期的方法,涉及javascript操作時間的技巧,需要的朋友可以參考下2015-03-03利用JavaScript實現(xiàn)的10種排序算法總結(jié)
這篇文章主要介紹了利用JavaScript實現(xiàn)的十種排序算法,主要介紹了冒泡,選擇,插入,希爾,歸并,快速,堆排,計數(shù),桶排和基數(shù),有感興趣的小伙伴可以參考閱讀本文2023-05-05