【问题标题】:Return model value formatted with Vue.js返回使用 Vue.js 格式化的模型值
【发布时间】:2020-08-11 16:06:29
【问题描述】:

我在 Vue.js 应用程序中有一个表格,列出了一个 URL 和一个 ID。这个 URL 是由用户定义的,所以我创建并输入了带有输入文本的组件,使用 URL 作为值和 Id 作为参数。该组件的 v-model 是一个数组,我需要将数据存储为 JSON 对象,如下所示:

{ 
    "id": 1, 
    "url": "www.some-url.com" 
}

如何捕获 url 字段中的更改并返回其父项以附加到数组中?

组件:

<template>
  <div class="row">
      <div class="col-md-12">
        <input type="text"
               class="form-control"
               v-model="value.url">
      </div>
  </div>
</template>
<script>
  export default {
    name: 'inputUrl',
    props: {
      url: {
        type: [String],
        description: 'URL'
      },
      id: {
        type: Number,
        description: 'Id'
      }
    },
    components: {
    }
    data() {
      return {
        value: {
          id: this.id,
          url: this.default
        }
      };
    },
    methods: {
    },
    mounted() {
    },
    watch: {
    }
  }
</script>

用法:

<inputUrl
    :id="1"
    url="www.some-url.com"
    v-model="array">
</inputUrl>

【问题讨论】:

  • 只有在组件内部的输入中添加v-bind:value="value" v-on:input="$emit('input', $event.target.value)" 以捕获更改时,才能将v-model 与自定义组件一起使用。

标签: vue.js vue-component


【解决方案1】:

我将array 变量传递给InputUrl 组件,然后使用v-on 指令将当前输入值传递给自定义函数,以便将新值附加到array 变量。 这里是example

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2018-09-25
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    相关资源
    最近更新 更多