vue中 input type=date组件封装及调用

12 篇文章 0 订阅

封装组件vue

<template>
  <div class="inputItem">
    <div class="leftText">{{textName}}</div>
    <div class="rightText">
      <input type="date" class="inputstyle" v-on:change="dateChanged" >
    </div>
  </div>
</template>

<script>
export default {
  name: "InputDate",
  props: ["textName"],
  data() {
    return {};
  },
  mounted() {},

  methods: {
  //监听日期改变函数
    dateChanged: function($event) {
      this.$emit("dateChanged", $event); //将值放在自定义的事件函数中作为参数
    }
  },
  computed: {}
};
</script>


<style scoped>
input:focus {
  outline: none;
  border: 1px solid #fff;
}
.inputItem {
  width: 100%;
  height: 60px;
  font-size: 18px;
  text-indent: 10px;
  letter-spacing: 10px;
  display: flex;
  display: -webkit-flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  align-content: center;
  border-bottom: 1px solid #eceef1;
}
.leftText {
  width: 15%;
  padding-left: 1%;
  color: #757575;
  overflow: hidden;
  word-break:keep-all;
  white-space:nowrap;  
}
.rightText {
  width: 84%;
}
.inputstyle {
  width: auto;
  min-width: 50%;
  border: none;
  outline: none;
  text-indent: 3px;
  letter-spacing: 3px;
  color: #757575;
  /* border-bottom: 1px solid #6a7c9f; */
}
</style>

调用组件vue

<template>
  <div class="main" :style="{'height':getClientHeight1}">
    <div class="menuContainer">
      <div class="menuItem">
        <span class="exportBarText">
          test >
          <b>test</b>
        </span>
      </div>
      <div class="menuItem" id="menuItem"></div>
    </div>

    <div class="dataList" :style="{'height':getClientHeight2}">
      <div class="reactDatalist">
      
          <InputDate textName="日期"  v-on:dateChanged="dateChanged($event)"></InputDate>
          
          <div class="btnContainer">
              <button class="btn" @click="clicking($event)">确定</button>
          </div>
      </div>
    </div>
    <toast v-show="toastShow" :msg="toastMsg" v-on:closeToast="closeToast($event)"></toast>
  </div>
</template>

<script>
import toast from "../basicComponents/Toast";
import InputDate from "../basicComponents/InputDate";
export default {
  components: {
    InputDate,
    toast
  },
  data() {
    return {
      toastShow: false,
      toastMsg: "",
      date:"",
    };
  },

  methods: {
    closeToast($event) {
      console.log("closeToast");
      this.toastShow = false;
    },
    showToast: function(content) {
      let that = this;
      that.toastShow = true;
      that.toastMsg = content;
      setTimeout(function() {
        that.toastShow = false;
      }, 2000);
    },

    dateChanged:function ($event) {
        console.log($event.target.value)
    },
    clicking:function ($event) {
      console.log("pushKafka");
      console.log(this.$refs.inputDate)
    }
  },
  mounted: function(e) {
    console.log("mounted query Terminal Data");
  },
  computed: {
    getClientHeight1: function() {
      return tools.getClientHeight() - 100 + "px";
    },
    getClientHeight2: function() {
      return tools.getClientHeight() - 160 + "px";
    },
    saveTerminalListData: function(list) {
      return this.$store.commit("TerminalList", list);
    }
  }
};
</script>
<style  scoped>
.menuContainer {
  width: 100%;
  height: 60px;
  line-height: 60px;
  display: flex;
  display: -webkit-flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  align-content: center;
}
.menuItem {
  width: 50%;
  height: 60px;
}
#menuItem {
  position: relative;
  padding-left: 24%;
}

.main {
  width: 100%;
  float: left;
  font-family: NotoSansHans-Light, Arial, Helvetica, sans-serif;
}

.dataList {
  position: relative;
  width: 100%;
  background: #f0f3fa;
}

.reactDatalist {
  position: absolute;
  top: 7%;
  left: 50%;
  width: 97%;
  min-height: 400px;
  transform: translateX(-50%);
  border-radius: 8px;
  box-shadow: 0px 0px 12px -2px #2a3853;

  overflow: auto;
  background: #fff;
}
.exportBarText {
  width: 300px;
  height: 60px;
  display: inline-block;
  padding-left: 20px;
  color: #94a0b9;
}
.btnContainer{
  width: 100%;
  height: 60px;
  margin-top: 20px;
  font-size: 18px;
  text-indent: 10px;
  letter-spacing: 10px;
  display: flex;
  display: -webkit-flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  align-content: center;
}
.btn{
  width: 35%;
  height: 45px;
  font-size: 18px;
  text-indent: 10px;
  letter-spacing: 10px;  
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2,针对input type=file的change事件,可以通过以下步骤进行处理: 1. 首先,我们需要在Vue组件的template定义一个input元素,并指定type为file,用于选择文件: ``` <input type="file" @change="handleFileChange"> ``` 2. 接下来,在Vue组件的methods定义handleFileChange方法,用于处理change事件: ``` methods: { handleFileChange(event) { // 通过event.target.files获取选择的文件 const file = event.target.files[0]; // 进行相应的操作或赋值 this.uploadFile(file); }, uploadFile(file) { // 上传文件的逻辑处理 // 可以使用第三方库如axios进行文件上传操作 // 也可以调用后端接口进行文件上传 console.log('上传文件:', file); } } ``` 3. 在handleFileChange方法,我们可以通过event.target.files获取到选择的文件列表。由于input type=file元素支持多文件选择,所以通过files属性获取到的是一个文件数组。在大多数情况下,我们只需要选择单个文件,所以可以直接取files[0]获取到文件对象。 4. 在handleFileChange方法,我们可以通过this.uploadFile(file)来调用上传文件的逻辑处理函数。在uploadFile函数,可以进行相应的文件上传操作,如使用第三方库axios发送请求,或调用后端接口进行文件上传。 总结:以上就是在Vue2处理input type=file的change事件的步骤。首先在template定义input元素,并指定type为file,然后在methods定义处理change事件的方法,并在其处理选择的文件。通过event.target.files获取选择的文件,再进行相应的操作或赋值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值