首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有v模型的Vue.js组件

具有v模型的Vue.js组件
EN

Stack Overflow用户
提问于 2017-06-12 18:19:39
回答 1查看 7.8K关注 0票数 3

我已经能够在自定义组件上完成一个v模型双向绑定的单一层次深度,但需要将其提高一个层次。

现行工作守则:

代码语言:javascript
复制
    <template lang="html">

      <div class="email-edit">

        <input ref="email" :value="value.email" @input="updateInput()"/>
<input ref="body" :value="value.body" @input="updateInput()"/>

      </div>

    </template>
    <script type="text/javascript">
      import LineEditor from './LineEditor.vue'
      export default {
        components: {
          LineEditor
        },
        computed: {
        },
        methods: {
          updateInput: function(){
            this.$emit('input',{
              email: this.$refs.email.value,
              body: this.$refs.body.value
            })
          }
        },
        data: function(){
          return {}
        },
        props: {
          value: {
            default: {
              email: "",
              body: ""
            },
            type:Object
          }
        }
      }
    </script>

像这样使用:<email-edit-input v-model="emailModel" />

但是,如果我添加此部分,则该值将不再向上传播:

代码语言:javascript
复制
      <div class="email-edit">

        <line-editor ref="email" :title="'Email'" :value="value.email" @input="updateInput()"/>
<input ref="body" :value="value.body" @input="updateInput()"/>

      </div>

    </template>
    <script type="text/javascript">
      import LineEditor from './LineEditor.vue'
      export default {
        components: {
          LineEditor
        },
        computed: {
        },
        methods: {
          updateInput: function(){
            this.$emit('input',{
              email: this.$refs.email.value,
              body: this.$refs.body.value
            })
          }
        },
        data: function(){
          return {}
        },
        props: {
          value: {
            default: {
              email: "",
              body: ""
            },
            type:Object
          }
        }
      }
    </script>

使用第二个自定义组件:

代码语言:javascript
复制
<template lang="html">

  <div class="line-edit">
    <div class="line-edit__title">{{title}}</div>
    <input class="line-edit__input" ref="textInput" type="text" :value="value" @input="updateInput()" />
  </div>

</template>
<script type="text/javascript">
  export default {
    components: {
    },
    computed: {
    },
    methods: {
      updateInput: function(){
        this.$emit('input', this.$refs.textInput.value)
      }
    },
    data: function(){
      return {}
    },
    props: {
      title:{
        default:"",
        type:String
      },
      value: {
        default: "",
        type: String
      }
    }
  }
</script>

第一个代码块只需输入就可以正常工作。但是,使用两个自定义组件似乎不会在两个组件中出现气泡,只有LineEditor。如何使这些值在所有自定义组件中出现,而不管嵌套是什么?

EN

回答 1

Stack Overflow用户

发布于 2017-06-12 19:20:48

在我的例子中,手动完成两个组件的通入操作都不起作用。但是,用这个替换我的第一个自定义组件:

代码语言:javascript
复制
<line-editor ref="email" :title="'Email'" v-model="value.email"/>
<input ref="body" :value="value.body" @input="updateInput()"/>

在第一个组件中只使用v-模型,然后允许第二个自定义组件向上发出,就成功了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44506200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档