vue.js 三元表达式到达目标值的v模型

0sgqnhkj  于 2023-03-03  发布在  Vue.js
关注(0)|答案(1)|浏览(195)

当我尝试使用v-model的三元表达式时,我可以访问数据(在此代码示例中:name:'data_value'),但当我尝试访问“detailCompanyDatas.smCompany.name“时,我无法访问。我该怎么办?
谢谢

<v-text-field
       v-model="$data[dialogDetail ? 'name' : null]"
    />  // this works

   <v-text-field
       v-model="$data[dialogDetail ? 'detailCompanyDatas.smCompany.name' : null]"
    />  // not works

   data () {
       return {
         dialogDetail: true,
         detailCompanyDatas: {
           smCompany: {
            name: 'object_value'
          }
        },
        name: 'data_value',
      } 
     }
twh00eeo

twh00eeo1#

您可以将v-model绑定到可写的计算:

<v-text-field
       v-model="companyName"
    />
data () {
        return {
          dialogDetail: true,
          detailCompanyDatas: {
            smCompany: {
             name: 'object_value'
           }
         },
         name: 'data_value',
       },
     computed:{
         companyName:{
                get(){
                  return this.dialogDetail? this.detailCompanyDatas.smCompany.name : null
                },
                set(val){
                  this.detailCompanyDatas.smCompany.name = this.dialogDetail? ? val:null
                }
  
            }
          } 
      }
</script>

相关问题