国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

小程序的基本使用知識點(diǎn)(非常全面,推薦?。?/h1>
發(fā)布時(shí)間:2021-08-17 12:16 來(lái)源: 閱讀:0 作者:像大前端挺進(jìn) 欄目: JavaScript 歡迎投稿:712375056

目錄

注冊App時(shí)做什么呢?

  1. 判斷小程序的進(jìn)入場(chǎng)景
  2. 監聽(tīng)生命周期函數,在生命周期中執行對應的業(yè)務(wù)邏輯,比如在某個(gè)生命周期函數中獲取微信用戶(hù)的信息。
  3. 因為App()實(shí)例是全局共享的(單例對象),所以我們可以將一些共享數據放在這里。

注冊page頁(yè)面時(shí),我們一般需要做什么呢?

  1. 在生命周期中發(fā)送網(wǎng)絡(luò )請求,從服務(wù)器獲取數據;
  2. 初始化一些數據,以方便被wxml引用展示
  3. 監聽(tīng)wxml中的一些事件,綁定對應的事件函數
  4. 其他一些監聽(tīng),(比如頁(yè)面滾動(dòng),上拉刷新,下拉加載更多等)

關(guān)于wxml的template和include

在wxml中是不能直接調用Page/Component中定義函數的

在wxml文件中定義template標簽,template標簽在沒(méi)有調用的情況下是不會(huì )進(jìn)行任何渲染

name屬性和is屬性對應;可以使用{{}}語(yǔ)法

<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的導入有兩種方式:

import導入:

1:主要是導入template

2:特點(diǎn)是 不能進(jìn)行遞歸導入

include引入:

1:將公共的組件抽取到一個(gè)文件中

特點(diǎn):不能導入template/wxs/可以進(jìn)行遞歸導入

wxs模塊

wxs模塊是小程序的一套腳本語(yǔ)言結合wxml,可以構建出頁(yè)面的結構

wxs與javascript是不同的語(yǔ)言,有自己的語(yǔ)法,并不和JavaScript一致。(不過(guò)基本一致)

在wxml中是不能直接調用Page/Component中定義函數的

但是某些情況下,我們希望使用函數來(lái)處理wxml中的數據(類(lèi)似Vue在的過(guò)濾器),這個(gè)時(shí)候就使用wxs了

wxl使用的限制和特點(diǎn):

WXS的運行環(huán)境和其他的JavaScript代碼是隔離的,wxs中不能調用其他Javascript文件中定義的函數,也不能調用小程序中的API

wxs不能作為數組的函數回調。

由于運行環(huán)境的差異,在ios設備上的小程序內的wxs會(huì )比JavaScript快2~20.倍,在android設備上二者運行效率無(wú)差異。

小程序組件化相關(guān)

小程序組件和調用該組件的頁(yè)面樣式互不影響。

調用組件需要在頁(yè)面的json文件中加入組件名稱(chēng)和路徑

{
  "usingComponents": {
    "my-cut":"/pages/compent/my-cut"  }
}

在有需要是頁(yè)面和組件間的樣式相互影響的時(shí)候,可以利用options的 styleIsolation屬性

在組件的js文件Component({})下面

  // 在組件里面設置options的 styleIsolation: "apply-shared"會(huì )是頁(yè)面設置的樣式屬性影響組件的樣式
  // 在組件里面設置options的 styleIsolation: "shared"會(huì )是頁(yè)面設置的樣式屬性影響組件的樣式
  Component({
	options:{
    styleIsolation: "apply-shared"
  },
})

組件間的動(dòng)態(tài)轉值使用properties屬性

在組件中可以利用externalClasses這個(gè)屬性來(lái)動(dòng)態(tài)設置設置css的樣式

Component({
properties: {
    // title: String
    title:{
      type: String,
      value:"我是默認值",
      //監聽(tīng)新值和舊值
      observer: function(newVal,oldVal){
        console.log(newVal,oldVal)
      }
    },
    
  },
    // 在組件中可以利用這個(gè)屬性來(lái)動(dòng)態(tài)設置設置css的樣式
  externalClasses:["tltleclass"]

})

在頁(yè)面中調用屬性

<my-cut title="哈哈" tltleclass="red"></my-cut>
<my-cut title="嘿嘿" tltleclass="green" />
<my-cut/>

頁(yè)面css文件

.red{
  color: red;
}
.green{
  color: green;
}

組件到頁(yè)面間的函數調用

在頁(yè)面使用組件的時(shí)候,我們有時(shí)候會(huì )需要修改組件的數據,這就需要我們在頁(yè)面的js文件調用到組件的data。

在小程序中有個(gè)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: ["政治", "數學(xué)", "英語(yǔ)"]
      })
    }

  }

頁(yè)面wxml

<switch titled="{{list}}" bind:targetCount="targetCount" class="sw-class"></switch>

頁(yè)面js

  targetCount(){
  //獲取組件對象
    const content = this.selectComponent('.sw-class') 
    console.log(content)
    // content.setData({
    //   titled:["政治", "數學(xué)", "英語(yǔ)"]
    // })
    // 開(kāi)放規范一般不推薦這種直接在函數的修改方法,建議在組件內使用methods內部以方法的形式修改,在頁(yè)面調用即可
    content.amend()
  },

小程序組件插槽的使用

單插槽

組件wxml

<view>
  我是一個(gè)標題
</view>
<slot></slot>
<view>
  我是一個(gè)內容
</view>

頁(yè)面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>

多個(gè)插槽的使用

需要給每一個(gè)插槽取一個(gè)名字使用name屬性

必須在component對象下面的options對象下面的multipleSolts屬性設置為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
  }
  )}

頁(yè)面的使用

<!-- 多插槽的使用 -->
<!-- 注意事項:
需要給每一個(gè)插槽取一個(gè)名字
必須在component對象下面的options對象下面的multipleSolts屬性設置為true -->
<my-mslot>
<button slot="slot3">415461</button>
<slider slot="slot1" value="20"></slider>
<text slot="slot2">呵呵呵呵呵</text>
</my-mslot>

小程序組件的Component構造器有那些作用

1 properties 讓使用者可以給組件傳入數據

properties:{ 
	title: string,
	content:{
	type: array,
	value:[1,2.3],
	observer:function(newVal,oldVal){
        console.log(newVal,oldVal)
      }
	}
}

2.data 定義一些組件的初始化數據

data:{
	counter:0
}	

3 methods 用于定義組件內部的函數

methods:{
foo(){
	
}
}

4 options 定義組件的配置選項

options:{
//在需要使用多插槽時(shí)設置為true
	multipleSlots:true,
	//設置樣式的隔離方式
	styleIsolation: "apply-shared", //頁(yè)面設置的樣式屬性影響組件的樣式
   //styleIsolation: "shared"  頁(yè)面設置的樣式屬性影響組件的樣式
}

5 externalClasses 定義外部傳入額外的樣式

externalClasses:["on","tre"]

6 observers可以監聽(tīng)properties/data的改變

observers:{
	counter: function(newVal){
		console.log(newVal)
	}
}

7 監聽(tīng)所在頁(yè)面的生命周期

在組件js文件

  pageLifetimes:{
    show(){
      console.log("監聽(tīng)組件所在頁(yè)面顯示出來(lái)的")
    },
    hide(){
      console.log("監聽(tīng)組件所在頁(yè)面隱藏起來(lái)的時(shí)候")
    },
    resize(){
      console.log("監聽(tīng)頁(yè)面尺寸的改變")
    }
  }

監聽(tīng)組件內生命周期

lifetimes:{
    created(){
      console.log("組件被創(chuàng  )建")
    },
    attached(){
      console.log("組件被添加到頁(yè)面中")
    },
    ready(){
      console.log("組件被渲染出來(lái)")
    },
    moved(){
      console.log("組件被移動(dòng)到節點(diǎn)樹(shù)的另一個(gè)位置")
    },
    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)鍵的幾個(gè)屬性

  1. url: 必傳
  2. data: 請求參數
  3. method:請求的方式
  4. success:成功時(shí)的回調
  5. fail:失敗時(shí)的回調

一般情況下為了降低網(wǎng)絡(luò )請求和wx.request的耦合度,我們會(huì )Promise的方法獲取回調結果

使用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)
        }
      })
    })
}

頁(yè)面調用

 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)
    })
  },

小程序中的彈窗展示

頁(yè)面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>

頁(yè)面js文件

Page({
	showToast(){
    wx.showToast({
      title: '我是showToast',
    })
  },
  showModal(){
    wx.showModal({
      title:'是否刪除',
      cancelColor: 'cancelColor',
      success:function (params) {
          console.log(params)
          if (params.cancel) {
              console.log("點(diǎn)擊了取消刪除")
          } 
      } 
    })
  },
  showLoading(){
    wx.showLoading({
      title: '等待中',
      mask: true //給一個(gè)遮罩,防止其他操作
    })
    setTimeout(()=>{
      wx.hideLoading({
        success: (res) => {},
      })
    },1500)
  },
  showAction(){
    wx.showActionSheet({
      itemList: ['拍照','文件'],
    })
  }
})

小程序頁(yè)面分享

小程序的分享方式有兩種,一種是點(diǎn)擊按鈕分享,另一種是點(diǎn)擊右上角的菜單選項選擇分享。

我們分享小程序的時(shí)候需要通過(guò)onShareAppMessage來(lái)展示分享信息

監聽(tīng)用戶(hù)點(diǎn)擊頁(yè)面內轉發(fā)按鈕(button 組件 open-type=“share”)或右上角菜單“轉發(fā)”按鈕的行為,并自定義轉發(fā)內容。

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

關(guān)于小程序的跳轉

navigator標簽

關(guān)于navigator跳轉使用url.

<navigator url="/pages/home/home">跳轉home</navigator>

跳轉屬性open-type有如下取值

redirect:關(guān)閉當前頁(yè)面,跳轉到應用內的某個(gè)頁(yè)面。但是不允許跳轉到tabBer頁(yè)面,并且不能返回

switchTab:跳轉到tabBer頁(yè)面,并關(guān)閉其他非tabber頁(yè)面(需要在tabBer中定義)

reLaunch:關(guān)閉所有頁(yè)面,打開(kāi)某個(gè)頁(yè)面(直接展示某個(gè)頁(yè)面,斌切可以跳轉到tabBer頁(yè)面)

<navigator url="/pages/home/home">跳轉home</navigator>
<navigator url="/pages/home/home" open-type="redirect">跳轉home(redirect)</navigator>
<navigator url="/pages/home/home" open-type="reLaunch">跳轉home(reLaunch)</navigator>
<navigator url="/pages/home/home" open-type="switchTab">跳轉home(switchTab)</navigator>

總結

到此這篇關(guān)于小程序的基本使用的文章就介紹到這了,更多相關(guān)小程序基本使用內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

成人网站免费大全日韩国产| 国产AV无码专区亚洲AV毛片搜| 日本欧美大码AⅤ在线播放| 天堂中文最新版在线官网在线| 久久久久人妻一区精品果冻| 色欲天天天综合网|