【问题标题】:vuetify autocomplete preloadvuetify 自动完成预加载
【发布时间】:2019-01-06 20:03:27
【问题描述】:

我正在学习 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>

【问题讨论】:

    标签: javascript vue.js vuejs2 vuetify.js


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,糟糕的是,我在这里发布之前更新了我的代码。我会更新问题,但还不够
    【解决方案2】:

    discrod 频道上的blalan05 给我回复:我创建的函数是乱码。

    new Vue({
      el: '#app',
      data: () => ({
        descriptionLimit: 60,
        entries: [],
        isLoading: false,
        model: null,
        search: "Cats"
      }),
     created () {
      this.model = {"API":"Cats","Description":"Pictures of cats from Tumblr","Auth":"","HTTPS":true,"Cors":"unknown","Link":"https://thecatapi.com/docs.html","Category":"Animals"},
       this.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 APIssss
          </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 repositoryyy</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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 2023-03-03
      • 2012-05-12
      • 2019-08-22
      • 1970-01-01
      • 2020-07-07
      • 2020-04-19
      相关资源
      最近更新 更多