问题

elementui改为按需引入后出现报错,报错信息Uncaught ReferenceError: _MessageBox is not defined

操作

  • npm i -D babel-plugin-component
    
  • babel.config.js

    module.exports = {
      presets: [
        '@vue/app'
      ],
      plugins: [
        [
          "component",
          {
            "libraryName": "element-ui",
            "styleLibraryName": "theme-chalk"
          },
        ]
      ]
    }
    
  • element.js

import Vue from 'vue'
import {
  Badge,
  Switch,
  Button,
  Select,
  Input,
  InputNumber,
  Radio,
  Menu,
  Upload,
  Form,
  FormItem,
  Tabs,
  Checkbox,
  Container,
  Aside,
  Dropdown,
  DropdownMenu,
  DropdownItem,
  Header,
  Main,
  TabPane,
  RadioGroup,
  RadioButton,
  Row,
  DatePicker,
  Col,
  Table,
  TableColumn,
  MessageBox,
  Message,
  Popover,
  Breadcrumb,
  BreadcrumbItem,
  Autocomplete,
  Option,
  TimeSelect,
  CheckboxGroup,
  Pagination,
  Tag,
  Collapse,
  CollapseItem,
  Loading,
  Timeline,
  TimelineItem,
  Dialog,
  Tooltip,
  Tree,
  Divider
} from 'element-ui'

Vue.use(Switch)
Vue.use(Divider)
Vue.use(Badge)
Vue.use(Timeline)
Vue.use(TimelineItem)
Vue.use(Loading)
Vue.use(CollapseItem)
Vue.use(Collapse)
Vue.use(Tag)
Vue.use(Pagination)
Vue.use(CheckboxGroup)
Vue.use(TimeSelect)
Vue.use(Option)
Vue.use(Autocomplete)
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Popover)
Vue.use(TableColumn)
Vue.use(Table)
Vue.use(Col)
Vue.use(RadioGroup)
Vue.use(RadioButton)
Vue.use(Row)
Vue.use(Upload)
Vue.use(DatePicker)
Vue.use(TabPane)
Vue.use(Container)
Vue.use(Aside)
Vue.use(Dropdown)
Vue.use(DropdownMenu)
Vue.use(DropdownItem)
Vue.use(Tooltip)
Vue.use(Header)
Vue.use(Main)
Vue.use(Button)
Vue.use(Select)
Vue.use(Input)
Vue.use(InputNumber)
Vue.use(Radio)
Vue.use(Menu)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Tabs)
Vue.use(Checkbox)
Vue.use(Dialog)
Vue.use(Tree)

Vue.prototype.$msgbox = MessageBox
Vue.prototype.$alert = MessageBox.alert
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$prompt = MessageBox.prompt
Vue.prototype.$message = Message

解决

...
const msgbox = MessageBox
const { alert, confirm } = msgbox

Vue.prototype.$msgbox = msgbox
Vue.prototype.$alert = alert
Vue.prototype.$confirm = confirm
Vue.prototype.$message = Message
参考链接:

https://github.com/ElementUI/babel-plugin-component/issues/31

Logo

前往低代码交流专区

更多推荐