File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
packages/taro-ui-docs/markdown Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 56
56
57
57
### 4. 使用 Taro UI
58
58
59
+ #### 通过 babel 插件按需引入组件
60
+
61
+ [ babel-plugin-import] ( https://github.com/ant-design/babel-plugin-import ) 是一款 babel 插件,它会在编译过程中将 import 语句自动转换为按需引入的方式。
62
+
63
+ ``` bash
64
+ # 安装插件
65
+ npm i babel-plugin-import -D
66
+ ```
67
+
68
+ 在.babelrc 或 babel.config.js 中添加配置:
69
+
70
+ ``` js
71
+ {
72
+ " plugins" : [
73
+ [
74
+ ' import' ,
75
+ {
76
+ libraryName: ' taro-ui' ,
77
+ customName : name => ` taro-ui/lib/components/${ name .split (' -' )[1 ]} ` ,
78
+ customStyleName : name => ` taro-ui/dist/style/components/${ name .split (' -' )[1 ]} .scss`
79
+ },
80
+ ' taro-ui'
81
+ ]
82
+ ]
83
+ }
84
+ ```
85
+
86
+ 接着你可以在代码中直接引入组件,插件会自动将代码转化为按需引入的形式。
87
+
88
+ ``` js
89
+ // 原始代码
90
+ import { AtButton } from ' taro-ui'
91
+
92
+ // 编译后代码
93
+ import AtButton from ' taro-ui/lib/components/button'
94
+ import ' taro-ui/dist/style/components/button.scss'
95
+ ```
96
+
59
97
#### 引入所需组件
60
98
61
99
在代码中 ` import ` 需要的组件并按照文档说明使用
You can’t perform that action at this time.
0 commit comments