了解如何在您的项目中使用 Webpack 使用 Bootstrap Table Vue 组件。

导入 JavaScript

通过在应用程序的入口点(通常是 index.jsapp.js)添加以下行来导入 Bootstrap Table 的 JavaScript:

import 'bootstrap-table'

当然,您还可以导入所需的主题、语言环境或扩展:

// 导入主题
import 'bootstrap-table/dist/themes/materialize/bootstrap-table-materialize.min.js'

// 导入语言环境
import 'bootstrap-table/dist/locale/bootstrap-table-zh-CN.min.js'

// 导入扩展和依赖项
import 'tableexport.jquery.plugin'
import 'bootstrap-table/dist/extensions/export/bootstrap-table-export.min.js'

默认情况下,Bootstrap Table 依赖于 jQueryBootstrapPopper,这些被定义为 peerDependencies,这意味着您需要确保使用 npm install --save jquery bootstrap popper.js 将它们添加到您的 package.json 中。

导入 CSS

通过在应用程序的入口点添加以下行来导入 Bootstrap Table 的 CSS:

import 'bootstrap-table/dist/bootstrap-table.min.css'

当然,您还可以导入所需的主题或扩展:

// 导入主题
import 'bootstrap-table/dist/themes/materialize/bootstrap-table-materialize.min.css'

// 导入扩展
import 'bootstrap-table/dist/extensions/fixed-columns/bootstrap-table-fixed-columns.min.css'

使用方法

<template>
  <BootstrapTable
    :columns="columns"
    :data="data"
    :options="options"
  />
</template>

<script>
import BootstrapTable from 'bootstrap-table/dist/bootstrap-table-vue.js'

export default {
  components: {
    BootstrapTable
  },
  data () {
    return {
      columns: [
        {
          title: '项目 ID',
          field: 'id'
        },
        {
          field: 'name',
          title: '项目名称'
        },
        {
          field: 'price',
          title: '项目价格'
        }
      ],
      data: [
        {
          id: 1,
          name: '项目 1',
          price: '$1'
        }
      ],
      options: {
        search: true,
        showColumns: true
      }
    }
  }
}
</script>

起始模板

在 bootstrap-table-example 项目中有一个 vue-starter 示例。

plugins/jquery.js

import jQuery from 'jquery'
window.jQuery = jQuery

plugins/table.js

import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap-table/dist/bootstrap-table.min.css'

import './jquery.js'
import Vue from 'vue'
import 'bootstrap'
import 'bootstrap-table/dist/bootstrap-table.js'
import BootstrapTable from 'bootstrap-table/dist/bootstrap-table-vue.esm.js'

Vue.component('BootstrapTable', BootstrapTable)

main.js

import './plugins/table.js'

View.vue

<template>
  <BootstrapTable :columns="columns" :data="data" :options="options"></BootstrapTable>
</template>

<script>
export default {
  data () {
    return {
      columns: [
        {
          title: '项目 ID',
          field: 'id'
        },
        {
          field: 'name',
          title: '项目名称'
        }, {
          field: 'price',
          title: '项目价格'
        }
      ],
      data: [
        {
          id: 1,
          name: '项目 1',
          price: '$1'
        },
        {
          id: 2,
          name: '项目 2',
          price: '$2'
        },
        {
          id: 3,
          name: '项目 3',
          price: '$3'
        },
        {
          id: 4,
          name: '项目 4',
          price: '$4'
        },
        {
          id: 5,
          name: '项目 5',
          price: '$5'
        }
      ],
      options: {
        search: true,
        showColumns: true
      }
    }
  }
}
</script>