小程序開發(fā) 事件、跳轉(zhuǎn)、傳參取參、http、數(shù)據(jù)綁定和渲染

js引入:

const util =require('../../utils/util.js')

獲取全局對象和方法:

const app = getApp()

事件:

1、key 以bind或catch開頭,然后跟上事件的類型,如bindtap、catchtouchstart。

跳轉(zhuǎn):

頁面和tabBar之間跳轉(zhuǎn):

wx.switchTab({

url:'../index/index'

});

頁面之間跳轉(zhuǎn)方法:

1、保留當前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個頁面,使用wx.navigateBack可以返回到原頁面。

wx.navigateTo({ ? 新頁面會有返回按鈕

url:'../logs/logs?key=value&key2=value2' //傳參

,

success: function(res){

// success

},

fail: function() {

// fail

},

complete: function() {

// complete

}

})

2、wx.redirectTo(OBJECT)

關(guān)閉當前頁面,跳轉(zhuǎn)到應(yīng)用內(nèi)的某個頁面。

wx.redirectTo({ ? 新頁面不會有返回按鈕

url:'../index/index',

,

success: function(res){

// success

},

fail: function() {

// fail

},

complete: function() {

// complete

}

})

3、x.navigateBack(OBJECT)

關(guān)閉當前頁面,返回上一頁面或多級頁面??赏ㄟ^ getCurrentPages()) 獲取當前的頁面棧,決定需要返回幾層。如果 delta 大于現(xiàn)有頁面數(shù),則返回到首頁。

varpages = getCurrentPages()varnum = pages.length

navigateBack:function(){wx.navigateBack({

delta: num

})

}

頁面跳轉(zhuǎn)也可直接用 url:

<navigator url="../login/login?title=1111" >跳轉(zhuǎn)到新頁面</navigator>

<navigator url="../login/login?title=111" redirect>在當前頁打開新頁面</navigator>


如果要傳 數(shù)組, 字典等復(fù)雜類型, 要先用 JSON.stringify() 轉(zhuǎn)成字符串然后傳遞,接收到之后要用JSON.parse()轉(zhuǎn)換。.

取參:

Page({

onLoad:function(options)?{

this.setData({ ? ? ? //用于給data中的變量賦值

title:?options.key

})

}

})

數(shù)據(jù)綁定:

數(shù)據(jù)綁定使用 Mustache 語法(雙大括號)將變量包起來。

內(nèi)容:<view>{{message}}</view>

控制屬性:<view wx:if="{{condition}}"></view>

關(guān)鍵字:<checkbox checked="{{false}}"></checkbox>

...

http:(微信公眾號和小程序上線的話都必須是https請求,小程序接口不允許ip和端口號)

wx.request({?

?url:'test.php',//僅為示例,并非真實的接口地址

data: { x:'', y:''},

?header: {'content-type':'application/json、默認值、或者、、'application/x-www-form-urlencoded'// },?

method:'GET/POST',

?success:function(res){console.log(res.data) }

})

APP.JSON(tabBar)

{

"pages":[

"pages/index/index",

"pages/tucao/tucao",

"pages/center/center"

],

"window":{

"backgroundTextStyle":"",

"navigationBarBackgroundColor":"red",

"navigationBarTitleText":"一個標題而已",

"navigationBarTextStyle":"white"

},

"tabBar":?{

"list":?[{

"pagePath":"pages/index/index",

"text":"首頁",

"iconPath":"/images/public/menu-cd.png",

"selectedIconPath":"/images/public/menu.png"

},{

"pagePath":"pages/tucao/tucao",

"text":"吐槽",

"iconPath":"/images/public/hot-cd.png",

"selectedIconPath":"/images/public/hot.png"

},{

"pagePath":"pages/center/center",

"text":"我的",

"iconPath":"/images/public/center-cd.png",

"selectedIconPath":"/images/public/center.png"

}],

"borderStyle":"white"

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容