|
2 | 2 |
|
3 | 3 | 基于本工程,您可以构建属于自己的跨技术栈/无框架 组件。 |
4 | 4 |
|
| 5 | +## 如何使用 |
5 | 6 |
|
6 | | -可以用下面命令生成本工程模版! |
| 7 | +``` |
| 8 | +npm install |
| 9 | +npm run dev |
| 10 | +``` |
| 11 | + |
| 12 | +入口文件为 `src/index.tsx`,这里使用 `vite` 进行开发和生产打包。 |
| 13 | + |
| 14 | +## 打包产物 |
7 | 15 |
|
8 | | -```bash |
9 | | -npm create quarkc@latest |
| 16 | +``` |
| 17 | +npm run build |
10 | 18 | ``` |
11 | 19 |
|
12 | | -文档见:https://quark-ecosystem.github.io/quarkc-docs/#/ |
| 20 | +打包后的产出为: `lib/index.js`和`lib/index.umd.cjs`。 |
13 | 21 |
|
14 | 22 |
|
| 23 | +## 使用产物 |
15 | 24 |
|
16 | | -## 如何使用 |
| 25 | +### 1、创建组件构建模版工程 |
17 | 26 |
|
| 27 | +创建模版 |
| 28 | +```bash |
| 29 | +npm create quarkc@latest |
18 | 30 | ``` |
| 31 | + |
| 32 | +启动工程 |
| 33 | +```bash |
19 | 34 | npm install |
20 | | -npm run dev |
| 35 | +npm start |
21 | 36 | ``` |
22 | 37 |
|
23 | | -入口文件为 `src/index.tsx`,这里使用 `vite` 进行开发和生产打包。 |
| 38 | +### 2、自定义你的 Custom Elements(组件/元素) |
| 39 | +```jsx |
| 40 | +import { QuarkElement, property, customElement } from "quarkc" |
| 41 | +import style from "./index.less?inline" |
| 42 | + |
| 43 | +@customElement({ tag: "my-element", style }) // 自定义标签/组件、CSS |
| 44 | +export default class MyElement extends QuarkElement { |
| 45 | + @property() // 外部属性 |
| 46 | + count |
| 47 | + |
| 48 | + add = () => { |
| 49 | + this.count += 1 |
| 50 | + } |
| 51 | + |
| 52 | + render() { |
| 53 | + return ( |
| 54 | + <button onClick={this.add}>count is: { this.count }</button> |
| 55 | + ) |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
24 | 59 |
|
25 | | -## 打包产物 |
| 60 | +### 3、Build 打包 |
26 | 61 |
|
27 | | -``` |
| 62 | +打包默认输出为 UMD / ESM 格式 |
| 63 | + |
| 64 | +```bash |
28 | 65 | npm run build |
29 | 66 | ``` |
30 | 67 |
|
31 | | -打包后的产出为: `lib/index.js`和`lib/index.umd.cjs`。 |
| 68 | +此时,构建产物 `lib/` 下的资源可以直接被任何框架的前端项目中使用。 |
32 | 69 |
|
33 | 70 | ```tree |
34 | | -. |
| 71 | +./lib |
35 | 72 | ├── types |
36 | 73 | | └── install.d.ts |
37 | 74 | ├── index.js |
38 | 75 | └── index.umd.js |
39 | 76 | ``` |
40 | 77 |
|
41 | | -## 使用产物 |
| 78 | +### 4、使用 |
42 | 79 |
|
43 | | -无论是`Vue`,`React`,`Angular`还是`Jq`项目,该组件都可以被使用。 |
| 80 | +##### (1)含有工程管理的前端项目(含有package.json/node_modules等文件) |
| 81 | +```jsx |
| 82 | +import "./lib/index.js" |
44 | 83 |
|
45 | | -```js |
46 | | -import "my-component/lib"; |
47 | 84 |
|
48 | | -<my-component count="0" />; |
| 85 | +<my-element count="count" /> |
| 86 | + |
| 87 | +// vue |
| 88 | +// <my-element :count="count" /> |
| 89 | + |
| 90 | +// react |
| 91 | +// <my-element count={count} /> |
| 92 | + |
| 93 | +// svelte |
| 94 | +// <my-element {count} /> |
| 95 | + |
| 96 | +// angular |
| 97 | +// <my-element [count]="count" /> |
| 98 | +``` |
| 99 | + |
| 100 | +##### (2)无工程管理的前端项目(不含有package.json/node_modules等文件,纯HTML+CSS+JS文件) |
| 101 | + |
| 102 | +单个 quarkc 组件,可以直接使用: |
| 103 | + |
| 104 | +```html |
| 105 | +<!DOCTYPE html> |
| 106 | +<html lang="en"> |
| 107 | + <head> |
| 108 | + <!-- 引用 npm run build 产物 --> |
| 109 | + <script type="module" src="./lib/index.mjs"></script> |
| 110 | + </head> |
| 111 | + <body> |
| 112 | + <my-element></my-element> |
| 113 | + </body> |
| 114 | +</html> |
49 | 115 | ``` |
50 | 116 |
|
51 | | -详细文档,请访问:https://quarkc.hellobike.com/#/zh-CN/docs/publishing |
| 117 | +多个 quarkc 组件同时加载,为了共用 quarkc 核心库,您可以选择开启了 `external`: |
| 118 | +```diff |
| 119 | +// vite.config.build.ts |
| 120 | +export default defineConfig({ |
| 121 | + build: { |
| 122 | + rollupOptions: { |
| 123 | ++ external: ['quarkc'], |
| 124 | + }, |
| 125 | + }, |
| 126 | +}); |
| 127 | + |
| 128 | +``` |
| 129 | +然后,用下面方式单独加载 `quarkc` 核心库: |
| 130 | +```html |
| 131 | +<!DOCTYPE html> |
| 132 | +<html lang="en"> |
| 133 | + <head> |
| 134 | + <script type="importmap"> |
| 135 | + { |
| 136 | + "imports": { |
| 137 | + "quarkc": "https://unpkg.com/quarkc@latest/lib/index.browser.js" |
| 138 | + } |
| 139 | + } |
| 140 | + </script> |
| 141 | + <!-- 引用 npm run build 产物 --> |
| 142 | + <!-- quarkc 构建的组件1 --> |
| 143 | + <script type="module" src="my-element1/lib/index.mjs"></script> |
| 144 | + <!-- quarkc 构建的组件2 --> |
| 145 | + <script type="module" src="my-element2/lib/index.mjs"></script> |
| 146 | + </head> |
| 147 | + <body> |
| 148 | + <!-- 使用 quarkc 元素/组件 --> |
| 149 | + <my-element1></my-element1> |
| 150 | + <my-element2></my-element2> |
| 151 | + </body> |
| 152 | +</html> |
| 153 | +``` |
| 154 | + |
| 155 | + |
| 156 | +## 文档 |
| 157 | + |
| 158 | +完整文档,请访问 [https://quark-ecosystem.github.io/quarkc-docs](https://quark-ecosystem.github.io/quarkc-docs) |
| 159 | + |
| 160 | +### 联系我们 |
| 161 | + |
| 162 | +添加微信:Sanqi9675 |
| 163 | + |
| 164 | +### 社区示例 |
| 165 | + |
| 166 | +| 作者 | github 地址 | 截图 / 链接 |
| 167 | +| ---- | ---- | ----- | |
| 168 | +| @xsf0105 | https://github.com/xsf0105/dark-light-element | https://unpkg.com/dark-light-element@latest/demo.html | |
| 169 | +| @hellof2e | https://github.com/hellof2e/quark-doc-header |  https://quarkc.hellobike.com/#/ | |
| 170 | +| @yuhaiyang1 | https://github.com/yuhaiyang1/quarkc-time | https://unpkg.com/quark-timer@0.0.2/demo.html | |
| 171 | +| @dyf19118 | https://github.com/dyf19118/quark-ui-rate |  | |
| 172 | +| @hellof2e | https://github.com/hellof2e/quark-doc-home |  | |
| 173 | +| @zhangfisher | https://github.com/zhangfisher/lite-tree/tree/master/packages/quark | [点击查看](https://github.com/zhangfisher/lite-tree/blob/master/docs/tree.png?raw=true) | |
| 174 | + |
0 commit comments