mirror of
https://github.com/jekip/naive-ui-admin.git
synced 2026-03-01 00:23:11 +08:00
perf: 单独拆分 naive 全局 API 挂载方法
This commit is contained in:
16
src/main.ts
16
src/main.ts
@@ -1,6 +1,6 @@
|
|||||||
import './styles/tailwind.css';
|
import './styles/tailwind.css';
|
||||||
import { createApp } from 'vue';
|
import { createApp } from 'vue';
|
||||||
import { setupNaive, setupDirectives } from '@/plugins';
|
import { setupNaiveDiscreteApi, setupNaive, setupDirectives } from '@/plugins';
|
||||||
import App from './App.vue';
|
import App from './App.vue';
|
||||||
import router, { setupRouter } from './router';
|
import router, { setupRouter } from './router';
|
||||||
import { setupStore } from '@/store';
|
import { setupStore } from '@/store';
|
||||||
@@ -8,9 +8,15 @@ import { setupStore } from '@/store';
|
|||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = createApp(App);
|
const app = createApp(App);
|
||||||
|
|
||||||
|
// 挂载状态管理
|
||||||
|
setupStore(app);
|
||||||
|
|
||||||
// 注册全局常用的 naive-ui 组件
|
// 注册全局常用的 naive-ui 组件
|
||||||
setupNaive(app);
|
setupNaive(app);
|
||||||
|
|
||||||
|
// 挂载 naive-ui 脱离上下文的 Api
|
||||||
|
setupNaiveDiscreteApi();
|
||||||
|
|
||||||
// 注册全局自定义组件
|
// 注册全局自定义组件
|
||||||
//setupCustomComponents();
|
//setupCustomComponents();
|
||||||
|
|
||||||
@@ -20,13 +26,11 @@ async function bootstrap() {
|
|||||||
// 注册全局方法,如:app.config.globalProperties.$message = message
|
// 注册全局方法,如:app.config.globalProperties.$message = message
|
||||||
//setupGlobalMethods(app);
|
//setupGlobalMethods(app);
|
||||||
|
|
||||||
// 挂载状态管理
|
|
||||||
setupStore(app);
|
|
||||||
|
|
||||||
// 挂载路由
|
// 挂载路由
|
||||||
await setupRouter(app);
|
setupRouter(app);
|
||||||
|
|
||||||
// 路由准备就绪后挂载APP实例
|
// 路由准备就绪后挂载 APP 实例
|
||||||
|
// https://router.vuejs.org/api/interfaces/router.html#isready
|
||||||
await router.isReady();
|
await router.isReady();
|
||||||
|
|
||||||
app.mount('#app', true);
|
app.mount('#app', true);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
export { setupNaive } from '@/plugins/naive';
|
export { setupNaive } from '@/plugins/naive';
|
||||||
|
export { setupNaiveDiscreteApi } from '@/plugins/naiveDiscreteApi';
|
||||||
export { setupDirectives } from '@/plugins/directives';
|
export { setupDirectives } from '@/plugins/directives';
|
||||||
export { setupCustomComponents } from '@/plugins/customComponents';
|
export { setupCustomComponents } from '@/plugins/customComponents';
|
||||||
export { setupGlobalMethods } from '@/plugins/globalMethods';
|
export { setupGlobalMethods } from '@/plugins/globalMethods';
|
||||||
|
|||||||
@@ -1,35 +1,5 @@
|
|||||||
import type { App } from 'vue';
|
import type { App } from 'vue';
|
||||||
import * as NaiveUI from 'naive-ui';
|
import * as NaiveUI from 'naive-ui';
|
||||||
import { computed } from 'vue';
|
|
||||||
import { useDesignSettingStore } from '@/store/modules/designSetting';
|
|
||||||
import { store } from '@/store';
|
|
||||||
import { lighten } from '@/utils/index';
|
|
||||||
|
|
||||||
// NaiveUI 全局方法注册
|
|
||||||
const designStore = useDesignSettingStore(store);
|
|
||||||
const configProviderPropsRef = computed(() => ({
|
|
||||||
theme: designStore.darkTheme ? NaiveUI.darkTheme : undefined,
|
|
||||||
themeOverrides: {
|
|
||||||
common: {
|
|
||||||
primaryColor: designStore.appTheme,
|
|
||||||
primaryColorHover: lighten(designStore.appTheme, 6),
|
|
||||||
primaryColorPressed: lighten(designStore.appTheme, 6),
|
|
||||||
},
|
|
||||||
LoadingBar: {
|
|
||||||
colorLoading: designStore.appTheme,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
const { message, dialog, notification, loadingBar } = NaiveUI.createDiscreteApi(
|
|
||||||
['message', 'dialog', 'notification', 'loadingBar'],
|
|
||||||
{
|
|
||||||
configProviderProps: configProviderPropsRef,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
window['$message'] = message;
|
|
||||||
window['$dialog'] = dialog;
|
|
||||||
window['$notification'] = notification;
|
|
||||||
window['$loading'] = loadingBar;
|
|
||||||
|
|
||||||
const naive = NaiveUI.create({
|
const naive = NaiveUI.create({
|
||||||
components: [
|
components: [
|
||||||
|
|||||||
39
src/plugins/naiveDiscreteApi.ts
Normal file
39
src/plugins/naiveDiscreteApi.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import * as NaiveUI from 'naive-ui';
|
||||||
|
import { computed } from 'vue';
|
||||||
|
import { useDesignSettingWithOut } from '@/store/modules/designSetting';
|
||||||
|
import { lighten } from '@/utils/index';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 挂载 Naive-ui 脱离上下文的 API
|
||||||
|
* 如果你想在 setup 外使用 useDialog、useMessage、useNotification、useLoadingBar,可以通过 createDiscreteApi 来构建对应的 API。
|
||||||
|
* https://www.naiveui.com/zh-CN/dark/components/discrete
|
||||||
|
*/
|
||||||
|
|
||||||
|
export function setupNaiveDiscreteApi() {
|
||||||
|
const designStore = useDesignSettingWithOut();
|
||||||
|
|
||||||
|
const configProviderPropsRef = computed(() => ({
|
||||||
|
theme: designStore.darkTheme ? NaiveUI.darkTheme : undefined,
|
||||||
|
themeOverrides: {
|
||||||
|
common: {
|
||||||
|
primaryColor: designStore.appTheme,
|
||||||
|
primaryColorHover: lighten(designStore.appTheme, 6),
|
||||||
|
primaryColorPressed: lighten(designStore.appTheme, 6),
|
||||||
|
},
|
||||||
|
LoadingBar: {
|
||||||
|
colorLoading: designStore.appTheme,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
const { message, dialog, notification, loadingBar } = NaiveUI.createDiscreteApi(
|
||||||
|
['message', 'dialog', 'notification', 'loadingBar'],
|
||||||
|
{
|
||||||
|
configProviderProps: configProviderPropsRef,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
window['$message'] = message;
|
||||||
|
window['$dialog'] = dialog;
|
||||||
|
window['$notification'] = notification;
|
||||||
|
window['$loading'] = loadingBar;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user