学习Vant中,van-dialog不能弹窗问题解决。

文章讲述了在使用Vue前端框架时,遇到van-dialog弹窗组件无法正常显示的问题,通过debug发现v-model属性错误,修正为v-model:show后解决,强调了全局和局部注册van-dialog组件的方法。
摘要由CSDN通过智能技术生成

        在学习前端Vue中,使用van-dialog发现不能弹窗,源代码如下

<template>
  <div class="goods_car_wrap">
    <div class="address">
      <div v-if="addressList.length <=0" class="add_address" @click="handleAddAddress">
        <van-icon name="add" />
        <span>添加地址</span>
      </div>
      <div v-else>

      </div>
    </div>
    <div class="goods_list_wrap">
      <div class="item flex" v-for="item in carGoodsList" :key="item.id">
        <img :src="require('../assets/image/goods/' + item.goodsPicture)" alt="">  
        <div class="wrap">
          <p>商品名:{{ item.goodsName }}</p>    
          <p>单价:{{ item.goodsPrice }}元</p>
          <p>总金额:{{ item.goodsPrice * item.goodsNum }}元</p>
        </div> 
        <van-stepper v-model="item.goodsNum" async-change @change="(value)=>{handleChange(value,item)}"/>
      </div>      
    </div>
    <van-submit-bar class="sub_wrap" :price="allPrice" button-text="提交订单" @submit="onClickButton" />
    <TabBar/>
    <!-- 添加地址弹窗 -->
    <van-dialog v-model="addressShow" title="添加地址" show-cancel-button>    
      <van-cell-group>
        <van-field v-model="tel" label="姓名" type="tel" />
        <van-field v-model="tel" label="手机号" type="number" required />
        <van-field v-model="tel" label="地址" type="textarea" rows="2" autosize required />
      </van-cell-group>
    </van-dialog>
  </div>
</template>

<script lang="ts">
import {Options,Vue} from 'vue-class-component'
import {Watch} from 'vue-property-decorator'
import TabBar from '@/components/tabBar.vue'
import { useRoute, useRouter } from 'vue-router'
import {getCarListApi,putAddCarApi,getUserAddrApi,putUserAddrApi} from '@/utils/request'
import {goodsListType} from '@/utils/interface'
import {useStore} from 'vuex'

@Options({
    components:{
      TabBar
    }    
  })
export default class goodsCar extends Vue{
  router = useRouter()
  route = useRoute()
  store = useStore()

  carGoodsList:goodsListType[] = []  
  addressList:any[] = []
  allPrice = 0
  addressShow = false
  tel: any

  //监控watch carGoodsList改变后重新计算价格
  @Watch('carGoodsList',{deep:true,immediate:true})
  watchCarGoodsList(val:goodsListType[],oldVal:goodsListType[]){
    this.allPrice = this.countPrice()
  }

  onClickButton = ()=>{
    console.log('display')
  }
  countPrice = ():number=>{
    let price = 0
    this.carGoodsList.forEach(el=>{
      price += el.goodsNum * el.goodsPrice
    })
    return price*100
  }

  handleChange = async(val:string,item:goodsListType):Promise<boolean>=>{
    const res = await putAddCarApi({
      'userId': this.store.state.uInfo[0].id,
      'goodsId': item.id,
      'goodsNum': val
    })
    if (res.status == 200){
      return true
    } else {
      return false
    }
  }

  //去购物车地址
  getAddress = async():Promise<void>=>{
    const res = await getUserAddrApi({
      userId:this.store.state.uInfo[0].id
    })
    if (res.status == 200){
      console.log('客户收货地址:',res)
      this.addressList = res.data
    }
  }

  //添加地址
  handleAddAddress = ()=>{
    console.log('显示地址弹窗框',this.addressList.length)
    this.addressShow = true
  }

  //初始化加载
  init = async():Promise<void>=>{
    // console.log('this.init()')
    // console.log('this.store.state.uInfo[0].id',this.store.state.uInfo[0].id)
    if (this.store.state.uInfo[0].id){
      // console.log('this.store.state.uInfo[0].id',this.store.state.uInfo[0].id)
      const res = await getCarListApi({
        userId:this.store.state.uInfo[0].id
      })
      // console.log('res',res.status)
      if (res.status == 200){
        // console.log('this.carGoodsList',this.carGoodsList)
        this.carGoodsList = res.data
        this.allPrice = this.countPrice()
      }
      this.getAddress()
    }
  }
  public mounted(){
    // console.log('this.init()')
    this.init()
  }
}

</script>
<style lang="less" scoped>
  .goods_car_wrap{
    .item{
      padding: 30px;
      background: #fff;
      margin-bottom: 30px;
      .wrap{
        font-size: 15px;
        p{
          padding-bottom: 5px;
        }
        img{
          display: block;
          width: 100px;
          height: 100px;
          margin-right: 30px;
        }
      }    
    } 
    .sub_wrap{
      bottom: 50px;
    }   
    .address{
      width: 300px;
      height: 300px;
      background: #fff;
      margin-bottom: 30px;
      padding: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: row;
      
      .add_address{
        font-size: 15px;
        font-weight: bold;
      }
    }       
  }
  
</style>

参考网上大神的解决方案,

如全局注册,依然不起作用。

import { Dialog } from 'vant';
// 全局注册
Vue.use(Dialog);
// 局部注册
export default {
  components: {
    [Dialog.Component.name]: Dialog.Component
  }
}

一点点debug发现,问题出在v-model="addressShow"上                              

<van-dialog v-model="addressShow" title="添加地址" show-cancel-button>

只要改成 v-model:show="addressShow" 就可以了。虽然在VS编辑器上有红色错误提示。

<van-dialog v-model:show="addressShow" title="添加地址" show-cancel-button>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值