vue组件模板_Vue组件提供有关定制模板的建议

vue组件模板

v建议 (v-suggestions)

Automatic suggestions with custom templates.

具有自定义模板的自动建议。

安装 (Installation)

# npm
npm install v-suggestion

# yarn
yarn add v-suggestion
import Suggestions from 'v-suggestions'
import 'v-suggestions/dist/v-suggestions.css' // you can import the stylesheets also (optional)
Vue.use(Suggestions)

Component supports Vue 2.1.0+ version. v-suggetions uses slot-scope based templates for customizing suggestions.

组件支持Vue 2.1.0+版本。 v-suggetions使用基于插槽范围的模板来定制建议。

用户指南 (User Guide)

PropertyTypeDescription
v-modelStringan empty or predefined string as search query
onInputChangeFunctionWhen the search query is changed, this function will be trigerred. The function should return an array of objects for the Component to render. It can also return a Promise instead of a set of objects. AJAX calls or delays can be addressed.
onItemSelectedFunction (optional)When user selects (clicks or presses enter on an item), this function will be called
optionsObjectA set of options for customization of the component
options.debounceIntegerA delay in milliseconds between each "onInputChange" events. If unspecified, it will be ignored. Comes in handy for ajax requests. See examples.
options.placeholderstringA placeholder string for search (optional)
options.inputClassstringOverride classnames given to the input text element (optional)
属性 类型 描述
模型 空或预定义的字符串作为搜索查询
onInputChange 功能 更改搜索查询后,将触发此功能。 该函数应返回对象数组,以便Component呈现。 它还可以返回一个Promise而不是一组对象。 可以解决AJAX呼叫或延迟。
onItemSelected 功能(可选) 当用户选择(单击或按下项目上的Enter)时,将调用此功能
选项 目的 一组用于自定义组件的选项
options.debounce 整数 每个“ onInputChange”事件之间的延迟(以毫秒为单位)。 如果未指定,它将被忽略。 可用于处理ajax请求。 参见示例。
options.placeholder 用于搜索的占位符字符串(可选)
options.inputClass 覆盖输入文本元素的类名(可选)

简单的例子 (Simple Example)

<suggestions
    v-model="query"
    :options="options"
    :onInputChange="onCountryInputChange">
export default {
  data () {
    let countries = ['Afghanistan', 'Åland Islands', 'Albania', 'Algeria', 'American Samoa', 'AndorrA', 'Angola', 'Anguilla', 'Antarctica', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Aruba', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize']
    return {
      query: '',
      countries: countries,
      selectedCountry: null,
      options: {}
    }
  },
  methods: {
    onCountryInputChange (query) {
      if (query.trim().length === 0) {
        return null
      }
      // return the matching countries as an array
      return this.countries.filter((country) => {
        return country.toLowerCase().includes(query.toLowerCase())
      })
    },
    onCountrySelected (item) {
      this.selectedCountry = item
    },
    onSearchItemSelected (item) {
      this.selectedSearchItem = item
    }
  }
}

带有自定义模板的基于Ajax的结果(Duckduckgo API) (Ajax based results with custom template (Duckduckgo API))

<suggestions
  v-model="searchQuery"
  :options="searchOptions"
  :onItemSelected="onSearchItemSelected"
  :onInputChange="onInputChange">
  <div slot="item" slot-scope="props" class="single-item">
    <template v-if="props.item.Icon && props.item.Icon.URL">
      <div class="image-wrap" :style="{'backgroundImage': 'url('+ props.item.Icon.URL + ')' }"></div>
    </template>
    <span class="name">{{props.item.Text}}</span>
  </div>
</suggestions>
export default {
  data () {
    return {
      searchQuery: '',
      selectedSearchItem: null,
      options: {}
    }
  },
  methods: {
    onInputChange (query) {
      if (query.trim().length === 0) {
        return null
      }
      const url = `http://api.duckduckgo.com/?q=${query}&format=json&pretty=1`
      return new Promise(resolve => {
        axios.get(url).then(response => {
          const items = []
          response.data.RelatedTopics.forEach((item) => {
            if (item.Text) {
              items.push(item)
            } else if (item.Topics && item.Topics.length > 0) {
              item.Topics.forEach(topic => {
                items.push(topic)
              })
            }
          })
          resolve(items)
        })
      })
    },
    onSearchItemSelected (item) {
      this.selectedSearchItem = item
    }
  }
}

翻译自: https://vuejsexamples.com/vue-component-for-suggestions-with-custom-templates/

vue组件模板

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值