vue之封装组件,组件可使用v-model语法糖

vue之封装组件,组件可使用v-model语法糖

Input.vue

<template>
  <div class="x_input">
    <input
      :type="type"
      :class="['x_input_inner', { 'is-disabled': disabled }]"
      :placeholder="placeholder"
      :name="name"
      :disabled="disabled"
      :value="value"
      @input="handleInput"
    />
  </div>
</template>

<script>
export default {
  name: "Input",
  props: {
    placeholder: {
      type: String,
      default: "请输入",
    },
    type: {
      type: String,
      default: "text",
    },
    name: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    value: {
      // type: String,
      default: "",
    },
  },
  data() {
    return {};
  },
  methods: {
    handleInput(e) {
      this.$emit("input", e.target.value);
    },
  },
  components: {},
};
</script>
<style  lang="scss" scoped>
.x_input {
  width: 100%;
  font-style: 14px;
  display: inline-block;
  .x_input_inner {
    -webkit-appearance: none;
    background: #fff;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 32px;
    line-height: 32px;
    outline: none;
    padding: 0 15px;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    width: 100%;
    &:focus {
      outline: none;
      border-color: #409eff;
    }
    &.is-disabled {
      border-color: #f5f7fa;
      border-color: #e4e7ed;
      color: #c0c4cc;
      cursor: not-allowed;
    }
  }
}
</style>

使用组件

在这里插入图片描述

<template>
  <div class="home">
    username -- {{ username }}
    <x-input
      class="x_input_wrap"
      placeholder="请输入用户名"
      v-model="username"
    ></x-input>
    age -- {{ age }}
    <x-input
      class="x_input_wrap"
      placeholder="请输入年龄"
      v-model="age"
    ></x-input>
    <x-input
      class="x_input_wrap"
      placeholder="请输入秘密"
      type="password"
      disabled
    ></x-input>
  </div>
</template>

<script>
import XInput from "./Test/Input.vue";
export default {
  name: "Home",
  components: {
    XInput,
  },
  data() {
    return {
      username: "123",
      age: 20,
    };
  },
  methods: {
  },
};
</script>
<style lang="scss" scoped>
.x_input_wrap {
  width: 200px;
}
.home {
  height: calc(100% - 34px);
  width: calc(100% - 34px);
  background: #fff;
  padding: 16px;
  border: 1px solid #ccc;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值