移动端Vue组件库-Vant学习

全局引入

import Vant from 'vant';  //嫌麻烦就全部一次导出,虽然包会稍微有点大
import 'vant/lib/index.css';  //注意导入全局的这个css,否则布局组件无法正确使用

Vue.use(Vant)

下拉选框

只能单选,不支持多选
返回obj对象,填充picker, 并取选择对象的ID

<div>
	<van-field readonly clickable name="picker" :value="selectedClassify" label="分类" 
	placeholder="点击选择分类" @click="showPicker = true" />
	<van-popup v-model="showPicker" position="bottom">
		<van-picker show-toolbar :columns="columns" value-key="name" @confirm="onConfirm" @cancel="showPicker = false" />
	</van-popup>
</div>

export default {
  data() {
    return {
      selectedClassify: [],
      columns: [{id:1, name:"名称A"}, {id:2, name:"名称B"}]
    };
  },
  methods: {
   	onConfirm(obj) {
		this.selectedClassify.push(obj.id);
		this.showPicker = false;
	}
  }
};

checkbox 多选框

<div>
	<van-checkbox-group v-model="selectedClassify" direction="horizontal">
	<van-checkbox shape="square" v-for="item in tags" v-bind:name="item.id" >{{item.name}}</van-checkbox>
	</van-checkbox-group>
</div>

export default {
  data() {
    return {
     selectedClassify: [],
     tags: []
    };
  },
  methods: {
   	
  }
};

注意:这里的v-bind要放在v-for后面,否则selectedClassify的第一个元素是item.id的字符串,未明白为什么

消息提示

this.$toast('创建成功')
或者
this.$notify('成功')
posted @ 2020-03-28 20:58  柚有有  阅读(468)  评论(0编辑  收藏  举报