为什么我不能在我的Vuetify的v-autocomplete栏中输入任何内容?

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

Why can't I type anything in my v-autocomplete bar in Vuetify?

问题

这是您提供的代码片段的翻译:

我正在尝试创建一个获取用户列表的功能,该列表将依赖于用户的输入。为了实现这一目标,我正在使用 v-autocomplete。问题是,当我按照 vuetify 自动完成页面上的步骤来设置时,我的 v-autocomplete 输入框不允许我输入任何内容。

这是我正在使用的代码:

<v-autocomplete v-model="selectedUserId" :items="nonRegisteredSearchUsers" :search-input.sync="search2" hide-no-data hide-selected :item-text="item => item.firstName + ' ' + item.lastName" item-value="id" label="Users" placeholder="开始输入以搜索" filled> </v-autocomplete>

这是脚本的代码部分:

data() {
    return {
      searchUsers: [],
      selectedUserId: null,
      search2: null,
      userSearchDelay: 0
    }
  },
watch: {
    search2() {
      if (this.userSearchDelay)
        clearTimeout(this.userSearchDelay)
      this.userSearchDelay = setTimeout(this.getSearchUsers, 300)
    }
  },
computed: {
    nonRegisteredSearchUsers() {
      let userList = []
      for (let user of this.searchUsers) {
        if (!this.users.includes(user)) {
          userList.push(user)
        }
      }
      return userList
    }
  },
 methods: {
    getSearchUsers() {
      if (this.search2 != null) {
        apiService.getAdminViewUserIndex(this.search2).then(({ data }) => {
          this.searchUsers = data.users
        })
      }
    }
  }

我尝试删除 sync 以使其允许我再次输入。这样做后,我可以输入文字,但无法获取任何用户。我还尝试暂时删除 watch 部分,但仍无法输入。

英文:

I am trying to make a functionality of grabbing a list of users, and that list will depend on what the user types. To achieve that, I am using v-autocomplete. The problem is that when I follow the steps in the vuetify autocomplete page on how to do such a setup, my v-autocomplete bar won't let me type anything.

This is the code that I am using:

 &lt;v-autocomplete v-model=&quot;selectedUserId&quot; :items=&quot;nonRegisteredSearchUsers&quot; :search-input.sync=&quot;search2&quot; hide-no-data hide-selected :item-text=&quot;item =&gt; item.firstName + &#39;&#39; + item.lastName&quot; item-value=&quot;id&quot; label=&quot;Users&quot; placeholder=&quot;Start typing to search&quot; filled&gt; &lt;/v-autocomplete&gt;

This is what the script looks like:

data() {
    return {
      searchUsers: [],

      selectedUserId: null,
      search2: null,
      userSearchDelay: 0
    }
  },
watch: {
    search2() {
      if (this.userSearchDelay)
        clearTimeout(this.userSearchDelay)
      this.searchDelay = setTimeout(this.getSearchUsers, 300)
    }
  },
computed: {
    nonRegisteredSearchUsers() {
      let userList = []
      for (let user of this.searchUsers) {
        if (!this.users.includes(user)) {
          userList.push(user)
        }
      }
      return userList
    }
  },
 methods: {
    getSearchUsers() {
      if (this.search2 != null) {
        apiService.getAdminViewUserIndex(this.search2).then(({ data }) =&gt; {
          this.searchUsers = data.users
        })
      }
    }
  },

I tried to remove the sync in hopes of it letting me type again. It did, but I was not able to get any users back. I tried to remove the watch temporarily, but I was still not able to type.

答案1

得分: 0

问题出在:item-text中的内联函数上。看起来由于每次属性变化时都会创建新的函数,它会严重干扰v-autocomplete。

如果将该函数添加到组件的方法中,并将其放入属性中,它就会起作用。因此,在模板中,你应该使用存储在组件上的函数:

<v-autocomplete
  :item-text="printItem"
  ...

相应地,在组件中:

methods: {
  printItem(item){
    return item.firstName + ' ' + item.lastName
  },
}

看一下片段以了解如何操作。

英文:

The issue comes from the inline function in :item-text. Looks like since the function will be newly created every time a prop changes, it royally screws up the v-autocomplete.

If you add the function to the component methods and put that into the prop, it works. So in your template, you should use a function stored on the component:

&lt;v-autocomplete
  :item-text=&quot;printItem&quot;
  ...

and accordingly in the component:

methods: {
  printItem(item){
    return item.firstName + &#39; &#39; + item.lastName
  },
}

Have a look at the snippet to see how it

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

const users = Array.from({length: 5}, (_,i) =&gt; ({id: i, firstName: &#39;FN&#39;, lastName: &#39;User&#39;+i}))

new Vue({
  el: &#39;#app&#39;,
  vuetify: new Vuetify(),

  data() {
    return {
      searchUsers: [],

      selectedUserId: null,
      search2: null,
      userSearchDelay: 0
    }
  },
  watch: {
    search2() {
      if (this.userSearchDelay)
        clearTimeout(this.userSearchDelay)
      this.searchDelay = setTimeout(this.getSearchUsers, 300)
    }
  },
  computed: {
    nonRegisteredSearchUsers() {
      return users
    }
  },
  methods: {
    printItem(item){
      return item.firstName + &#39; &#39; + item.lastName
    },
    getSearchsearchUsersUsers() {
      if (this.search2 != null) {
        apiService.getAdminViewUserIndex(this.search2).then(({
          data
        }) =&gt; {
          this.searchUsers = data.users
        })
      }
    }
  },

})

<!-- language: lang-html -->

&lt;link href=&quot;https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900&quot; rel=&quot;stylesheet&quot;&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css&quot; rel=&quot;stylesheet&quot;&gt;

&lt;div id=&quot;app&quot;&gt;
  &lt;v-app&gt;
    &lt;v-main&gt;
      &lt;v-container&gt;
        &lt;v-autocomplete 
          v-model=&quot;selectedUserId&quot; 
          :items=&quot;nonRegisteredSearchUsers&quot; 
          :search-input.sync=&quot;search2&quot; 
          hide-no-data 
          hide-selected 
          :item-text=&quot;printItem&quot; 
          item-value=&quot;id&quot; 
          label=&quot;Users&quot; 
          placeholder=&quot;Start typing to search&quot;
          filled
        &gt;
        &lt;/v-autocomplete&gt;
          &lt;div&gt;
          selectedUserId: {{selectedUserId}} 
          &lt;/div&gt;&lt;div&gt;
          search2: {{search2}}
          &lt;/div&gt;
      &lt;/v-container&gt;
    &lt;/v-main&gt;
  &lt;/v-app&gt;
&lt;/div&gt;

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 05:05:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377263.html
匿名

发表评论

匿名网友

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

确定