@@ -122,6 +122,127 @@ const themeVars: ConfigProviderThemeVars = {
122122注意:ConfigProvider 仅影响它的子组件的样式,不影响全局 root 节点。
123123:::
124124
125+ ## 全局共享
126+
127+ > 需要配合虚拟根组件([ uni-ku-root] ( https://github.com/uni-ku/root ) ) 来做全局共享
128+
129+ ### 安装
130+
131+ ::: code-group
132+ ``` bash [npm]
133+ npm i -D @uni-ku/root
134+ ```
135+
136+ ``` bash [yarn]
137+ yarn add -D @uni-ku/root
138+ ```
139+
140+ ``` bash [pnpm]
141+ pnpm add -D @uni-ku/root
142+ ```
143+ :::
144+
145+ ### 引入
146+
147+ - CLI项目: 直接编辑 根目录下的 vite.config.(js|ts)
148+ - HBuilderX项目: 需要在根目录下 创建 vite.config.(js|ts)
149+
150+ ``` ts
151+ // vite.config.(js|ts)
152+
153+ import { defineConfig } from ' vite'
154+ import UniKuRoot from ' @uni-ku/root'
155+ import Uni from ' @dcloudio/vite-plugin-uni'
156+
157+ export default defineConfig ({
158+ plugins: [
159+ // ...plugins
160+ UniKuRoot (),
161+ Uni ()
162+ ]
163+ })
164+ ```
165+
166+ ::: tip
167+ 若存在改变 pages.json 的插件,需要将 UniKuRoot 放置其后
168+ :::
169+
170+ ### 使用
171+
172+ 1 . 创建根组件并处理全局配置组件
173+
174+ - CLI项目: 在 ** src** 目录下创建下 App.ku.vue
175+ - HBuilderX项目: 在 ** 根** 目录下创建 App.ku.vue
176+
177+ ::: tip
178+ 在 App.ku.vue 中标签 ` <KuRootView /> ` 代表指定视图存放位置
179+ :::
180+
181+ ``` vue
182+ <!-- src/App.ku.vue | App.ku.vue -->
183+
184+ <script setup lang="ts">
185+ import { useTheme } from './composables/useTheme'
186+
187+ const { theme, themeVars } = useTheme({
188+ buttonPrimaryBgColor: '#07c160',
189+ buttonPrimaryColor: '#07c160'
190+ })
191+ </script>
192+
193+ <template>
194+ <div>Hello AppKuVue</div>
195+ <!-- 假设已注册 WdConfigProvider 组件 -->
196+ <WdConfigProvider :theme="theme" :theme-vars="themeVars">
197+ <KuRootView />
198+ </WdConfigProvider>
199+ </template>
200+ ```
201+
202+ 2 . 编写控制主题组合式函数
203+
204+ ``` ts
205+ // src/composables/useTheme.ts
206+
207+ import type { ConfigProviderThemeVars } from ' wot-design-uni'
208+ import { ref } from ' vue'
209+
210+ const theme = ref <' light' | ' dark' >(false )
211+ const themeVars = ref <ConfigProviderThemeVars >()
212+
213+ export function useTheme(vars ? : ConfigProviderThemeVars ) {
214+ vars && (themeVars .value = vars )
215+
216+ function toggleTheme(mode ? : ' light' | ' dark' ) {
217+ theme .value = mode || (theme .value === ' light' ? ' dark' : ' light' )
218+ }
219+
220+ return {
221+ theme ,
222+ themeVars ,
223+ toggleTheme ,
224+ }
225+ }
226+ ```
227+
228+ 3 . 在任意视图文件中使用切换主题模式
229+
230+ ``` vue
231+ <!-- src/pages/*.vue -->
232+
233+ <script setup lang="ts">
234+ import { useTheme } from '@/composables/useTheme'
235+
236+ const { theme, toggleTheme } = useTheme()
237+ </script>
238+
239+ <template>
240+ <button @click="toggleTheme">
241+ 切换主题,当前模式:{{ theme }}
242+ </button>
243+ </template>
244+ ```
245+
125246## Attributes
126247
127248| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
0 commit comments