简体   繁体   English

有没有办法通过 function 和 vue.js / vuetify 为 v-select 创建默认值

[英]Is there a way to create a default value for a v-select trough a function with vue.js / vuetify

I'm new to vuetify and I want to know if there is a way to assign a value for a v-select with a function.我是 vuetify 的新手,我想知道是否有办法使用 function 为 v-select 分配值。 I have an entire form that can create an enterprise.我有一个可以创建企业的完整表格。 I use apollo.js for the link with my database.我使用 apollo.js 作为与我的数据库的链接。 When I try to modify an enterprise all the fields fill correctly but I want the v-select to display the same value as the user set.当我尝试修改企业时,所有字段都正确填写,但我希望 v-select 显示与用户设置相同的值。 The code for my v-select in my "modify" page:我的“修改”页面中的 v-select 代码:

<v-select
 item-text="text"
 item-value="value"
 v-model="defaultSelected"
 :items="qualifications"
 menu-props="auto"
 :rules="SelectRules"
></v-select>

The data:数据:

export Default{
data: () => ({
defaultSelected{ value: "this is where I want to create a function to set a default value for the v-select" }
qualifications: [
 { text: "Blacklisted", value: "0"}
 { text: "Qualified", value: "1"}
 { text: "Non-qualified", value: "2"}
 { text: "High risk", value: "3"}
],
}),

I tried to do this but I generate many errors:我试图这样做,但我产生了很多错误:

export Default{
 data: () => ({
 defaultSelected{ value: renderQualificationValue }
 qualifications: [
  { text: "Blacklisted", value: "0"}
  { text: "Qualified", value: "1"}
  { text: "Non-qualified", value: "2"}
  { text: "High risk", value: "3"}
 ],
}),

methods: {

 renderQualificationValue(qualification)
  if (qualification === 0) return "0";
  else if (qualification === 1) return "1";
  else if (qualification === 2) return "2";
  else if (qualification === 3) return "3";
}

I don't know if I'm quite clear but in the end I just want to know if there is a way to assign or define a value with a function.我不知道我是否很清楚,但最后我只想知道是否有办法用 function 分配或定义一个值。

Any help is greatly appreciated, thanks!非常感谢任何帮助,谢谢!

First things first第一件事

defaultSelected{ value: renderQualificationValue } here you missed the: Should be defaultSelected: { value: renderQualificationValue } defaultSelected{ value: renderQualificationValue }这里你错过了: 应该是defaultSelected: { value: renderQualificationValue }

Second第二

What you're looking for is (literally) a computed property您正在寻找的是(字面意思)计算属性

so your code should look like所以你的代码应该看起来像

export Default {
  data: () => ({
    qualifications: [
      { text: "Blacklisted", value: "0" }
      { text: "Qualified", value: "1" }
      { text: "Non-qualified", value: "2" }
      { text: "High risk", value: "3" }
    ],
    qualification: "1"
  }),
  computed: {
    // Returns the Number type of qualification
    qualificationValue () {
      return Number.parseInt(this.qualification)
    }
  }
}
<v-select
 item-text="text"
 item-value="value"
 v-model="qualificationValue"
 :items="qualifications"
 menu-props="auto"
 :rules="SelectRules"
></v-select>

Third第三

Theres no point of using v-model there because it will just provide values as a getter, it will not be able to read data from v-select在那里使用 v-model 没有意义,因为它只会提供值作为 getter,它将无法从 v-select 读取数据

A slight different approach would be to split your computed property as Getter and Setter一种稍微不同的方法是将您的计算属性拆分为 Getter 和 Setter

//...
computed: {
    // Returns the Number type of qualification
    qualificationValue {
      get () {
        return Number.parseInt(this.qualification)
      },
      set (newValue) {
        // Here you choose what you want to do while setting
        // For example:
        this.qualification = newValue
      }
  }
//...

Fourth第四

I think it would be easier if you keep your variable as a number instead of parsing the value every time you read it.我认为如果将变量保留为数字而不是每次读取时都解析值会更容易。 So I would covert it on the Setter and not on the Getter, but that depends on your environment所以我会将它隐藏在 Setter 而不是 Getter 上,但这取决于你的环境

I would do it like this我会这样做

export Default {
  data: () => ({
    qualifications: [
      { text: "Blacklisted", value: "0" }
      { text: "Qualified", value: "1" }
      { text: "Non-qualified", value: "2" }
      { text: "High risk", value: "3" }
    ],
    qualification: 1
  }),
  qualificationValue {
      get () {
        return this.qualification
      },
      set (newValue) {
        this.qualification = Number.parseInt(newValue)
      }
  }
}

Note:笔记:

Computed getters cannot have input parameters, if you need that, you shall use a function instead.计算的 getter 不能有输入参数,如果你需要,你应该使用 function 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM