设置 v-select 的默认值,使用条件语句。

huangapple go评论79阅读模式
英文:

Setting v-select default value with if conditions

问题

I have a dropdown menu where I want the default value to be set based on the login usertype.

我有一个下拉菜单,我希望默认值根据登录用户类型进行设置。

I have the this page to appear after login, and I want the v-select option choice to be chosen according to the usertype condition and store the select value in localStorage.

登录后我有这个页面显示,我希望根据用户类型条件选择v-select选项,并将选择的值存储在localStorage中。

I put the if conditions in method, but the v-select default value still is shown as null. How can I pass and store the default value based on the usertype conditions when login and refresh.

我将条件放在方法中,但是v-select的默认值仍然显示为null。当登录和刷新时,如何根据用户类型条件传递和存储默认值呢?

#vue 2.6 vuetify 2.3
<template>
  <h1>title</h1>
  <v-select 
    :items="items"
    v-model="select"
  ></v-select>
</template>

<script>
export default {
  data() {
    return {
      items: ['A','B'],
      select: localStorage.getItem('select'),
    };
  },
  watch: {
    select(value) {
      localStorage.setItem('select', value);
      console.log(localStorage.getItem('select'));
    },
  },
  mounted() {
    this.selectChoice();
  },
  methods: {
    selectChoice() {
      if (this.users.types >= 0) {
         this.select = 'A';
      }
      else if (this.users.types >= 2) {
         this.select = 'B';
      }
    },
  },
};
</script>
英文:

I have a dropdown menu where I want the default value to be set based on the login usertype.

I have the this page to appear after login, and I want the v-select option choice to be chosen according to the usertype condition and store the select value in localStorage. I put the if conditions in method, but the v-select default value still is shown as null. How can I pass and store the default value based on the usertype conditions when login and refresh.

#vue 2.6 vuetify 2.3
&lt;template&gt;
  &lt;h1&gt;title&lt;/h1&gt;
  &lt;v-select 
    :items:&quot;items&quot;
    v-model:&quot;select&quot;
  &gt;&lt;/v-select&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  data() {
    return {
      items: [&#39;A&#39;,&#39;B&#39;],
      select: localStorage.getItem(&#39;select&#39;),
    };
  },
  watch: {
    selected(value) {
      localStorage.setItem(&#39;select&#39;, value);
      console.log(localStorage.getItem(&#39;select&#39;));
    },
  },
  mounted() {
    this.selectChoice();
  },
  methods: {
    selectChoice() {
      if (this.users.types&gt;=0) {
         this.select = &#39;A&#39;;
      }
      else if (this.users.types&gt;=2) {
         this.select = &#39;B&#39;;
      }

    },
  },
};
&lt;/script&gt;

答案1

得分: 1

这是错误的

<v-select 
  :items:"items"
  v-model:"select"
></v-select>

应该是

<v-select 
  :items="items"
  v-model="select"
></v-select>

还有

watch: {
    select(value) {  // 而不是 selected
      localStorage.setItem('select', value);
      console.log(localStorage.getItem('select'));
    },
}

另外,this.users 未定义,应该引发 JavaScript 异常。

英文:

This is wrong

  &lt;v-select 
    :items:&quot;items&quot;
    v-model:&quot;select&quot;
  &gt;&lt;/v-select&gt;

should be

  &lt;v-select 
    :items=&quot;items&quot;
    v-model=&quot;select&quot;
  &gt;&lt;/v-select&gt;

and

watch: {
    select(value) {  // instead of selected
      localStorage.setItem(&#39;select&#39;, value);
      console.log(localStorage.getItem(&#39;select&#39;));
    },
}

Also, this.users is not defined and should throw an js exception.

huangapple
  • 本文由 发表于 2023年5月11日 00:23:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定