1

只是尝试使用渲染功能创建组件,但我收到了一个奇怪的警告:

在此处输入图像描述

以下组件:

import Vue, { CreateElement, VNode } from 'vue'

export default Vue.extend({
  name: 'p-form-input',
  props: {
    type: String,
    name: String,
    value: {
      type: [ String, Number ]
    },
    placeholder: String,
    disabled: Boolean
  },
  data() {
    return {
      localValue: ''
    }
  },
  watch: {
    value(value) {
      this.localValue = value
    }
  },
  mounted() {
    this.localValue = this.$props.value
  },
  methods: {
    onInput(e: any) {
      this.localValue = e.target.value
      this.$emit('input', this.localValue)
    },
    onChange(e: any) {
      this.localValue = e.target.value
      this.$emit('change', this.localValue)
    }
  },
  render(h: CreateElement): VNode {
    return h('input', {
      class: 'form-control',
      domProps: {
        disabled: this.$props.disabled,
        type: this.$props.type,
        name: this.$props.name,
        placeholder: this.$props.placeholder,
        value: this.localValue
      },
      on: {
        input: this.onInput,
        change: this.onChange
      }
    })
  }
})

组件上的Av-model="inputValue"确实触发了输入/更改,inputValue但我收到了警告?

使用 vue 2.6.11!

编辑:

不要介意 ts-ignore,它抱怨找不到类型,所以更美观!!!

<template>
  <div id="app">
    <p-form-input type="text" name="some_input" v-model="inputValue" /> {{ inputValue }}
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
// @ts-ignore
import PFormInput from 'vue-components/esm/components/form-input'

export default Vue.extend({
  name: 'App',
  components: {
    PFormInput,
  },
  data() {
    return {
      inputValue: 'fdsdsfdsf'
    }
  }
});
</script>
4

1 回答 1

1

您有一个名为“value”的道具,然后在您的方法中使用了一个名为“value”的变量:

    onInput(e: any) {
      const value = e.target.value

      this.localValue = value

      this.$emit('input', value)
    },

不要重复使用名称“值”。事实上,你甚至不需要那个变量:

    onInput(e: any) {
      this.localValue = e.target.value
      this.$emit('input', this.localValue)
    },

onChange 也是一样:

    onChange(e: any) {
      this.localValue = e.target.value
      this.$emit('change', this.localValue)
    }
于 2020-07-27T07:32:54.520 回答