日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue vant Area组件使用详解

發(fā)布時間:2024/9/27 vue 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue vant Area组件使用详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


文章目錄

            • 1. 下載areaList.js
            • 2. 組件注冊
            • 3. 封裝組件
            • 4. 使用組件
            • 5. 效果圖
            • 6. 項(xiàng)目源碼

1. 下載areaList.js

見文章末尾

2. 組件注冊

main.js引入并注冊(一般與Popup一起使用)

  • 全局注冊
//全局導(dǎo)入所有組件 import Vant from 'vant'; import 'vant/lib/index.css';Vue.use(Vant)
  • 局部注冊
import { Area, Popup } from 'vue-router' Vue.use(Area) Vue.use(Popup)
3. 封裝組件

在src/components新建CascadeChoice.vue組件

<template><div><div class="flex-input"><div class="tx-lable">{{ itemName }}</div><div class="tx-input" @click="areaChoose"><inputtype="text":placeholder="phdText"v-model="chooseValue"readonly/><img class="my-img" src="@/assets/images/icon_arrow_right.png"/></div></div><van-popup v-model="showAddrPopup" position="bottom"><van-arearef="myArea"title="選擇地區(qū)":area-list="areaList"@confirm="confArea"@change="onChange"@cancel="onCancel"/></van-popup></div> </template> <script> import AreaList from "@/assets/js/area.js";export default {props: {itemName: {type: String, //按鈕名稱default: "地區(qū)"},phdText: {type: String, //按鈕名稱default: "請選擇地區(qū)"},showUnderline: {type: Boolean,default: true}},components: {},data() {return {//省市區(qū)列表areaList: {},//彈出層展示showAddrPopup: false,//數(shù)據(jù)滾動選擇臨時數(shù)據(jù)chooseTempValue: "",//頁面選擇后的數(shù)據(jù) 和v-model一致chooseValue: ""};},//在實(shí)例被創(chuàng)建之后被調(diào)用,初始化省區(qū)縣數(shù)據(jù)created() {this.initParams();},methods: {/*** 初始地區(qū)化數(shù)據(jù)*/initParams() {this.areaList = AreaList;},/*** 數(shù)據(jù)滾動選擇監(jiān)聽數(shù)據(jù)變化* @param picker* @param index* @param value* value=0改變省,1改變市,2改變區(qū)*/onChange(picker, index, value) {let val = picker.getValues();console.log(val)//查看打印let areaName = ''for (var i = 0; i < val.length; i++) {areaName = areaName + (i == 0 ? '' : '/') + val[i].name}//保存滾動選擇的值this.chooseTempValue = areaNameconsole.log("chooseTempValue", this.chooseTempValue)},//點(diǎn)擊imput觸發(fā),彈框事件areaChoose() {this.showAddrPopup = true;},//點(diǎn)擊取消后的事件onCancel() {this.showAddrPopup = false//關(guān)閉彈框//由于對滾動的數(shù)據(jù)保存到臨時的變量chooseTempValue中,與選擇后現(xiàn)實(shí)的數(shù)據(jù)做了解耦合,因此,這里不炫耀重置城市列表// this.$refs.myArea.reset()// 重置城市列表},//當(dāng)提交表單數(shù)據(jù)時需要,將地區(qū)數(shù)據(jù)提交到后端,因此,當(dāng)選擇完成后//確定選擇后保存事件confArea(data) {this.chooseValue = this.chooseTempValueconsole.log(data[0].name + "," + data[1].name)this.showAddrPopup = false//關(guān)閉彈框// 將子組件的值回傳給父組件this.$emit('callBackParent', this.chooseValue)}} }; </script> <style lang="less" scoped>.flex-input {display: flex;justify-content: space-between;background-color: #ffffff;height: 56px;padding: 0 25px;div {font-size: 16px;color: #2e042c;letter-spacing: 0;.my-img {height: 17px;margin-top: 2%;}} }.tx-lable {margin: 16px 0;height: 24px;line-height: 24px; }.tx-input {display: flex;justify-content: flex-end;margin: 16px 0;line-height: 24px;input {border: none;margin-right: 5px;text-align: right;}input::-moz-placeholder {color: #f6e9f7;}img {margin: 7px 5px;height: 12px;width: 12px;} } </style>
4. 使用組件
<template><div><cascade-choice:item-name="'地區(qū)'":phd-text="'請選擇地區(qū)'"@callBackParent="callBackParent"/></div> </template> <script> import CascadeChoice from "../../components/CascadeChoice";export default {name: 'Home',data() {return {// 定義城市變量值cityValue: ''}},components: {CascadeChoice},methods: {//父組件接收子組件回傳的參數(shù)值callBackParent(obj) {console.log("obj", obj)this.cityValue = obj}} } </script>
5. 效果圖

6. 項(xiàng)目源碼

https://gitee.com/gblfy/vue2-vant-h5

總結(jié)

以上是生活随笔為你收集整理的vue vant Area组件使用详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。