将两个或多个数据绑定到 VueJs 中的表单输入

Binding two or more data to a form input in VueJs

我正在考虑在 vue form input binding

中设置多个值

下面是我的代码示例。

<template> 
  <div> 
    <label for=""> Employee </label>
    <input class="form-control-plaintext" v-model="asset.current_status.user.surname" type= "text" readonly name="surname">
  </div>
</template>

<script> 
  data(){
    return:{
      asset:{},
    }
  },

  methods:{
     getAsset(){
        axios.get('/api/assets/'+this.id)
          .then ( response => {
            this.asset = response.data.data[0].data; 
            this.toFill = this.asset()
            this.assetForm.fill(this.toFill)
           })
        },
  },

   mounted(){
        this.getAsset();
    }
</script>

v-model 目前接受并显示姓氏的一个值,我相信可以将另一个值 asset.current_status.user.last_name 包含到 v-model 内联,但目前无法正确处理。

我想你需要这样 computed 属性:

computed: {
    surname () {
         if (this.asset.current_status.user.last_name 
             && this.asset.current_status.user.surname) {

         return this.asset.current_status.user.last_name 
                + this.asset.current_status.user.surname
         }

         return '' // or whatewer else you need...
    }
}

并设置v-model="surname".

详细了解 computed

此外,如果您需要编辑该输入,您将需要 get()set()。 你可以检查这个

祝你好运!

<template> 
  <div> 
    <label for=""> Employee </label>
    <input class="form-control-plaintext" :value="showAssignedUser()" type= "text" readonly name="surname">
  </div>
</template>

<script> 
  data(){
    return:{
      asset:{},
    }
  },

  methods:{
     getAsset(){
        axios.get('/api/assets/'+this.id)
          .then ( response => {
            this.asset = response.data.data[0].data; 
            this.toFill = this.asset()
            this.assetForm.fill(this.toFill)
           })
        },

    showAssignedUser(){
        return `${this.asset.current_status.user.surname}` + ' '+ `${this.asset.current_status.user.first_name}`
    },

  },

  mounted(){
     this.getAsset();
 }
</script>