Skip to content
On this page

快速上手

安装

js
yarn add element-next
# 或者
npm i element-next
# 或者
pnpm add element-next

完整引入

js
import { createApp } from 'vue'
import App from './App.vue'

import { ElButton } from 'element-plus'
import 'element-plus/dist/index.css'

import ElementNext from 'element-next'
import 'element-next/lib/styles/index.css'

const app = createApp(App)

app.use(ElButton)
app.use(ElementNext)
app.mount('#app')

按需引入

提示

因为组件是动态构建,所有 element-plus 依赖不能动态引入,必须是全量或者按需引入。包括element-next的时候也是一样。n-from组件的 item 项如果使用到了内置的拓展组件(n-tip,n-date-pricker .....) 都需要手工引入。

组件级注册

js
import { NSelect, NDatePricker } from 'element-next'
import 'element-plus/dist/index.css'
app.use(NSelect)
app.use(NDatePricker)

Css 按需引入

js
import { NForm } from 'element-pro'
import 'element-pro/lib/styles/form'

使用 unplugin-vue-components

安装及使用查看 unplugin-vue-components

  • 配置信息
js
{
  resolvers: [
    (name) => {
      if (name.startsWith('N')) {
        const fileName = name.slice(1).replace(/\B([A-Z])/g, '-$1').toLocaleLowerCase()
        return {
          importName: name,
          path: 'element-next',
          sideEffects: `element-next/lib/styles/${fileName}`
        }
      }
    }
  ],
}

在 vite 中使用 vite-plugin-style-import

安装及使用查看 vite-plugin-style-import

  • 修改配置 vite.config
js
import styleImport from 'vite-plugin-style-import'

export default {
  plugins: [
    styleImport({
      libs: [
        {
          importTest: /^N/,
          libraryName: 'element-next',
          esModule: true,
          ensureStyleFile: true,
          resolveStyle: (name) => {
            return `element-next/lib/styles/${name.slice(2)}`
          },
        },
      ],
    }),
  ],
}

全局配置

NameDescriptionTypeOptionsDefault
Pagination分页全局配置Pagination--
action操作按钮配置ActionOptions--
form表单全局配置FormGlobalConfig--
table表格全局配置TableGlobalConfig--
axiosInstanceaxios 实例AxiosInstance--
tinymce富文本全局配置TinymceConfig--
dict远端基础数据请求配置DictConfig--

Pagination

支持 element-plus Pagination 分页属性

NameDescriptionTypeOptionsDefault
background是否为分页按钮添加背景色boolean-true
layout组件布局,子组件名用逗号分隔string-total, sizes, prev, pager, next

ActionOptions

NameDescriptionTypeOptionsDefault
addProps表格表单新增按钮属性object--
submitProps表单提交按钮属性object--
resetProps表单提交按钮重置按钮object--

TableGlobalConfig

NameDescriptionTypeOptionsDefault
outsideHeight用于计算表格高度,详见ProTableFormoutsideHeight属性number-0
actionLimit列表按钮显示个数,详见ProTableFormactionLimit属性number-4
columns表格列操作配置object--

FormGlobalConfig

NameDescriptionTypeOptionsDefault
limit最多显示多少个field项,showMore=true的时候有效number4
highlightError表单验证不通过的时候输入框是否高亮boolean--
highlightRequired表单必填项输入框是否高亮boolean--

DictConfig

NameDescriptionTypeOptionsDefault
url请求路径string--
method请求方法stringpost,get'get'
cache是否开启缓存boolean-true
cache-key缓存的键,这里相当于是数据表的名称。具体的数据键值由参数组成string-_dict_
storage-type缓存类型sessionStorage,localStorage-localStorage
expires过期时间,默认单位:天number-7

TinymceConfig

如果下载的依赖包放到项目的public/resources下则无需配置,但是要注意不要更改下载下来的包文件目录位置。下载 Tinymce

NameDescriptionTypeOptionsDefault
tinymceJsUrltinymce.min.js 路径string-/resources/tinymce/tinymce.min.js

默认配置

json
{
  dict: {
    url: '',
    method: 'get',
    cache: true,
    cacheKey: '_dict_',
    storageType: 'localStorage',
    expires: 7,
  },
  table: {
    outsideHeight: 0,
    actionLimit: 4,
    columns: {
      saveUrl: '',
      listUrl: '',
      saveMethod: 'post',
      listMethod: 'post',
      requestConfig: undefined,
      cacheKey: '_table_columns_',
      storageType: 'localStorage',
      expires: 7,
      cache: true,
    },
  },
  form: {
    highlightError: true,
    highlightRequired: true,
    limit: 4,
  },
  pagination: {
    pageSize: 20,
    pageSizes: [20, 50, 100, 150, 200],
    background: true,
    layout: 'total, sizes, prev, pager, next',
  },
  action: {
    addProps: { type: 'primary', icon: markRaw(Plus) },
    submitProps: { type: 'primary' },
    searchProps: { type: 'primary', icon: markRaw(Search) },
    resetProps: { type: 'info' },
  },
  tinymce: {
    tinymceJsUrl: '/resources/tinymce/tinymce.min.js',
  },
}

配置

全局引入

js
app.use(ElementNext, {
  pagination: {
    small: true,
    hideOnSinglePage: true,
    layout: 'prev, pager, next',
  },
})

按需引入全局配置

通过导出的setGlobalConfig函数设置。注意它不是响应式的

tsx
import { setGlobalConfig } from 'element-next'
setGlobalConfig({
  dict: {
    url: '/sysDict/getByType',
  },
  axiosInstance: axios,
})

提示

文档示例基于 组合式 API 语法,如果不熟悉语法请前往官方文档查看

如果使用 VS Code 开发,配合 Vetur 使用提供完整的组件、属性、事件补全。例如:输入 <n- 将罗列出所有组件库组件

对于使用 VS Code 配合 typescript 开发,推荐使用插件 Volar。只需要中向 tsconfig.json 文件中的 include 字段增加

diff
{
  "include": [
    "node_modules/element-next/types/components.d.ts"
  ]
}

粤ICP备2022017444号