javascript - 验证自动完成预加载

标签 javascript vue.js vuejs2 vuetify.js

我正在学习 vuetify 框架,我设法使用了大部分组件,但我遇到了自动完成组件的问题。 从头开始使用它效果很好,如果我在创建过程中尝试设置值,它根本不起作用。 我试图扩展其中一个 vuetify 示例,但没有成功。

我想在创建期间从 API 加载第一个值,但它保持为空。

感谢您的帮助。

new Vue({
  el: '#app',
  data: () => ({
    descriptionLimit: 60,
    entries: [],
    isLoading: false,
    model: null,
    search: "Cats"
  }),
 created :function() {
  model={"API":"Cats","Description":"Pictures of cats from Tumblr","Auth":"","HTTPS":true,"Cors":"unknown","Link":"https://thecatapi.com/docs.html","Category":"Animals"},
   search="Cats"
},
  computed: {
    fields () {
      if (!this.model) return []

      return Object.keys(this.model).map(key => {
        return {
          key: key,
          value: this.model[key] || 'n/a'
        }
      })
    },
    items () {
      return this.entries.map(entry => {
        const Description = entry.Description.length > this.descriptionLimit
          ? entry.Description.slice(0, this.descriptionLimit) + '...'
          : entry.Description

        return Object.assign({}, entry, { Description })
      })
    }
  },

  watch: {
    search (val) {
      // Items have already been loaded
      if (this.items.length > 0) return

      this.isLoading = true
      console.log("loadgin data")
      // Lazily load input items
      axios.get('https://api.publicapis.org/entries')
        .then(res => {
          const { count, entries } = res.data
          this.count = count
          this.entries = entries
        })
        .catch(err => {
          console.log(err)
        })
        .finally(() => (this.isLoading = false))
    }
  }
})
<div id="app">
  <v-app id="inspire">
    <v-card
      color="red lighten-2"
      dark
    >
      <v-card-title class="headline red lighten-3">
        Search for Public APIs
      </v-card-title>
      <v-card-text>
        Explore hundreds of free API's ready for consumption! For more information visit
        <a
          class="grey--text text--lighten-3"
          href="https://github.com/toddmotto/public-apis"
          target="_blank"
        >the Github repository</a>.
      </v-card-text>
      <v-card-text>
        <v-autocomplete
          v-model="model"
          :items="items"
          :loading="isLoading"
          :search-input.sync="search"
          color="white"
          hide-no-data
          hide-selected
          item-text="Description"
          item-value="API"
          label="Public APIs"
          placeholder="Start typing to Search"
          prepend-icon="mdi-database-search"
          return-object
        ></v-autocomplete>
      </v-card-text>
      <v-divider></v-divider>
      <v-expand-transition>
        <v-list v-if="model" class="red lighten-3">
          <v-list-tile
            v-for="(field, i) in fields"
            :key="i"
          >
            <v-list-tile-content>
              <v-list-tile-title v-text="field.value"></v-list-tile-title>
              <v-list-tile-sub-title v-text="field.key"></v-list-tile-sub-title>
            </v-list-tile-content>
          </v-list-tile>
        </v-list>
      </v-expand-transition>
      <v-card-actions>
        <v-spacer></v-spacer>
        <v-btn
          :disabled="!model"
          color="grey darken-3"
          @click="model = null"
        >
          Clear
          <v-icon right>mdi-close-circle</v-icon>
        </v-btn>
      </v-card-actions>
    </v-card>
  </v-app>
</div>

最佳答案

如果这是您尝试使用的代码,那么您在“模型”的末尾缺少一个“,”。

created :function() { 
  model={...}, <----
  search="Cats"
},

关于javascript - 验证自动完成预加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51596196/

相关文章:

javascript - 将视频添加到 Youtube 播放列表 NodeJS

vue.js - 如何使用路由器链接滚动到特定 anchor ?

angular - SPA 中的 Google DFP(Angular、Vue)——如何销毁广告及其所有引用以避免内存泄漏

javascript - 如何将变量从 twig 传递到 vueJS(symfony 2.8 和 VueJS 2)

javascript - vue 属性 "$v"中的 vuelidate 在渲染期间被访问但未在实例上定义

javascript - jQuery 的 .css() 方法实际上是 CSS 吗?

javascript - 滚动混合效果 - 除 Chrome 之外的所有浏览器中的性能问题

javascript - 如何在 Google 脚本中格式化从扫描仪收到的数据?

vue.js - Vue转换: v-on:before-leave hides element before callback executes

unit-testing - 使用 jest 和 avoriaz 时如何在 Vuejs 的组件中测试异步方法