Vue组件封装 ——input 输入框组件

本文详细介绍了如何在Vue.js环境中创建一个功能丰富的Input组件,包括输入框类型切换、双向数据绑定、清空输入框功能以及密码明文显示切换。组件支持多种属性如placeholder、type、name、disabled、value、clearable和showPassword,并通过事件传递数据。同时,给出了App.vue中使用组件的示例。
摘要由CSDN通过智能技术生成

  一、基础准备工作

1、创建一个基础的vue项目包

2、创建components文件夹,用于存放组件,新建input.vue组件,可以自己取个名字

<script>
export default {
    name: "CatInput",
};
</script>

3、在main.js中引入组件

import CatInput from "./components/input.vue";
Vue.component(CatInput.name, CatInput);

4、App.vue中使用组件

二、input组件结构搭建

       主要功能为:输入框类型切换、双向数据绑定、清空输入框、密码明文显示切换

       输入框需要绑定v-model,实际上是一个语法糖,等价于:

                  :value="uname"

                  @input="uname=$event.target.value"

        需要传入的参数:

        placeholder:字段预期值的提示信息

        type:文本框类型

        name:name

        disabled:是否禁用

        value:值

        clearable:是否显示清空按钮

        showPassword:密码显示

        data中,添加字段 passwordVisiable,控制是否明文显示密码,因为子组件无法直接修改父组件传入的type值

页面效果:

                                       

input.vue 具体代码如下,style样式太多,不逐一列出了,可根据需求自己定义: 

<template>
  <div class="cat-input" :class="{ 'cat-input--suffix': showSuffix }">
    <!-- type:先判断是否有传入显示密码,控制输入框类型是文本/密码,然后是type传入的值 -->
    <input
      :type="showPassword ? (passwordVisiable ? 'text' : 'password') : type"
      class="cat-input__inner"
      :placeholder="placeholder"
      :name="name"
      :disabled="disabled"
      :class="{ 'is-disabled': disabled }"
      :value="value"
      @input="handleinput"
    />
    <span class="cat-input__suffix" v-if="showSuffix">
      <i
        class="cat-input__icon cat-icon-circle-close"
        v-if="clearable && value"
        @click="clear"
      ></i>
      <i
        class="cat-input__icon cat-icon-view"
        :class="{ 'cat-icon-view-active': passwordVisiable }"
        v-if="showPassword"
        @click="handlepwd"
      ></i>
    </span>
  </div>
</template>

<script>
export default {
  name: "CatInput",
  props: {
    placeholder: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "text",
    },
    name: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    value: {
      type: String,
      default: "",
    },
    clearable: {
      type: Boolean,
      default: false,
    },
    showPassword: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      passwordVisiable: false, //控制是否显示密码
    };
  },
  methods: {
    handleinput(event) {
      //父组件在绑定v-model时,其实就绑定的input事件,因此父组件不需要再声明事件了
      this.$emit("input", event.target.value);
    },
    clear() {
      this.$emit("input", "");
    },
    handlepwd() {
      this.passwordVisiable = !this.passwordVisiable;
    },
  },
  computed: {
    //有清空/显示密码,添加类名、显示span
    showSuffix() {
      return this.clearable || this.showPassword;
    },
  },
};
</script>

 

App.vue使用input组件:

<template>
  <!-- 
  输入框需要绑定v-model,实际上是一个语法糖,等价于:
  :value="uname"
  @input="uname=$event.target.value"
 -->
  <div>
    <Cat-input
      placeholder="请输入用户名"
      type="text"
      v-model="uname"
      clearable
    ></Cat-input>
    <br />

    <Cat-input placeholder="禁用的输入框" disabled></Cat-input>
    <br />

    <Cat-input
      placeholder="请输入密码"
      v-model="upwd"
      show-password
    ></Cat-input>
  </div>
</template>

<script>
export default {
  data() {
    return {
      uname: "",
      upwd: "",
    };
  },
};
</script>

<style lang="scss" scoped>
.cat-input {
  margin-bottom: 10px;
}
</style>

好的,针对您的问题,我可以给您提供一个简单的Vue组件的代码示例,该组件封装了一个名为`text`的字符串类型的prop属性,并且在组件内部将这个属性的值通过事件`input`传递给了父组件,从而实现了组件向引用页面传值的功能。您可以将这个示例代码集成到您的项目中,也可以根据自己的需求进行修改: ```vue <template> <div> <input type="text" :value="text" @input="$emit('input', $event.target.value)"> </div> </template> <script> export default { name: 'MyComponent', props: { text: { type: String } } } </script> ``` 在这个组件中,我们使用了Vue的`props`属性来声明一个名为`text`的prop属性,类型为字符串类型。在模板中,我们使用了Vue的`v-bind`指令将`text`绑定到了一个输入框的`value`属性上,并且监听了输入框的`input`事件。当输入框的值发生变化时,我们通过`$emit`方法触发了一个名为`input`的事件,并将输入框的新值作为参数传递给了这个事件。 在使用这个组件的时候,我们可以在父组件中使用`v-model`指令来绑定组件的`text`属性。例如: ```vue <template> <div> <my-component v-model="message"></my-component> <p>{{ message }}</p> </div> </template> <script> import MyComponent from '@/components/MyComponent' export default { name: 'App', components: { MyComponent }, data() { return { message: 'Hello, World!' } } } </script> ``` 在这个示例中,我们使用了`v-model`指令将`my-component`组件的`text`属性绑定到了父组件的`message`属性上。这样,当`my-component`组件输入框的值发生变化时,`message`的值也会随之变化,并且在页面上展示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值