mint-ui popup + 父子传值 + 子父传值
参考:
https://segmentfault.com/q/1010000007959780
XML/HTML Code复制内容到剪贴板
- <!-- 加参数 :modal=false 关闭遮罩 -->
- <mt-popup v-model="popupVisible" position="bottom">
- <chooseType :type=type> </chooseType>
- </mt-popup>
解释:
1、定义popupVisible值为true 或者 false 表示打开popup层,position表示弹层位置
2、:type 表示 父层将传type值给子层
父层代码:
XML/HTML Code复制内容到剪贴板
- <!-- 加参数 :modal=false 关闭遮罩 -->
- <mt-popup v-model="popupVisible" position="bottom">
- <chooseType :type=type> </chooseType>
- </mt-popup>
- <script>
- import chooseType from '@/components/cms/chooseType.vue'
- export default {
- name: 'publish',
- data: function() {
- return {
- popupVisible: false, //popup 弹层默认关闭
- type: 1, //1不限,2免费
- }
- },
- components: {
- chooseType
- },
- methods: { // 这里放方法,方法之间用逗号隔开
- publishFree() {
- // 发布免费内容,改变传入子层中的type值
- this.type = 2;
- this.openPopup();
- },
- publishPay() {
- //发布付费内容,改变传入子层中的type值
- this.type = 1;
- this.openPopup();
- },
- //打开弹出层,默认有透明背景,点击遮罩关闭弹出层
- openPopup() {
- // 打开popup
- this.popupVisible = true
- },
- closePopup() {
- // 关闭popup
- this.popupVisible = false
- },
- }
- </script>
子层接收传参:
XML/HTML Code复制内容到剪贴板
- <template>
- <div class="type-main">
- <p @click="publish($store.state.common.publishLive,type)">发布</p>
- </div>
- </template>
- <script>
- export default {
- name: 'chooseTag',
- data: function() {
- return {
- }
- },
- props:['type'],
- methods: {
- publish(type,fee){
- console.log(fee);
- }
- }
- }
- </script>
子父传值:
XML/HTML Code复制内容到剪贴板
- <!--
- 要求:子层点击确定的时候,执行关闭popup层
- v-on:childMethod 监听子层传过来的方法名
- -->
- <mt-popup v-model="popupVisible" position="right">
- <chooseTag :status="popupVisible" v-on:childMethod="listenToChooseTagCom"> </chooseTag>
- </mt-popup>
- <script>
- export default {
- name: 'publishBase',
- data: function() {
- return {
- popupVisible: false, // popup默认为关闭
- }
- },
- methods: {
- listenToChooseTagCom(val) {
- //监听选择tag的组件内传值,如果想要关闭,子层传一个false值过来即可
- this.popupVisible = val
- }
- }
- }
- </script>
- 子层:
- <template>
- <button class="fr" @click="closePopup">关闭popup层</button>
- </template>
- <script>
- export default {
- methods: {
- closePopup() {
- // 关闭当前popup,向父层传值
- this.$emit('childMethod', false);
- }
- }
- }
- </script>
DEMO1:选择标签,有确定选择按钮,支持关闭事件
XML/HTML Code复制内容到剪贴板
- <!-- 打开选择tag弹出层,监听子层是否要关闭 -->
- <mt-popup v-model="popupTagVisible" position="bottom" class="tagMainPopup">
- <chooseTag v-on:childClosePopup="listenTagPopupIfClose" v-on:chooseTags="listenTagPopupChoose" :tags="defaultTags"> </chooseTag>
- </mt-popup>
JavaScript Code复制内容到剪贴板
- <script>
- import chooseTag from "@/components/cms/chooseTag.vue";
- export default {
- name: "QuestionnaireBody",
- data() {
- return {
- popupTagVisible: false, // 选择tag的popup默认为关闭
- };
- },
- computed: {
- defaultTags() {
- return this.publish_tag ? this.publish_tag.join(",") : "";
- },
- },
- components: {
- chooseTag,
- },
- mounted() {
- this.init();
- },
- methods: {
- init() {
- // 初始化
- },
- openTagPopup() {
- // 打开tag选择的popup
- this.popupTagVisible = true;
- },
- listenTagPopupIfClose(val) {
- //监听tag弹出层是否关闭popup
- this.popupTagVisible = val;
- },
- listenTagPopupChoose(val) {
- //监听选择的tag标签
- this.publish_tag = val;
- },
- destroyed() {
- // 生命周期 - 离开当前路由后操作
- }
- };
chooseTag.vue:
XML/HTML Code复制内容到剪贴板
- <template>
- <div class="classify_list profession_item">
- <div class="box_shadow classify_details_item">
- <div class="tit clear">
- <h3 class="fl">更多标签
- <span class="tip">注:最多选择3个标签</span>
- </h3>
- <button class="fr" @click="closePopup">确定</button>
- </div>
- <span v-for="(tag, index) in tagList" :value="tag.tagName" :class="{'classify_item active-choose':tag.isA,'classify_item':!tag.isA}" v-on:click="addTag($event,tag,index)">{{tag.tagName}}</span>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "chooseTag",
- data: function() {
- return {
- tagList: this.getTagList(), //获取浏览器存储的tagList
- value: [] //选择的tag数组
- };
- },
- props: [
- "tags", //父组件传过来的默认选中的tag名称
- ],
- methods: {
- getTagList() {
- // 获取tag的列表数据,这里从缓存中取,也可以跑接口,据业务场景来定
- let list = sessionStorage.getItem("tagList");
- return JSON.parse(list);
- },
- addTag(event, tag, index) {
- // 添加tag
- let isA = this.tagList[index].isA;
- let that = this;
- if (!isA) {
- // 在选中的时候要判断
- if (this.value.length + 1 > 3) {
- that.$toast({
- message: "最多只能选择三个!",
- position: "top",
- className: "toast-z"
- });
- return false;
- }
- }
- this.value = [];
- this.$set(this.tagList[index], "isA", !isA);
- this.tagList.forEach(function(item) {
- if (item.isA) {
- that.value.push(item.tagName);
- }
- });
- },
- cancelChoose() {
- // 取消选择按钮,事件保留
- this.value = [];
- this.tagList.forEach(function(item) {
- item.isA = false;
- });
- this.closePopup();
- },
- closePopup() {
- // 关闭当前popup
- this.$emit("childClosePopup", false);
- }
- },
- watch: {
- tags() {
- let vm = this;
- //console.log(this.tags);
- if (vm.tags.length) {
- // 如果父层有tag传递进来
- let tags = vm.tags.split(",");
- vm.value = tags; //给已选择的数组赋值
- vm.tagList.forEach(function(item, index) {
- if (tags.indexOf(item.tagName) !== -1) {
- vm.$set(vm.tagList[index], "isA", true);
- }
- });
- //console.log(tags);
- }
- },
- value() {
- //设置选择的tag 监听,并且即时传给父层展示
- this.$emit("chooseTags", this.value);
- }
- }
- };
- </script>