1.v-for中,非 h5 平台 :key 不支持表达式 index+'_'

同级多个 v-for 时 key 的值是不允许重复的,key 的绑定是不支持表达式的,需要按照以下方式处理。

// 错误的写法
<view v-for="(item, index) in list1" :key="index">{{item}}</view>
<view v-for="(item, index) in list2" :key="index+'_'">{{item}}</view>
// 正确的写法
<view>
    <view v-for="(item, index) in list1" :key="index">{{item}}</view>
</view>
<view>
    <view v-for="(item, index) in list2" :key="index">{{item}}</view>
</view>

2.onLaunch里获取不到getApp()

控制台"Cannot read property 'globalData' of undefined"解决办法:将getApp()替换成this

onLaunch: function(e) {
      // 错误的写法
      getApp().globalData.uuid = this.getGuid()
      // 正确的写法
      this.globalData.uuid = this.getGuid()
}

3.pages.json里有重复的path在小程序控制台会报错

解决办法:删除重复的pages配置

4.避免使用window,navigator这种浏览器特有的对象,会报undefined

5.微信分享js-sdk,及其他三方的web解决方案,在小程序不能直接正常运转

6.插槽的写法注意:

// 错误的写法
<view v-slot:right v-if="rightText.length">
    123
</view>
// 正确的写法
<view slot="right" v-if="rightText.length">
     123
</view>

以上错误的写法编译都会不通过

7.{{}}里不要写复杂的表达式,否则会出现Bad value with message

// 错误的写法
<text>{{(school.subject_ask || "").length > 0 ? " ": ""}}</text>
// 正确的写法
<text>{{school.subject_ask? " ": ""}}</text>
8.v-if里不要写复杂的表达式,否则会出现Bad value with message(同7)
// 错误的写法
<view v-if="v-if="(item.title || '').length > 0"">{{item.title}}</view>
// 正确的写法1
<view v-if="item.title">{{item.title}}</view>
// 正确的写法2,
<view v-if="item.title.length > 0">{{item.title}}</view>

问题应该出在(item.title || '').length,其中(item.title || '')得到的结果有问题

9.绑定事件时方法里不要套方法,否则会出现Property value expected type of string but got null,编译都会不通过

// 错误的写法
<view @click.stop="clearDefaultSelect(getNum(orderIndex))"></view>
// 正确的写法
<view @click.stop="clearDefaultSelect(orderIndex)"></view>

10.uni.createSelectorQuery().select(id)获取不到节点信息

解决办法:加入in(this)

// 错误的写法
const view = uni.createSelectorQuery().select(id)
view.boundingClientRect(res => {    
 
}).exec()
// 正确的写法
const view = uni.createSelectorQuery().in(this).select(id)
view.boundingClientRect(res => {
 
}).exec()

11.在引用组件时对组件设置class样式在小程序不生效,在h5正常

<comp-a class="a" ></comp-a>
.a{
//针对.a的样式小程序不生效,h5正常
background-color: #F0AD4E;
}

原因:h5引用组件会渲染成uni-view标签,而小程序直接是comp-a标签,此时虽然显示设置了样式,但不会渲染出来解决办法:在子组件中设置好样式,或者用/deep/ 对子组件下的节点设置样式

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。