Skip to content

快速上手

本节介绍如何在uni-app项目中安装、配置并使用 Eleagnt Wui Uni

使用之前

使用前,请确保你已经学习过 Vue 官方的 快速上手 和 uni-app 提供的 学习路线

安装

关于安装

Eleagnt Wui Uni提供了uni_modulesnpm两种安装方式,按需选择。

  • 使用uni_modules安装无需额外配置,即插即用,但是每次更新组件库需要处理代码差异(一般直接覆盖就可以)。
  • 使用npm安装需要额外配置,更新组件库时无需处理代码差异。

npm 安装

bash
npm i elegant-wui-uni
bash
yarn add elegant-wui-uni
bash
pnpm add elegant-wui-uni

uni_modules 安装

Eleagnt Wui Uni 支持 uni_modules 规范,已经上架到 uni-app 的插件市场。

uni-app插件市场选择使用HBuildX导入,或者选择手动在 src 目录下创建 uni_modules 文件夹并将Eleagnt Wui Uni解压到 uni_modules 中,结构如下:

- uni_modules
- - - elegant-wui-uni

下载地址:elegant-wui-uni

配置

导入组件

提醒

使用 uni_modules 安装时Eleagnt Wui Uni的组件天然支持easycom规范,无需额外配置就可以自动引入组件,而使用 npm安装 需按照此步骤配置,以下两种方案二选一即可。

配置 easycom 自动引入组件方案 1

传统 vue 组件,需要安装、引用、注册,三个步骤后才能使用组件。easycom将其精简为一步。
只要组件路径符合规范(具体见easycom),就可以不用引用、注册,直接在页面中使用。

提醒

  • uni-app 考虑到编译速度,直接在pages.json内修改easycom不会触发重新编译,需要改动页面内容触发。
  • 请确保您的 pages.json 中只有一个 easycom 字段,否则请自行合并多个引入规则。
JSON
// pages.json
{
	"easycom": {
		"autoscan": true,
		"custom": {
		  "^wui-(.*)": "elegant-wui-uni/components/wui-$1/wui-$1.vue"
		}
	},

	// 此为本身已有的内容
	"pages": [
		// ......
	]
}

基于 vite 配置自动引入组件方案 2

如果不熟悉easycom,也可以通过@uni-helper/vite-plugin-uni-components实现组件的自动引入。

提醒

  • 推荐使用@uni-helper/vite-plugin-uni-components@0.0.9及以上版本,因为在 0.0.9 版本开始其内置了elegant-wui-uniresolver
  • 如果使用此方案时控制台打印很多Sourcemap for points to missing source files​,可以尝试将 vite 版本升级至4.5.x以上版本。
bash
npm i @uni-helper/vite-plugin-uni-components -D
bash
yarn add @uni-helper/vite-plugin-uni-components -D
bash
pnpm add @uni-helper/vite-plugin-uni-components -D
ts
// vite.config.ts
import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'

import Components from '@uni-helper/vite-plugin-uni-components'
export default defineConfig({
  plugins: [Components(), uni()]
})

如果你使用 pnpm ,请在根目录下创建一个 .npmrc 文件,参见issue

// .npmrc
public-hoist-pattern[]=@vue*
// or
// shamefully-hoist = true

Volar 支持

如果您使用 Volar,请在 tsconfig.json 中通过 compilerOptions.type 指定全局组件类型。

TIP

cli 项目使用uni_modules安装无需配置,对Volar的支持自动生效,HbuildX项目不支持此配置,故此步骤仅在cli项目使用npm安装时需要配置。

json
// tsconfig.json
{
  "compilerOptions": {
    "types": ["elegant-wui-uni/global"]
  }
}

使用

Eleagnt Wui Uni安装、配置完成之后,支持组件自动引入,故可以直接在 SFC 中使用,无需在页面内 import,也不需要在 components 内声明,即可在任意页面使用。值得注意的是,uni-app平台不支持全局挂载组件,所以MessageToast等组件仍需在 SFC 中显式使用,例如:

html
<wui-toast></wui-toast>

Released under the MIT License.

Released under the MIT License.