uniapp坑點(diǎn)(支付寶小程序)

1. pageInstance不能掛載在data上 => 擴(kuò)展:Function對象不能掛載在data上

letpages=getCurrentPages();this.pageInstance=pages[pages.length-1];// 會報錯letpageInstance=pages[pages.length-1];// 不報錯


解決:用的時候重新調(diào)用一遍獲取问裕。

2. 在父組件里對引用的自定義組件及組件內(nèi)部設(shè)置樣式無效果

<template><viewclass="test"><tabbar-itemv-for="item in tabbars":key="item.name":class="{ 'no-border-button': fullPath === item.path }">...</tabbar-item></view><template><stylelang="scss">.test { // 有效逮壁,.tabbar-item,.tabbar-item-content是自定義組件內(nèi)部的class .tabbar-item {...} .tabbar-item-content {...}}</style><stylelang="scss"scoped>.no-border-button {...} // 無效</style>


解決:用全局樣式,不用scoped

3. 對自定義組件設(shè)置普通監(jiān)聽事件無效粮宛,需內(nèi)部觸發(fā)事件

<cellclass="cell"title="合規(guī)商戶"is-link @click="handleMarketClick(item, 0)"http:// 單方面設(shè)置無效>...</cell>

解決:

// cell.vue<viewclass="cell"@click="$emit('click')">// 需要內(nèi)部觸發(fā)...</view>

4. scroll-view的upper-threshold設(shè)為0的話不觸發(fā)scrolltoupper

<scroll-viewclass="scroll-view":scroll-y="true":upper-threshold="0":lower-threshold="300"@scroll="handleScroll"@scrolltoupper="handleScrolltoupper"@scrolltolower="handleScrolltolower">

解決:設(shè)置為不為0的小數(shù)字

5. mixins的components不能混入

// 會報錯找不到mixins里面定義的組件<testComponent><template><view>...<testComponent/>...</view></template><script>export default { mixins: [test], ...}</script>

// test.jsimporttestfrom'...'exportdefault{components:{test},...}

擴(kuò)展:與template有關(guān)系的用components作復(fù)用窥淆,與script有關(guān)系的用mixins復(fù)用 或者 抽取成為公共js使用

解決:components及其引入放在組件里做

// 可正常使用components和mixins里面的js<template> <view> ...

? ? <testComponent/>? ? ...

? </view></template><script>import test from '...'

export default {

? mixins: [test],

? components: { test },

? ...

}</script>

6.?this.$refs獲取不到設(shè)置了ref的元素


嘗試:(1) 內(nèi)置組件如官方所示,獲取了undefined巍杈。

(2) 對自定義組件使用$refs忧饭,在mounted和onReady時機(jī)下可以獲取到值。

<testref="textArea"> <textclass="title">{{title}}111</text></test>...importtestfrom'@/components/test/test.vue'exportdefault{components:{test},...

(3)子組件slot中自定義組件的$refs筷畦,直接this.$refs獲取不到词裤,需經(jīng)過子組件的一層$refs才能獲取到內(nèi)部的$refs,如下

<test> <titleBoxclass="title"ref="childTitle">{{title}}111</titleBox></test>...importtitleBoxfrom'@/components/cell.vue'importtestfrom'@/components/test/test.vue'exportdefault{components:{test,titleBox},mounted(){console.log('mounted: ',this.$refs.childTitle)// 輸出 mounted: undefined},onReady(){console.log('onReady: ',this.$refs.childTitle)// 輸出 onReady: undefined},...

此時是獲取不到this.$refs.childTitle的鳖宾,它是屬于子組件底下的$refs

解決

<testref="textArea"> <titleBoxclass="title"ref="childTitle">{{title}}111</titleBox></test>...importtitleBoxfrom'@/components/cell.vue'importtestfrom'@/components/test/test.vue'exportdefault{components:{test,titleBox},mounted(){console.log('mounted: ',this.$refs.textArea,this.$refs.textArea.$refs.childTitle)},onReady(){console.log('onReady: ',this.$refs.textArea,this.$refs.textArea.$refs.childTitle)},...


參考:https://www.lervor.com/archives/121/

7. 一些基礎(chǔ)組件如icon無監(jiān)聽點(diǎn)擊事件

解決:可在外層包一個view作事件監(jiān)聽

<viewclass="clear-icon"@click="onClear"><iconv-if="showClear"type="clear"size="16"></icon></view>

8. class綁定里吼砂,如果一個對象里有多個屬性值,渲染出來的class會帶英文逗號 ,

<view:class="[styleType==='text'?'segmented-control__item--text':'segmented-control__item--button',{'segmented-control__item--button--active':index===currentIndex&&styleType==='button','segmented-control__item--button--first':index===0&&styleType==='button','segmented-control__item--button--last':index===values.length-1&&styleType==='button',disabled:item.disabled}]">

class渲染的結(jié)果:

segmented-control__itemdata-v-5311d210 segmented-control__item--text,,,disabled

此時disabled就失效了鼎文。

解決:拆開來賦值渔肩。

styleType === 'text'? ? 'segmented-control__item--text'? : 'segmented-control__item--button',{? 'segmented-control__item--button--active':? ? index === currentIndex && styleType === 'button'},{? 'segmented-control__item--button--first':? ? index === 0 && styleType === 'button'},{? 'segmented-control__item--button--last':? ? index === values.length - 1 && styleType === 'button'},{ disabled: item.disabled }

class渲染的結(jié)果:

segmented-control__itemdata-v-5311d210 segmented-control__item--text? ? disabled

9. 不能在template里直接寫js方法(也不美觀),編輯器不會報錯拇惋,但模擬器會報錯編譯失敗周偎,build error => 擴(kuò)展:不可在template中寫js代碼

<van-field...@input="? isX(form.shopIdcard,()=>{form.shopIdcard=form.shopIdcard+'X';})"/>


解決:把這種方法放在methods里

<van-field...@input="handleInput"/>...methods:{handleInput(){isX(form.shopIdcard,()=>{form.shopIdcard=form.shopIdcard+'X';})}}

10. picker-view的注意點(diǎn):

(1)注意要加<picker-view-column>抹剩,否則會報錯。必須有一層元素包住內(nèi)容蓉坎。

(2)在初始為空內(nèi)容(如此處的areaList初始為[])時渲染則會出現(xiàn)不可滑動的情況澳眷。因?yàn)樵阡秩緯r會根據(jù)初始內(nèi)容確定每一項(xiàng)高度,后續(xù)無法動態(tài)確定高度蛉艾。必須加上v-if="showPicker"

<viewv-if="showPicker"class="picker-box">? <picker-view :value="currentPick" @change="handlePickerChange">

? ? <picker-view-column>? ? ? <viewv-for="(item, index) in areaList":key="index">{{item.text}}</view>? ? </picker-view-column>? </picker-view>? <viewclass="picker-view mask"></view>? <button class="button" @click="onConfirmSearchArea">

? ? 確認(rèn)

? </button></view>

11. 在checkbox或radio外包一層label就可以點(diǎn)擊文字部分也能控制checkbox或radio的選中了(文檔中未講但示例中有此寫法)

<checkbox-group@change="handleManageModeChange"><labelclass="checkbox-item"v-for="item in manageModeSelection":key="item.dictCode"><p>{{item.dictName}}</p><checkboxclass="checkbox":value="item.dictCode":checked="item.checked"color="#1989fa"/></label></checkbox-group>

12. 數(shù)組的深層監(jiān)聽無效

watch:{arr:{handler(val){...},deep:true}}

13. 不支持new Function()和eval()

(1) new Function()

mounted(){letfuncStr=this.checkIDCard.toString();letfunc=newFunction('return '+funcStr);console.log(funcStr,func,Object.prototype.toString.apply(func));// 運(yùn)行func()會報錯TypeError: func is not a function},methods:{checkIDCard(){...}}

打印

(2)eval()

mounted(){letfuncStr=this.checkIDCard.toString();// 轉(zhuǎn)函數(shù)為字符串钳踊,可作為參數(shù)傳遞至別的方法或組件中;// 或this.checkIDCard + ''或String(this.checkIDCard)letfunc=eval("(false || "+funcStr+")");// 報錯},methods:{checkIDCard(params){...}}

控制臺報錯

14. regexp和function無法參與到組件間參數(shù)props傳遞

// parent.vue<testref="textArea":getValue="getValue":pattern="pattern"></test>...components:{test},data(){return{pattern:/\d/}},methods:{getValue(params){console.log(params)}}

// test.vueprops:['getValue','pattern'],mounted(){console.log(this.getValue,this.pattern)},

打印

如圖伺通,經(jīng)過props傳遞的兩個參數(shù)箍土,function變成未定義逢享,regExp變成空對象罐监。

解決:

對于正則表達(dá)式:可傳遞字符串后,在目標(biāo)位置new RegExp(regexpStr)

// parent.vuepattern:"\\d"

// test.vueletregexp=newRegExp(this.pattern)console.log(this.getValue,regexp,regexp.test('abc'))// 已可以正常使用正則的方法

打印

對于函數(shù):new Function('return '+funcStr)無效瞒爬;可通過其余方法繞行調(diào)用如父子組件$emit

替換辦法:

// parent.vue<testref="textArea":getValue="funcName":pattern="pattern"></test>...components:{test},data(){return{pattern:"\\d",funcName:"getValue"}},methods:{getValue(params){console.log(params)}}

// test.vueprops:['getValue','pattern'],mounted(){console.log(this.$parent[this.getValue],regexp,regexp.test('abc'))},

打印

拓展:在h5中弓柱,函數(shù)字符串轉(zhuǎn)函數(shù)的方法有

參考:js字符串轉(zhuǎn)函數(shù)

(1) new Function()

functioncheckIDCard(params){...}exportdefault{...mounted(){letfuncStr=checkIDCard.toString();// 轉(zhuǎn)函數(shù)為字符串,可作為參數(shù)傳遞至別的方法或組件中侧但;// 或checkIDCard + ''或String(checkIDCard)letfunc=newFunction('return '+funcStr)();// 轉(zhuǎn)字符串為函數(shù)letparams=...func(params);// 運(yùn)行函數(shù)},methods:{checkIDCard(params){...}}}

(2)eval()

functioncheckIDCard(params){...}exportdefault{...mounted(){letfuncStr=checkIDCard.toString();// 轉(zhuǎn)函數(shù)為字符串矢空,可作為參數(shù)傳遞至別的方法或組件中;// 或checkIDCard + ''或String(checkIDCard)letfunc=eval("(false || "+funcStr+")");// 轉(zhuǎn)字符串為函數(shù)letparams=...func(params);// 運(yùn)行函數(shù)},methods:{checkIDCard(params){...}}}

注:若使用this.checkIDCard會出錯禀横,因?yàn)閠his.checkIDCard.toString()=>"function () { [native code] }"屁药,這種字符串無法作為js代碼執(zhí)行。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末柏锄,一起剝皮案震驚了整個濱河市酿箭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌趾娃,老刑警劉巖缭嫡,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異抬闷,居然都是意外死亡妇蛀,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門笤成,熙熙樓的掌柜王于貴愁眉苦臉地迎上來评架,“玉大人,你說我怎么就攤上這事炕泳∽莸” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵喊崖,是天一觀的道長挣磨。 經(jīng)常有香客問我雇逞,道長,這世上最難降的妖魔是什么茁裙? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任塘砸,我火速辦了婚禮,結(jié)果婚禮上晤锥,老公的妹妹穿的比我還像新娘掉蔬。我一直安慰自己,他們只是感情好矾瘾,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布女轿。 她就那樣靜靜地躺著,像睡著了一般壕翩。 火紅的嫁衣襯著肌膚如雪蛉迹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天放妈,我揣著相機(jī)與錄音北救,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的铣耘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼攘宙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了拐迁?” 一聲冷哼從身側(cè)響起蹭劈,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎唠亚,沒想到半個月后链方,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡灶搜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年祟蚀,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片割卖。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡前酿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鹏溯,到底是詐尸還是另有隱情罢维,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布丙挽,位于F島的核電站肺孵,受9級特大地震影響匀借,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜平窘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一吓肋、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瑰艘,春花似錦是鬼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至芒率,卻和暖如春囤耳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背敲董。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工紫皇, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人腋寨。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像化焕,于是被迫代替她去往敵國和親萄窜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容