温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Nuxt如何配置Element-UI按需引入

发布时间:2020-07-07 11:30:57 来源:亿速云 阅读:413 作者:清晨 栏目:开发技术

这篇文章主要介绍Nuxt如何配置Element-UI按需引入,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

Nuxt 使用 create-nuxt-app 创建项目时,选择使用 Element-UI 为默认组件库,发现 Nuxt 没有开启 Element-UI 的按需引入配置,需要自行配置。

安装依赖

在 create-nuxt-app 中没有选择 Element-UI 的先安装。

npm install element-ui --save

或者

yarn add element-ui

Element-UI 开启按需引入,必须安装 babel-plugin-component 插件。

npm install babel-plugin-component --save-dev

或者

yarn add babel-plugin-component

安装完成后,在文件根目录创建(或已经存在) plugins/ 目录下创建相应的插件文件,创建名为:element-ui.js 的文件。

// element-ui.js
import Vue from 'vue'
import {
 Container,
 Header,
 Aside,
 Main,
 Menu,
 MenuItem,
 Button,
 Form,
 FormItem,
 Input
} from 'element-ui'
import locale from 'element-ui/lib/locale/lang/en'

const components = [
 Container,
 Header,
 Aside,
 Main,
 Menu,
 MenuItem,
 Button,
 Form,
 FormItem,
 Input
];

const Element = {
 install (Vue) {
 components.forEach(component => {
  Vue.component(component.name, component)
 })
 }
}

Vue.use(Element, { locale })

配置 plugins 选项

在 nuxt.config.js 文件中,配置 plugins 选项。

module.exports = {
 /*
 ** Plugins to load before mounting the App
 ** https://nuxtjs.org/guide/plugins
 */
 plugins: ["@/plugins/element-ui"],
}

Nuxt 默认为开启 SSR,采用服务端渲染,也可以手动配置关闭 SSR,配置为:

module.exports = {
 /*
 ** Plugins to load before mounting the App
 ** https://nuxtjs.org/guide/plugins
 */
 plugins: [
 {
  src: "@/plugins/element-ui",
  ssr: false // 关闭ssr
 }
 ],
}

如果在 create-nuxt-app 中默认选了 Element-UI 的,还需要将 Element-UI 的全局样式去掉,即在 nuxt.config.js 中:

module.exports = {
 /*
 ** Global CSS
 */
 css: ['element-ui/lib/theme-chalk/index.css'],
}

删除 'element-ui/lib/theme-chalk/index.css' 作为全局样式的打包配置,改为

module.exports = {
 /*
 ** Global CSS
 */
 css: [],
}

配置 babel 选项

在 nuxt.config.js 文件中,在选项 build 中配置 babel 选项:

module.exports = {
 /*
 ** Build configuration
 ** See https://nuxtjs.org/api/configuration-build/
 */
 build: {
 babel: {
  "plugins": [
  [
   "component",
   {
   "libraryName": "element-ui",
   "styleLibraryName": "theme-chalk"
   }
  ]
  ]
 }
 }
}

到此,Element-UI 按需引入配置完成。

以上是Nuxt如何配置Element-UI按需引入的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI