1. Introduction

The new front-end framework of svelte.js is very popular, but there are relatively few component libraries and back-end systems developed by svelte.js on the Internet. So I developed a svelte-ui component library and svelte-admin background management system by myself.

Svelte Ui Admin is a middle and background management based on svelte3.x+svelteKit+svelte-ui+sass+mockjs and other technologies.

2. Technical realization

  • Development tools: Vscode
  • Technical framework: svelte3.x+svelteKit+vite3
  • UI component library: svelte-ui (custom pc-side UI component library based on svelte)
  • Chart component: echarts^5.3.3
  • Graphic editor: wangeditor^4.7.15
  • Internationalization scheme: svelte-i18n^3.4.0
  • Data mock: mockjs^1.1.0

svelte-admin Support dynamic skinning, Chinese/English/Traditional international languages.

Directory Structure

Svelte-Ui component library

All components in the project follow the svelte-ui component library. The overall style is unified and beautiful.

svelte-ui desktop pc UI component library

public layout template

svelte-admin The whole is divided into three modules: top navigation bar + left dynamic routing menu + right main content area.

image.png

 <div class="svadmin__container" style="--themeSkin: {$skin}">
    <div class="svadmin__wrapper-layout flexbox flex-col">
        <div class="sv__layout-header">
            <Header />
        </div>
        
        <div class="sv__layout-body flex1 flexbox">
            <!-- //侧边栏 -->
            {#if rootRouteEnable}
            <div class="sv__bd-sidebar">
                <SideMenu routes={mainRoutes} {activeRoute} />
            </div>
            {/if}

            {#if (rootRouteEnable && route != '/') || !rootRouteEnable}
            <div class="sv__bd-menus" class:collapsed={collapsed&&!rootRouteEnable}>
                <RouteMenu
                    routes={getAllRoutes}
                    {activeRoute}
                    {activeRootRoute}
                    {rootRouteEnable}
                    {collapsed}
                />
            </div>
            {/if}

            <div class="sv__bd-main flex1 flexbox flex-col">
                <!-- 面包屑导航 -->
                <BreadCrumb routes={getAllRoutes} {activeRoute} {activeRootRoute} />
                
                <!-- 主内容区 -->
                <Scrollbar autohide gap={2}>
                    <div class="sv__main-wrapper">
                        <slot />
                    </div>
                </Scrollbar>
            </div>
        </div>
    </div>
</div>

+error.svelte error handling

 <script>
    import { page } from '$app/stores'
    import { goto } from '$app/navigation'
    import { Button } from '$lib/svelte-ui'

    function goHome() {
        goto('/home/index')
    }
</script>

<svelte:head>
    <title>{$page.status} Error!</title>
</svelte:head>

<div class="svadmin__pageErr flexbox flex-col flex-alignc flex-justifyc">
    <div class="svadmin__pageErr-img"><i class="sv-icon-round_close_fill_light"></i></div>
    <div class="svadmin__pageErr-content">
        <div class="c-red fs-18">┗| {$page.status} |┛  Page Error~~</div>
        <div class="c-999 mt-10 mb-10">{$page.error.message}</div>
        <Button size="small" on:click={goHome}>Go Home</Button>
    </div>
</div>

svelte-i18n internationalization

Project routing menus and pages support dynamic configuration of Chinese, English/Traditional languages.

 npm i svelte-i18n

image.png

 /**
 * 国际化语言配置
 * @author YXY
 */

import { addMessages, init, getLocaleFromNavigator } from 'svelte-i18n'
import { browser } from '$app/env'
import Storage from '@/utils/storage'

// 引入语言配置
import cn from '@/locale/zh-CN'
import tw from '@/locale/zh-TW'
import en from '@/locale/en-US'

export const langKey = 'lang'
export const langVal = 'cn'

addMessages('cn', cn)
addMessages('tw', tw)
addMessages('en', en)

const lang = getLang()
console.log('当前国际化:', lang)
init({
    fallbackLocale: lang,
    initialLocale: getLocaleFromNavigator()
})
setHtmlLang(lang)

/* 获取语言 */
export function getLang() {
    const lang = Storage.get(langKey)
    return lang || langVal
}

/* 持久化存储 */
export function setLang(lang, reload = false) {
    if(lang != getLang()) {
        Storage.set(langKey, lang || '')
        setHtmlLang(lang)

        // 重载页面
        if(reload) {
            window.location.reload()
        }
    }
}

svelte dynamic chart configuration

In order to solve the problem of using charts in multiple places, a new chart hooks file is created.

 /**
 * @title    动态图表Hooks
 * @author    YXY
*/
import * as echarts from 'echarts'
import elementResizeDetector from "element-resize-detector"
import utils from '@/utils'

export const useCharts = async(node, options) => {
    let chartInstance
    let chartNode = null
    let erd = elementResizeDetector()

    const resizeFn = utils.debounce(() => {
        chartInstance.resize()
    }, 100)

    if(node) {
        chartInstance = echarts.init(node)
        chartInstance.setOption(options)
        chartNode = chartInstance
    }
    erd.listenTo(node, resizeFn)
}

Well, based on svlete-ui development background management system, I will share so much first. We will share some example projects in the future.

https://segmentfault.com/a/1190000041845857

https://segmentfault.com/a/1190000041357547


xiaoyan2017
765 声望315 粉丝

web前端开发爱好者,专注于前端h5、jquery、vue、react、angular等技术研发实战项目案例。