svelte+vite+ts+melt-ui從0到1完整框架搭建

框架太“重”了:通常一個小型項目只由少數幾個簡單頁面構成,如果使用 Vue 或者 React 這些框架來研發的話,有點“大材小用”了。構建的產物中包含了不少框架運行時代碼(虛擬 DOM、響應式、狀態管理等),這些代碼對于小型項目而言是冗余的,它們影響了包體大小,進而影響頁面的啟動速度和執行性能。
打包太慢了:以 Vue CLI 為例,它的底層基于 Webpack,雖然 Webpack 具備更強大的功能和靈活性,但相比于 Vite、Esbuild 這些以速度為標桿的構建工具來說,它的速度確實慢了一些,影響了研發效率。

文章目錄

    • 一、 創建基本項目
      • 1.1 全局安裝 Vite
      • 1.2 創建 Svelte 項目
    • 二、目錄結構
    • 三、svelte路由配置
      • 3.1 npm安裝
      • 3.2 定義router
        • 3.2.1 動態導入組件
        • 3.2.2 在頁面之間導航
      • 3.3 使用路由
    • 四、svelte CSS預處理器
      • 4.1 less的使用
        • 4.1.1 npm安裝
      • 4.2 Tailwind CSS的使用
    • 五、svelte環境變量配置
      • 5.1 環境變量命名規則
      • 5.2 .env文件的使用
      • 5.3 在代碼中使用環境變量
      • 5.4 配置運行與打包環境
    • 六、svelte國際化
      • 6.1 安裝 `svelte-i18n`
      • 6.2 初始化 `svelte-i18n`
      • 6.3 創建語言文件
      • 6.4 在 Svelte 組件中使用 `svelte-i18n`
      • 6.5 切換語言
      • 6.6 在 `App.svelte` 中引入 `i18n.js`
      • 6.7 運行項目
      • 6.8 構建項目
      • 6.9 預覽項目
      • 6.10 檢查項目
    • 七、svelte接口請求
      • 7.1 安裝 `axios`
      • 7.2 創建 `axios` 實例
      • 7.3 在 Svelte 組件中使用 `axios`
      • 7.4 處理請求和響應攔截器
      • 7.5 在 `App.svelte` 中使用 `axios`
      • 7.6 處理錯誤
    • 八、svelte組件庫
    • 九、svelte阿里圖標庫
      • 9.1 獲取阿里圖標
      • 9.2 將圖標文件放入項目
      • 9.3 引入圖標文件
      • 9.4 使用圖標
      • 9.5 動態切換圖標
      • 9.6 使用 Symbol 方式(可選)
      • 9.7 樣式調整(可選)
      • 9.8 示例代碼
    • 十、svelte輪播圖
    • 十一、store數據共享
      • 1. 創建 `store` 模塊
      • 2. 創建全局 `store`
      • 3. 在組件中使用 `store`
      • 4. 封裝 `store` 的優勢
      • 5. 示例:`about.svelte` 中使用 `store`
    • 十二、擴展內容
    • 十三、框架git地址

一、 創建基本項目

1.1 全局安裝 Vite

通過 npm 全局安裝 Vite

npm install vite 

1.2 創建 Svelte 項目

Vite 原生支持直接通過腳手架創建 Svelte 項目,執行以下命令

npm create vite@latest

輸入命令后選擇如下

? Project name: vite-svelte? Select a framework: ? - Use arrow-keys. Return to submit.VanillaVueReactPreactLit
?   SvelteSolidQwikOthers? Select a variant: ? - Use arrow-keys. Return to submit.TypeScript
?   JavaScriptSvelteKit

基本項目創建完成

二、目錄結構

根據上一步創建項目,項目的基本結構栓是完成了,但這樣還是不夠的,接下來介紹一下完整的項目目錄
在這里插入圖片描述

三、svelte路由配置

3.1 npm安裝

項目中安裝svelte-spa-router

npm install svelte-spa-router

3.2 定義router

  • 每條路由都是一個普通的Svelte組件,包含標記、腳本、綁定等。任何Svelte組件都可以是路由。
  • 路由定義只是一個JavaScript字典(對象),其中鍵是一個帶有路徑(包括參數等)的字符串,值是路由對象。
import Home from './routes/Home.svelte'
import Author from './routes/Author.svelte'
import Book from './routes/Book.svelte'
import NotFound from './routes/NotFound.svelte'const routes = {// Exact path'/': Home,// Using named parameters, with last being optional'/author/:first/:last?': Author,// Wildcard parameter'/book/*': Book,// Catch-all// This is optional, but if present it must be the last'*': NotFound,
}
3.2.1 動態導入組件

使用動態導入的優點是,如果您的打包器支持,您可以啟用代碼拆分并減小發送給用戶的捆綁包的大小。這已經用包括Rollup和Webpack在內的捆綁器進行了測試

  1. 要使用動態導入的組件,您需要利用包裝方法(根據路線包裝文檔,該方法可用于各種操作)。首先,導入wrap方法:
import {wrap} from 'svelte-spa-router/wrap'
  1. 然后,在路由定義中,使用wrap方法包裝路由,將一個函數傳遞給asyncComponent屬性,該函數將動態導入的組件返回給asyncComponent:
wrap({asyncComponent: () => import('./Foo.svelte')
})

案例:

// Import the wrap method
import {wrap} from 'svelte-spa-router/wrap'// Note that Author and Book are not imported here anymore, so they can be imported at runtime
import Home from './routes/Home.svelte'
import NotFound from './routes/NotFound.svelte'const routes = {'/': Home,// Wrapping the Author component'/author/:first/:last?': wrap({asyncComponent: () => import('./routes/Author.svelte')}),// Wrapping the Book component'/book/*': wrap({asyncComponent: () => import('./routes/Book.svelte')}),// Catch-all route last'*': NotFound,
}
3.2.2 在頁面之間導航
  1. 錨點導航
<a href="#/book/123">Thus Spoke Zarathustra</a>
  1. use:link導航(可以使用use:link操作,而不必在每個鏈接前鍵入#)
<script>
import {link} from 'svelte-spa-router'
</script>
<a href="/book/321" use:link>The Little Prince</a>

3.3 使用路由

在app.svelte中全局調用

import Router from 'svelte-spa-router'

然后,通過將組件放置在標記中,將路由器顯示在您想要的任何位置

<body><Router {routes}/>
</body>

四、svelte CSS預處理器

4.1 less的使用

4.1.1 npm安裝

安裝less與svelte-preprocess-less依賴

npm install --save-dev svelte-preprocess-less less

在vite.config.js進行配置

import { less } from 'svelte-preprocess-less'
export default defineConfig({plugins: [svelte({preprocess: {style: less(),},})],
})

4.2 Tailwind CSS的使用

通過npx安裝直接配置完 tailwindcss

npx sv add tailwindcss

五、svelte環境變量配置

?Vite中使用環境變量主要通過.env文件來配置,這些文件根據不同的環境(開發、測試、生產等)有不同的命名規則和使用方式。

5.1 環境變量命名規則

所有環境變量必須以VITE_為前綴

VITE_API_URL=https://api.example.com
VITE_APP_TITLE=My Vite App

5.2 .env文件的使用

1?. 通用環境變量?:在項目的根目錄下創建.env文件,用于定義所有環境通用的變量。
2?. 特定環境變量?:根據不同的環境需求,可以創建以下類型的.env文件:
.env.devt:僅在開發環境中使用。
.env.pro:僅在生產環境中使用。
.env.local:通用的本地配置文件,通常不提交到版本控制系統中。
.env.development.local:開發環境的本地配置文件。
.env.production.local:生產環境的本地配置文件?

5.3 在代碼中使用環境變量

console.log(import.meta.env.VITE_API_URL);

5.4 配置運行與打包環境

  "scripts": {"dev": "vite --mode dev",//運行dev環境"dev-pro": "vite --mode pro",//運行pro環境"build": "vite build","preview": "vite preview","check": "svelte-check --tsconfig ./tsconfig.app.json && tsc -p tsconfig.node.json"},

六、svelte國際化

svelte-i18n 是一個用于 Svelte 應用的國際化(i18n)庫,它可以幫助你輕松地管理和切換應用中的多語言內容。以下是如何在 Svelte 項目中使用 svelte-i18n 的基本步驟:

6.1 安裝 svelte-i18n

首先,確保你已經安裝了 svelte-i18n。根據你的 package.json 文件,它已經存在于 dependencies 中。

npm install svelte-i18n

6.2 初始化 svelte-i18n

在你的 Svelte 項目中,通常會在 src 目錄下創建一個 i18n.jsi18n.ts 文件來初始化 svelte-i18n

// src/i18n.js
import { init, register, locale } from 'svelte-i18n';// 注冊默認語言
register('en', () => import('./locales/en.json'));
register('zh', () => import('./locales/zh.json'));// 初始化并設置默認語言
init({fallbackLocale: 'en',initialLocale: 'en',
});

6.3 創建語言文件

src/locales 目錄下創建語言文件,例如 en.jsonzh.json

// src/locales/en.json
{"welcome": "Welcome to Svelte App","greeting": "Hello, {name}!"
}
// src/locales/zh.json
{"welcome": "歡迎使用 Svelte 應用","greeting": "你好, {name}!"
}

6.4 在 Svelte 組件中使用 svelte-i18n

你可以在 Svelte 組件中使用 $t 函數來獲取翻譯內容。

<script>import { t } from 'svelte-i18n';
</script><h1>{$t('welcome')}</h1>
<p>{$t('greeting', { name: 'John' })}</p>

6.5 切換語言

你可以通過 locale.set 方法來動態切換語言。

<script>import { locale } from 'svelte-i18n';
</script><button on:click={() => locale.set('en')}>English</button>
<button on:click={() => locale.set('zh')}>中文</button>

6.6 在 App.svelte 中引入 i18n.js

  1. 確保在 App.svelte 或你的主入口文件中引入 i18n.js
<script>import './i18n.js';
</script>
  1. 確保加載完i18n后在加載頁面
<script>import { locale } from "svelte-i18n";import Router from "@/router/Router.svelte";
</script>
{#if $locale}<Layout><Router /></Layout>
{/if}

6.7 運行項目

使用 npm run dev 運行你的項目,你應該能夠看到國際化內容并根據按鈕切換語言。

6.8 構建項目

當你準備好發布項目時,使用 npm run build 來構建項目。

npm run build

6.9 預覽項目

使用 npm run preview 來預覽構建后的項目。

npm run preview

6.10 檢查項目

使用 npm run check 來檢查 Svelte 和 TypeScript 的類型。

npm run check

通過以上步驟,你應該能夠在 Svelte 項目中成功使用 svelte-i18n 來實現國際化功能。

七、svelte接口請求

在 Svelte 項目中使用 axios 進行 HTTP 請求是非常常見的操作。以下是如何在 Svelte 項目中集成和使用 axios 的步驟:

7.1 安裝 axios

首先,確保你已經安裝了 axios。根據你的 package.json 文件,它已經存在于 dependencies 中。

npm install axios

7.2 創建 axios 實例

為了更好的管理和配置 axios,通常會在 src/utils 目錄下創建一個 api.tsapi.js 文件來創建 axios 實例。

// src/utils/api.ts
import axios from 'axios';const api = axios.create({baseURL: 'https://api.example.com', // 你的 API 基礎 URLtimeout: 10000, // 請求超時時間headers: {'Content-Type': 'application/json',},
});export default api;

7.3 在 Svelte 組件中使用 axios

你可以在 Svelte 組件中導入并使用 axios 實例來發送 HTTP 請求。

<script lang="ts">import api from '@/utils/api';import { onMount } from 'svelte';let data: any;onMount(async () => {try {const response = await api.get('/endpoint');data = response.data;} catch (error) {console.error('Error fetching data:', error);}});
</script>{#if data}<div><h1>{data.title}</h1><p>{data.description}</p></div>
{/if}

7.4 處理請求和響應攔截器

你可以在 axios 實例中添加請求和響應攔截器,以便在請求發送前或響應到達后進行一些處理。

// src/utils/api.ts
import axios from 'axios';const api = axios.create({baseURL: 'https://api.example.com',timeout: 10000,headers: {'Content-Type': 'application/json',},
});// 請求攔截器
api.interceptors.request.use((config) => {// 在請求發送之前做一些處理,例如添加 tokenconst token = localStorage.getItem('token');if (token) {config.headers.Authorization = `Bearer ${token}`;}return config;},(error) => {return Promise.reject(error);}
);// 響應攔截器
api.interceptors.response.use((response) => {// 對響應數據做一些處理return response;},(error) => {// 對響應錯誤做一些處理return Promise.reject(error);}
);export default api;

7.5 在 App.svelte 中使用 axios

你可以在 App.svelte 中使用 axios 來獲取數據或執行其他 HTTP 操作。

<script lang="ts">import api from '@/utils/api';import { onMount } from 'svelte';let userData: any;onMount(async () => {try {const response = await api.get('/user');userData = response.data;} catch (error) {console.error('Error fetching user data:', error);}});
</script>{#if userData}<div><h1>Welcome, {userData.name}!</h1><p>Email: {userData.email}</p></div>
{/if}

7.6 處理錯誤

在使用 axios 時,確保你處理了可能的錯誤,例如網絡錯誤或服務器錯誤。

<script lang="ts">import api from '@/utils/api';import { onMount } from 'svelte';let userData: any;let errorMessage: string | null = null;onMount(async () => {try {const response = await api.get('/user');userData = response.data;} catch (error) {errorMessage = 'Failed to fetch user data. Please try again later.';console.error('Error fetching user data:', error);}});
</script>{#if userData}<div><h1>Welcome, {userData.name}!</h1><p>Email: {userData.email}</p></div>
{:else if errorMessage}<p style="color: red;">{errorMessage}</p>
{/if}

通過以上步驟,你應該能夠在 Svelte 項目中成功使用 axios 來進行 HTTP 請求。

八、svelte組件庫

這里用的是melt-ui,訪問地址是:https://www.melt-ui.com/docs/introduction
一鍵配置

npx @melt-ui/cli@latest init

九、svelte阿里圖標庫

在 Svelte 項目中使用阿里圖標(如 iconfont)可以通過以下步驟實現:


9.1 獲取阿里圖標

  1. 訪問 iconfont 并登錄。
  2. 創建一個項目,將需要的圖標添加到項目中。
  3. 選擇 Font classSymbol 方式生成代碼。
  4. 點擊 下載至本地,解壓后得到圖標文件。

9.2 將圖標文件放入項目

將下載的圖標文件(如 iconfont.css 和字體文件)放入項目的 publicsrc/assets 目錄中。

例如:

public/iconfont/iconfont.cssiconfont.ttficonfont.wofficonfont.woff2

9.3 引入圖標文件

App.sveltemain.ts 中引入 iconfont.css 文件。

<script lang="ts">import "./app.css";import Layout from "@/layout/Layout.svelte";import Router from "@/router/Router.svelte";import { locale } from "svelte-i18n";import Toast from "./components/Toast.svelte";import { toast } from "@/utils/toastService";// 引入阿里圖標import '../public/iconfont/iconfont.css';
</script>

9.4 使用圖標

在 Svelte 組件中使用阿里圖標,直接通過 class 引用圖標類名。

<div><i class="iconfont icon-home"></i> <!-- icon-home 是圖標類名 --><i class="iconfont icon-user"></i> <!-- icon-user 是圖標類名 -->
</div>

9.5 動態切換圖標

如果需要動態切換圖標,可以將圖標類名綁定到變量。

<script lang="ts">let iconClass = 'icon-home';
</script><div><i class={`iconfont ${iconClass}`}></i><button on:click={() => iconClass = 'icon-user'}>切換圖標</button>
</div>

9.6 使用 Symbol 方式(可選)

如果選擇 Symbol 方式,需要引入 iconfont.js 文件,并使用 <svg> 標簽。

<script lang="ts">import '../public/iconfont/iconfont.js';
</script><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home"></use> <!-- #icon-home 是圖標 ID -->
</svg>

9.7 樣式調整(可選)

如果需要調整圖標大小或顏色,可以通過 CSS 設置。

<style lang="less">.iconfont {font-size: 24px;color: #333;}
</style>

9.8 示例代碼

以下是一個完整的示例:

<script lang="ts">import "./app.css";import Layout from "@/layout/Layout.svelte";import Router from "@/router/Router.svelte";import { locale } from "svelte-i18n";import Toast from "./components/Toast.svelte";import { toast } from "@/utils/toastService";// 引入阿里圖標import '../public/iconfont/iconfont.css';let iconClass = 'icon-home';
</script>{#if $locale}<Layout><Router /></Layout>{#if $toast.visible}<Toast message={$toast.message} />{/if}
{/if}<div><i class={`iconfont ${iconClass}`}></i><button on:click={() => iconClass = 'icon-user'}>切換圖標</button>
</div><style lang="less">.iconfont {font-size: 24px;color: #333;}
</style>

通過以上步驟,你可以在 Svelte 項目中成功使用阿里圖標。如果需要更多定制化功能,可以參考 iconfont 官方文檔。

十、svelte輪播圖

這里用的是https://3.swiper.com.cn/
下載引入相關css與js即可
demo如下

<script>import { onMount } from 'svelte';import  '@/utils/swiper/swiper.min.js';import '@/utils/swiper/swiper.min.css';let swiperInstance;onMount(() => {// 初始化 SwiperswiperInstance = new Swiper('.swiper-container', {pagination: '.swiper-pagination',paginationClickable: true,autoplay:2500,loop:true});});</script><style>html, body {position: relative;height: 100%;}body {background: #eee;font-family: Helvetica Neue, Helvetica, Arial, sans-serif;font-size: 14px;color:#000;margin: 0;padding: 0;}.swiper-container {width: 100%;height: 350px;}.swiper-slide {text-align: center;font-size: 18px;background: #fff;/* Center slide text vertically */display: -webkit-box;display: -ms-flexbox;display: -webkit-flex;display: flex;-webkit-box-pack: center;-ms-flex-pack: center;-webkit-justify-content: center;justify-content: center;-webkit-box-align: center;-ms-flex-align: center;-webkit-align-items: center;align-items: center;}</style><!-- Swiper --><div class="swiper-container"><div class="swiper-wrapper"><div class="swiper-slide">Slide 1</div><div class="swiper-slide">Slide 2</div></div><!-- Add Pagination --><div class="swiper-pagination"></div>
</div>

十一、store數據共享

在 Svelte 中,store 是一個核心概念,用于管理應用的狀態。為了更好地組織代碼,可以將 store 封裝為模塊,包括 stateactionsgettersmutations,類似于 Vuex 或 Redux 的設計模式。以下是如何封裝 store 的示例:


1. 創建 store 模塊

src/store 目錄下創建一個模塊,例如 centerStore.ts,用于管理特定模塊的狀態和邏輯。

// src/store/centerStore.ts
import { writable, derived } from 'svelte/store';// State
const state = writable({userData: null,loading: false,error: null,
});// Actions
const actions = {async getUserData(params: { onlyMakeTheSame: boolean }) {try {state.update((s) => ({ ...s, loading: true, error: null }));// 模擬 API 調用const response = await fetch('/api/user', { method: 'GET' });const data = await response.json();state.update((s) => ({ ...s, userData: data, loading: false }));} catch (error) {state.update((s) => ({ ...s, error: error.message, loading: false }));}},
};// Getters
const getters = {userData: derived(state, ($state) => $state.userData),isLoading: derived(state, ($state) => $state.loading),error: derived(state, ($state) => $state.error),
};// Mutations (可選)
const mutations = {setUserData(userData: any) {state.update((s) => ({ ...s, userData }));},
};// 導出模塊
export const centerStore = {state,actions,getters,mutations,
};

2. 創建全局 store

src/store/index.ts 中整合所有模塊,創建一個全局 store

// src/store/index.ts
import { centerStore } from './centerStore';export const store = {center: centerStore,
};

3. 在組件中使用 store

在 Svelte 組件中導入并使用 store

<script lang="ts">import { store } from '@/store/index';import { onMount } from 'svelte';// 獲取 state 和 gettersconst { state, getters } = store.center;// 調用 actionfunction fetchData() {store.center.actions.getUserData({ onlyMakeTheSame: false });}onMount(() => {fetchData();});
</script>{#if $getters.isLoading}<p>Loading...</p>
{:else if $getters.error}<p style="color: red;">Error: {$getters.error}</p>
{:else if $getters.userData}<div><h1>User Data</h1><pre>{JSON.stringify($getters.userData, null, 2)}</pre></div>
{/if}<button on:click={fetchData}>Refresh Data</button>

4. 封裝 store 的優勢

  • 模塊化:將狀態和邏輯按模塊劃分,便于維護和擴展。
  • 復用性actionsgetters 可以在多個組件中復用。
  • 可測試性actionsmutations 可以單獨測試。
  • 清晰性stateactionsgettersmutations 分離,代碼結構更清晰。

5. 示例:about.svelte 中使用 store

根據你的 about.svelte 文件,可以這樣使用 store

<script lang="ts">import { t, locale } from "svelte-i18n";import { toast } from '@/utils/toastService';import { store } from '@/store/index';function getData() {store.center.actions.getUserData({ onlyMakeTheSame: false });}
</script><h1>{$t("welcome")}</h1>
<p>{$t("about")}</p><button on:click={getData}>獲取接口數據</button>{#if $store.center.getters.isLoading}<p>Loading...</p>
{:else if $store.center.getters.error}<p style="color: red;">Error: {$store.center.getters.error}</p>
{:else if $store.center.getters.userData}<div><h1>User Data</h1><pre>{JSON.stringify($store.center.getters.userData, null, 2)}</pre></div>
{/if}

通過以上步驟,你可以在 Svelte 項目中封裝 store,并實現 stateactionsgettersmutations 的分離,使代碼更易于維護和擴展。

十二、擴展內容

這里由于使用的melt-ui沒有toast提示于是做了一個全局組建toas.svelte

  1. 組建創建
<script>import { fade } from "svelte/transition";export let message = "";export let duration = 3000; // 持續時間,單位毫秒let visible = false;const showToast = () => {visible = true;setTimeout(() => {visible = false;}, duration);};showToast(); // 顯示Toast
</script>{#if visible}<div class="toast" transition:fade>{message}</div>
{/if}<style>.toast {position: fixed;top: 300px;left: 50%;transform: translateX(-50%);padding: 10px 20px;background-color: #333;color: white;border-radius: 5px;z-index: 1000;}
</style>
  1. toastService封裝
import { writable } from 'svelte/store';
function createToast() {const { subscribe, set, update } = writable({ message: '', visible: false });function show(message, duration = 3000) {set({ message, visible: true });setTimeout(() => {update(current => ({ ...current, visible: false }));}, duration);}return {subscribe,show, // 公開show方法供外部調用};
}export const toast = createToast(); // 創建并導出toast服務實例
  1. 全局調用app.svelte
<script lang="ts">import Toast from "./components/Toast.svelte";import { toast } from "@/utils/toastService";
</script>{#if $toast.visible}<!-- 使用$來訪問store的值 --><Toast message={$toast.message} /><!-- 將消息傳遞給Toast組件 -->{/if}
  1. 使用
  import { toast } from '@/utils/toastService';toast.show('Hello, this is a toast!')

十三、框架git地址

https://gitee.com/cyp926/svelte-vite

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/75999.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/75999.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/75999.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

無法看到新安裝的 JDK 17

在 Linux 系統中使用 update-alternatives --config java 無法看到新安裝的 JDK 17&#xff0c;可能是由于 JDK 未正確注冊到系統備選列表中。 一、原因分析 JDK 未注冊到 update-alternatives update-alternatives 工具需要手動注冊 JDK 路徑后才能識別新版本。如果僅安裝 JDK…

鼎訊信通 便攜式雷達信號干擾模擬器:打造實戰化電磁環境的新利器

在現代戰爭中&#xff0c;電磁環境的復雜性直接影響著雷達裝備的性能和作戰效果。面對敵方日益精進的電子戰手段&#xff0c;如何提升雷達設備的抗干擾能力&#xff0c;確保其在實戰環境中的穩定性和可靠性&#xff0c;已成為各國軍隊和科研機構的重要課題。 為此&#xff0c;…

【AI提示詞】決策專家

提示說明 決策專家可以幫助你進行科學決策&#xff0c;盡可能避免錯誤&#xff0c;提升決策成功的概率。 提示詞 # Role : 決策專家決策&#xff0c;是面對不容易判斷優劣的幾個選項&#xff0c;做出正確的選擇。說白了&#xff0c;決策就是拿個主意。決策專家是基于科學決策…

力扣Hot100題,刷題

力扣HOT100 - 1. 兩數之和 解題思路&#xff1a; 解法一&#xff1a;暴力 class Solution {public int[] twoSum(int[] nums, int target) {int n nums.length;for (int i 0; i < n; i)for (int j i 1; j < n; j) {if (target nums[i] nums[j])return new int[]…

uni-app ucharts自定義換行tooltips

實現效果&#xff1a; 第一步&#xff1a;在uni_modules文件夾下找到config-ucharts.js和u-charts.js文件 第二步&#xff1a;在config-ucharts.js文件中配置換行格式 // 換行格式"wrapTooltip":function(item, category, index, opts){return item.name&#xff1a;…

國標GB28181視頻平臺EasyCVR順應智慧農業自動化趨勢,打造大棚實時視頻監控防線

一、方案背景 近年來&#xff0c;溫室大棚種植技術憑借其顯著的優勢&#xff0c;在提升農作物產量和質量、豐富農產品供應方面發揮了重要的作用&#xff0c;極大改善了人們的生活水平&#xff0c;得到了廣泛的推廣和應用。大棚內的溫度、濕度、光照度和二氧化碳濃度等環境因素…

InternVideo2.5:Empowering Video MLLMs with Long and Rich Context Modeling

一、TL&#xff1b;DR InternVideo2.5通過LRC建模來提升MLLM的性能。層次化token壓縮和任務偏好優化&#xff08;mask時空 head&#xff09;整合到一個框架中&#xff0c;并通過自適應層次化token壓縮來開發緊湊的時空表征MVBench/Perception Test/EgoSchema/MLVU數據benchmar…

【時時三省】(C語言基礎)條件運算符和條件表達式

山不在高&#xff0c;有仙則名。水不在深&#xff0c;有龍則靈。 ----CSDN 時時三省 有一種if語句&#xff0c;當被判別的表達式的值為“真”或“假”時&#xff0c;都執行一個賦值語句且向一個變量賦值。 如&#xff1a; if ( a > b ) max a&#xff1b; else max …

KWDB創作者計劃—邊緣計算:從概念到落地的技術解讀

引言 隨著物聯網&#xff08;IoT&#xff09;和人工智能&#xff08;AI&#xff09;的快速發展&#xff0c;數據量呈爆炸式增長&#xff0c;傳統的云計算架構逐漸暴露出延遲高、帶寬占用大等問題。邊緣計算作為一種新興的分布式計算范式&#xff0c;正在改變數據處理的方式。本…

藍橋杯基礎算法-遞歸

代碼簡潔&#xff0c;但涉及到的運算&#xff0c;會隨著遞歸層數的增加成指數級增長 路分析&#xff1a;第20行20列位于45度這條線上 這條線上的數字是1 5 13 25 41...兩數之差:4 8 12 16 --->每一個都是在前面的基礎上4&#xff0c;可以用遞歸或者循環 public class dem…

通過學習opencv圖像庫編程借助第三方庫函數完成一個綜合程序設計

通過學習opencv圖像庫編程借助第三方庫函數完成一個綜合程序設計 1) 編譯命令解釋&#xff1a; 編譯命令&#xff1a; gcc test1.cpp -o test1 pkg-config --cflags --libs opencv這條命令包含了以下部分&#xff1a; gcc test1.cpp -o test1: gcc 是 GNU 編譯器集合&#…

第十四屆藍橋杯大賽軟件賽國賽C/C++研究生組

研究生C國賽軟件大賽 題一&#xff1a;混乘數字題二&#xff1a;釘板上的正方形題三&#xff1a;整數變換題四&#xff1a;躲炮彈題五&#xff1a;最大區間 題一&#xff1a;混乘數字 有一點像哈希表&#xff1a; 首先定義兩個數組&#xff0c;拆分ab和n 然后令n a*b 查看兩個…

系統與網絡安全------網絡通信原理(2)

資料整理于網絡資料、書本資料、AI&#xff0c;僅供個人學習參考。 物理層解析 物理層概述 物理層是TCP/IP模型的最底層物理層數據傳輸提供穩定的物理連接 物理層功能 定義設備的物理連接的標準和特性&#xff0c;比如接口形狀&#xff0c;大小定義電氣特性&#xff0c;高低…

內容中臺的數字化管理核心是什么?

數字化整合與系統協同 現代企業的內容管理正經歷從分散式架構向數字化整合的范式轉變。通過將內容管理系統與文檔管理技術深度耦合&#xff0c;組織能夠打破數據孤島&#xff0c;實現跨部門、跨平臺的資源互通。例如&#xff0c;基于元數據分類的標準化體系&#xff0c;不僅提…

Python爬蟲第二戰(使用xpath爬取網站數據)

本文是我在學習過程中記錄學習的點點滴滴&#xff0c;目的是為了學完之后鞏固一下順便也和大家分享一下&#xff0c;日后忘記了也可以方便快速的復習。 使用xpath爬取豬八戒網站數據 前言 前言 今天學習的主要是關于Python使用xpath來爬取豬八戒網的網頁知識的理解和應用 #1.獲…

進程同步和進程互斥的區別

如大家所了解的&#xff0c;進程互斥是由互斥資源引起的&#xff0c;即各進程之間共享互斥資源的使用權&#xff0c;這種競爭沒有確定的必然聯系&#xff0c;哪個進程競爭到互斥資源的使用權&#xff0c;則該資源就歸哪個進程使用&#xff0c;從而獲得所需資源的進程就可以獲得…

ArkTS語言基礎之函數

前言 臭寶們終于來到了ArkTS基礎之函數&#xff0c;今天我們來學習一下ArkTS的函數的相關知識&#xff0c;上一節中也有一些函數的基礎知識。 函數聲明 函數聲明引入一個函數&#xff0c;包含其名稱、參數列表、返回類型和函數體,在下面的例子中&#xff0c;我們聲明了一個名…

redis中的hash

Redis中的hash是什么 Hash: 哈希&#xff0c;也叫散列&#xff0c;是一種通過哈希函數將鍵映射到表中位置的數據結構&#xff0c;哈希函數是關鍵&#xff0c;它把鍵轉換成索引。 Redis Hash&#xff08;散列表&#xff09;是一種 field-value pairs&#xff08;鍵值對&#x…

彈簧質點系統(C++實現)

本文實現一個簡單的物理算法&#xff1a;彈簧質點系統&#xff08;Mass-Spring System&#xff09;。這是一個經典的物理模擬算法&#xff0c;常用于模擬彈性物體&#xff08;如布料、彈簧等&#xff09;的行為。我們將使用C來實現這個算法&#xff0c;并結合鏈表數據結構來管理…

領域大模型

領域技術標準文檔或領域相關數據是領域模型Continue PreTrain的關鍵。 現有大模型在預訓練過程中都會加入書籍、論文等數據&#xff0c;那么在領域預訓練時這兩種數據其實也是必不可少的&#xff0c;主要是因為這些數據的數據質量較高、領域強相關、知識覆蓋率&#xff08;密度…