我用vue,返回错误,怎么能不报错?

            <!--拼团信息-->
            <view class="goods contain" v-if="teamInfo">
                <view class="item row">
                    <view class="goods-img">
                        <custom-image width="180rpx" radius="10rpx" height="180rpx" lazy-load :src="''"></custom-image>
                    </view>
                    <view class="goods-info ml20 flex1">
                        <view class="goods-name line2 mb10">
                            <u-tag class="mr10" v-if="teamInfo.found.need" :text="teamInfo.found.need + '人团'" size="mini" type="primary" mode="plain" />
                             {{teamInfo.goods.name}}</view>
                        <view class="goods-spec xs muted mb20">默认</view>
                        <view class="row-between">
                            <view class="goods-price ">
                                <view class="primary">
                                    <price-format :subscript-size="24" :first-size="32" :second-size="24" :price="item.goods_price"></price-format>
                                </view>
                            </view> 
                            <view class="goods-num sm ">x1</view>
                        </view>
                    </view>
                </view>
export default {
        data() {
            return {
                teamInfo:{},
            }
        }
        onShow(){
           this.getTeamInfo()
        },
        methods: {
           getTeamInfo(){
             //请求服务器,假设服务器返回了,是个json集合。
             this.teamInfo = data;
           }
        }
}

如果返回信息正确,浏览器不提示错误。如果一旦返回的是错误信息。浏览器就提示错误,怎么能让他返回错误的,也不报错。

返回信息错误,像这种,肯定是没有的,teamInfo.found.need,肯定报错need,肯定不是存在的。怎么能不报呢?

我上面用了 v-if=“teamInfo“,判断一下。好像不顶用。

阅读 1.6k
1 个回答

因为不存在teamInfo.found,所以可以考虑给它一个初始值。
或者判断那里写v-if="teamInfo.found"

data() {
    return {
        teamInfo:{
            found: {}
        },
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题