{"id":3925,"date":"2024-11-10T08:40:36","date_gmt":"2024-11-10T00:40:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3925\/"},"modified":"2024-11-10T08:40:36","modified_gmt":"2024-11-10T00:40:36","slug":"svelte-5-%e4%b8%ad%e7%9a%84%e5%8a%a9%e6%89%8b%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3925\/","title":{"rendered":"Svelte 5 \u4e2d\u7684\u201c\u52a9\u624b\u201d\u53d8\u91cf"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173119698378766.jpg\" class=\"aligncenter\" title=\"Svelte 5 \u4e2d\u7684\u201c\u52a9\u624b\u201d\u53d8\u91cf\u63d2\u56fe\" alt=\"Svelte 5 \u4e2d\u7684\u201c\u52a9\u624b\u201d\u53d8\u91cf\u63d2\u56fe\" \/><\/p>\n<h2> \u518d\u89c1\u795e\u5947 svelte 4 $\uff1a <\/h2>\n<p>\u5728\u6211\u6700\u8fd1\u53d1\u5e03 svelte 5 \u8fc1\u79fb\u7684\u7ecf\u9a8c\u548c\u6ce8\u610f\u4e8b\u9879\u4e4b\u540e\uff0c\u6211\u60f3\u91cd\u70b9\u4ecb\u7ecd\u4ece svelte 4 \u8fc1\u79fb\u5230 svelte 5 \u65f6\u7684\u4e00\u4e9b\u6280\u5de7\u548c\u5fc3\u6001\u7684\u53d8\u5316\u3002<\/p>\n<p>svelte 4 \u4f7f\u7528\u201c\u795e\u5947\u7684\u201d$: \u5e76\u8ba9\u548c\u5b8c\u6210\u6240\u6709\u7e41\u91cd\u7684\u5de5\u4f5c\u4ee5\u4f7f\u4ee3\u7801\u5177\u6709\u53cd\u5e94\u6027\u3002\u6211\u4eec\u8fd8\u63a5\u53d7\u4e86\u53d8\u91cf\u91cd\u65b0\u5206\u914d\uff0c\u4f8b\u5982<\/p>\n<pre>&lt;script&gt;\nlet arr = [1, 2, 3]\nlet value = 4\n\narr = [...arr, value]\n&lt;\/script&gt;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u800c\u4e0d\u662f\u66f4\u65b0\/\u6539\u53d8\u53d8\u91cf\u7684\u65b9\u6cd5\uff0c\u5982\u63a8\u9001\u7b49<\/p>\n<p>\u6211\u5f88\u60ca\u8bb6\u80fd\u591f\u4f7f\u7528 svelte 5 \u91cd\u65b0\u5b66\u4e60\u597d\u7684\u65e7 js \u6a21\u5f0f\u3002<\/p>\n<h2> \u65e0\u9700\u4e00\u76f4\u4fdd\u6301\u53cd\u5e94 <\/h2>\n<p>\u800c\u4e14\u6211\u53ef\u80fd\u4e5f\u88ab svelte 4 \u4e2d\u7684 let \u5ba0\u574f\u4e86\uff0c\u6ca1\u6709\u8003\u8651\u53cd\u5e94\u6027\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\u5b83\u4f1a\u88ab\u5305\u542b\u5728\u5185\u3002 \u4f46\u5e76\u975e\u6240\u6709\u53d8\u91cf\u90fd\u5fc5\u987b\u662f\u53cd\u5e94\u6027\u7684\u3002\u975e\u53cd\u5e94\u6027\u53d8\u91cf\u4e5f\u53ef\u4ee5\u5728\u53cd\u5e94\u6027\u751a\u81f3\u201c\u4f20\u7edf\u53d8\u5f02\u4ee3\u7801\u201d\u4e2d\u66f4\u65b0\u3002\u53cd\u5e94\u5f0f\u53d8\u91cf\u7684\u771f\u6b63\u9700\u8981\u662f\u5f53\u6211\u4eec\u5728 ui \u4e2d\u4f7f\u7528\u5b83\u65f6\uff08\u5f53\u8fd9\u4e2a\u53d8\u91cf\u5728 html\/\u9875\u9762\u4e2d\u5448\u73b0\u5e76\u4e14\u6211\u4eec\u9700\u8981\u5b83\u7a0d\u540e\u66f4\u65b0\u65f6\uff09\u3002 <\/p>\n<p>\u60a8\u53ef\u80fd\u4f1a\u5728 svelte 5 \u4e2d\u9047\u5230\u9519\u8bef\uff0c\u4f8b\u5982\u65e0\u6cd5\u5206\u914d\u7ed9\u6d3e\u751f\u72b6\u6001\uff0c\u5728\u5176\u81ea\u5df1\u7684\u8303\u56f4\u5185\u5f15\u7528\u7684\u72b6\u6001\u6c38\u8fdc\u4e0d\u4f1a\u66f4\u65b0\u3002\u60a8\u7684\u610f\u601d\u662f\u5728\u95ed\u5305\u5185\u5f15\u7528\u5b83\u5417\uff1f\u6216\u884d\u751f_\u5f15\u7528_selfn\u5982\u679c\u4f7f\u7528 svelte 4 \u7f16\u7801\u98ce\u683c\uff0c\u5219\u6d3e\u751f\u503c\u65e0\u6cd5\u9012\u5f52\u5f15\u7528\u81ea\u8eab\u3002<\/p>\n<h2> \u8f85\u52a9\u53d8\u91cf\u7684\u793a\u4f8b <\/h2>\n<p>\u770b\u4e00\u4e0b\u8fd9\u4e2a svelte 4 \u98ce\u683c\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;script&gt;\n    let value;\n    let derivedarr = []\n\n    $: if (value) {\n        derivedarr = [...derivedarr, value]\n    }\n\n    function random () {\n        value = math.floor(1 + math.random() * 10)\n    }\n&lt;\/script&gt;\n\n&lt;button on:click={random}&gt;generate random value&lt;\/button&gt;\n&lt;p&gt;value: {value}&lt;\/p&gt;\n&lt;p&gt;derivedarr: {derivedarr}&lt;\/p&gt;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6f14\u793a<\/p>\n<p>\u6211\u4eec\u6709\u4e24\u4e2a\u53cd\u5e94\u53d8\u91cf\uff0csvelte 4 \u81ea\u52a8\u89e3\u51b3\u66f4\u65b0\u95ee\u9898\u3002\u6211\u4eec\u53ea\u9700\u8981\u8bb0\u4f4f\u6b63\u786e\u7684\u65b9\u6cd5\u662f\u91cd\u65b0\u5206\u914d\u53d8\u91cf\u3002 <\/p>\n<p>\u5728 svelte 5 \u4e2d\uff0c\u6211\u4eec\u5e94\u8be5\u601d\u8003\u5982\u4f55\u5b9e\u73b0\u76f8\u540c\u7684\u7ed3\u679c\u3002\u6211\u4eec\u4f7f\u7528\u7684\u4e24\u4e2a\u53d8\u91cf\u8fd8\u4e0d\u591f\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4e00\u4e2a\uff0c\u5373\u8f85\u52a9\u53d8\u91cf\u3002 <\/p>\n<p>\u9996\u9009\u65b9\u6cd5\u662f\u4f7f\u7528 $driven() \u7b26\u6587\u3002<\/p>\n<pre>&lt;script&gt;\n    let value = $state();\n    let helperarr = [];\n\n    let derivedarr = $derived.by(() =&gt; {\n      if (value) {\n                helperarr.push(value);\n                return helperarr;\n        }\n    });\n\n    function random () {\n        value = math.floor(1 + math.random() * 10)\n    }\n&lt;\/script&gt;\n\n&lt;button onclick={random}&gt;generate random value&lt;\/button&gt;\n&lt;p&gt;value: {value}&lt;\/p&gt;\n&lt;p&gt;derivedarr: {derivedarr}&lt;\/p&gt;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6f14\u793a<\/p>\n<p>\u5982\u679c\u60a8\u77e5\u9053\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u8bf7\u544a\u8bc9\u6211\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a $effect() \u7b26\u6587\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u76f8\u540c\u7684\u6548\u679c\u3002\u5b83\u53ef\u80fd\u770b\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u4f46\u6211\u4eec\u5e94\u8be5\u5c3d\u53ef\u80fd\u907f\u514d\u6548\u679c\uff08\u4e3b\u8981\u662f svetlet 5 \u6548\u679c\u4e0d\u5728\u670d\u52a1\u5668\/ssr \u4e0a\u8fd0\u884c\uff09\u3002<\/p>\n<pre>&lt;script&gt;\n    let value = $state();\n    let helperarr = []\n    let effectarr = $derived(helperarr);\n\n    $effect.pre(() =&gt; {\n            if (value) {\n              helperarr.push(value)\n            }\n        })\n\n    function random () {\n        value = math.floor(1 + math.random() * 10)\n    }\n&lt;\/script&gt;\n\n&lt;button onclick={random}&gt;generate random value&lt;\/button&gt;\n&lt;p&gt;value: {value}&lt;\/p&gt;\n&lt;p&gt;effectarr: {effectarr}&lt;\/p&gt;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6f14\u793a<\/p>\n<h2> \u73b0\u5b9e\u751f\u6d3b\u4e2d\u7684\u4f8b\u5b50 <\/h2>\n<p>\u8fd9\u662f\u6211\u5c1d\u8bd5\u5c06 svelte 4 \u9875\u9762\u76f4\u63a5\u8fc1\u79fb\u5230 svelte 5 \u7684\u793a\u4f8b\u3002\u6211\u82b1\u4e86\u4e00\u6bb5\u65f6\u95f4\u91cd\u65b0\u601d\u8003\u4ee3\u7801\u3002\u6b64\u9875\u9762\u7528\u4f5c\u5e16\u5b50\u641c\u7d22\uff0c\u5177\u6709\u201c\u52a0\u8f7d\u66f4\u591a\u201d\u529f\u80fd\uff08\u5982\u679c\u7528\u6237\u6ca1\u6709 js\uff0c\u5219\u6dfb\u52a0\u7ed3\u679c\u6216\u5206\u9875\uff09\uff1a<\/p>\n<p>\u82d7\u67614<\/p>\n<pre>&lt;script&gt;\n    import icon from '..\/components\/icon.svelte';\n    import { enhance } from '$app\/forms';\n    import { tick } from 'svelte';\n\n    export let form;\n    export let searchinglang;\n    export let l;\n\n    let results = [];\n    let previoussearch = '';\n    let searchterm;\n    let skip;\n\n    $: if (!!form &amp;&amp; form?.thereismore) {\n        searchterm = form.searchterm;\n        skip = number(form?.skip) + 20;\n    }\n\n    $: if (!!form?.searchresultfromaction) {\n        if (previoussearch == form.searchterm &amp;&amp; form.therewasmore) {\n            results = [...results, ...form.searchresultfromaction];\n        } else {\n            results = [...form.searchresultfromaction];\n            previoussearch = form.searchterm;\n        }\n    }\n\n    async function intoview(el) {\n        await tick();\n        if (el.attributes.index.nodevalue == skip - 20 &amp;&amp; skip != undefined) {\n            el.scrollintoview({ behavior: 'smooth' });\n        }\n    }\n&lt;\/script&gt;\n\n{#if results.length}\n    &lt;ol&gt;\n        {#each results as item, index}\n            &lt;li use:intoview {index} aria-posinset={index}&gt;\n                &lt;!-- users without javascript have calculated order of results within paggination and css disables standard ol ul numbering --&gt;\n                &lt;!-- users with javascript have standard ol ul numbering and loading more feature --&gt;\n                &lt;noscript&gt;{number(index) + 1 + number(form?.skip)}. &lt;\/noscript&gt;\n                &lt;a href=\"\/act\/{searchinglang}\/{item.id}\/present\/text\"&gt;{item.title}&lt;\/a&gt;\n            &lt;\/li&gt;\n        {\/each}\n    &lt;\/ol&gt;\n\n    {#if form?.thereismore}\n        &lt;form\n            method=\"post\"\n            action=\"?\/search&amp;skip={skip}&amp;therewasmore={form?.thereismore}\"\n            use:enhance\n            autocomplete=\"off\"\n        &gt;\n            &lt;label&gt;\n                &lt;!-- probably we do not need to bind the value as this is hidden input --&gt;\n                &lt;!-- &lt;input name=\"searchterm\" type=\"hidden\" bind:value={searchterm} \/&gt; --&gt;\n                &lt;input name=\"searchterm\" type=\"hidden\" value={searchterm} \/&gt;\n            &lt;\/label&gt;\n            &lt;button aria-label=\"button to load more search results\" class=\"outline\"&gt;\n                &lt;icon name=\"loadmore\" \/&gt;\n            &lt;\/button&gt;\n        &lt;\/form&gt;\n    {\/if}\n{:else if form?.searchresultfromaction.length == 0}\n    {l.noresultsfound}\n{\/if}\n\n&lt;style&gt;\n    @media (scripting: none) {\n        \/* users without javascript have calculated order of results within paggination and css disables standard ol ul numbering\nusers with javascript have standard ol ul numbering and loading more feature *\/\n        ol {\n            list-style-type: none;\n        }\n    }\n&lt;\/style&gt;\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u82d7\u67615<\/p>\n<pre>&lt;script&gt;\n    import Icon from '..\/components\/Icon.svelte';\n    import { enhance } from '$app\/forms';\n    import { tick } from 'svelte';\n\n    let { form, searchingLang, l } = $props();\n\n    let previousSearch = '';\n    let skip = $derived.by(() =&gt; {\n        if (!!form &amp;&amp; form?.thereIsMore) {\n            return Number(form?.skip) + 20;\n        }\n    });\n\n    let helperResultsArr = [];\n    let results = $derived.by(() =&gt; {\n        if (!!form?.searchResultFromAction) {\n            if (previousSearch == form.searchTerm &amp;&amp; form.thereWasMore) {\n                helperResultsArr.push(...form.searchResultFromAction);\n                return helperResultsArr;\n            } else {\n                helperResultsArr = [];\n                helperResultsArr.push(...form.searchResultFromAction);\n                previousSearch = form.searchTerm;\n                return helperResultsArr;\n            }\n        } else return [];\n    });\n\n    async function intoView(el) {\n        await tick();\n        if (el.attributes.index.nodeValue == skip - 20 &amp;&amp; skip != undefined) {\n            el.scrollIntoView({ behavior: 'smooth' });\n        }\n    }\n&lt;\/script&gt;\n\n{#if results.length}\n    &lt;ol&gt;\n        {#each results as item, index}\n            &lt;li use:intoView {index} aria-posinset={index}&gt;\n                &lt;!-- users without javascript have calculated order of results within paggination and css disables standard ol ul numbering --&gt;\n                &lt;!-- users with javascript have standard ol ul numbering and loading more feature --&gt;\n                &lt;noscript&gt;{Number(index) + 1 + Number(form?.skip)}. &lt;\/noscript&gt;\n                &lt;a href=\"\/act\/{searchingLang}\/{item.id}\/present\/text\"&gt;{item.title}&lt;\/a&gt;\n            &lt;\/li&gt;\n        {\/each}\n    &lt;\/ol&gt;\n\n    {#if form?.thereIsMore}\n        &lt;form\n            method=\"POST\"\n            action=\"?\/search&amp;skip={skip}&amp;thereWasMore={form?.thereIsMore}\"\n            use:enhance\n            autocomplete=\"off\"\n        &gt;\n            &lt;label&gt;\n                &lt;input name=\"searchTerm\" type=\"hidden\" value={form.searchTerm} \/&gt;\n            &lt;\/label&gt;\n            &lt;button aria-label=\"Button to load more search results\" class=\"outline\"&gt;\n                &lt;Icon name=\"loadMore\" \/&gt;\n            &lt;\/button&gt;\n        &lt;\/form&gt;\n    {\/if}\n{:else if form?.searchResultFromAction.length == 0}\n    {l.noResultsFound}\n{\/if}\n\n&lt;style&gt;\n    @media (scripting: none) {\n        \/* users without javascript have calculated order of results within paggination and css disables standard ol ul numbering\nusers with javascript have standard ol ul numbering and loading more feature *\/\n        ol {\n            list-style-type: none;\n        }\n    }\n&lt;\/style&gt;\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\u5c31\u8fd9\u4e9b\u4e86\u3002 <\/p>\n<p>ps\uff1a\u5982\u679c\u60a8\u613f\u610f\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u8fc1\u79fb\uff0c\u8bf7\u968f\u65f6\u544a\u8bc9\u6211\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSvelte 5 \u4e2d\u7684\u201c\u52a9\u624b\u201d\u53d8\u91cf\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u518d\u89c1\u795e\u5947 svelte 4 $\uff1a \u5728\u6211\u6700\u8fd1\u53d1\u5e03 svelte 5 \u8fc1\u79fb\u7684\u7ecf\u9a8c\u548c\u6ce8\u610f\u4e8b\u9879\u4e4b\u540e\uff0c\u6211\u60f3\u91cd\u70b9\u4ecb\u7ecd\u4ece svelte 4 \u8fc1\u79fb\u5230 svelte 5 \u65f6\u7684\u4e00\u4e9b\u6280\u5de7\u548c\u5fc3\u6001\u7684\u53d8\u5316\u3002 svelte 4 \u4f7f\u7528\u201c\u795e\u5947\u7684\u201d$: \u5e76\u8ba9\u548c\u5b8c\u6210\u6240\u6709\u7e41\u91cd\u7684\u5de5\u4f5c\u4ee5\u4f7f\u4ee3\u7801\u5177\u6709\u53cd\u5e94\u6027\u3002\u6211\u4eec\u8fd8\u63a5\u53d7\u4e86\u53d8\u91cf\u91cd\u65b0\u5206\u914d\uff0c\u4f8b\u5982 &lt;script&gt; let arr = [1, 2, 3] let value = 4 arr = [&#8230;arr, value] &lt;\/script&gt; \u767b\u5f55\u540e\u590d\u5236 \u800c\u4e0d\u662f\u66f4\u65b0\/\u6539\u53d8\u53d8\u91cf\u7684\u65b9\u6cd5\uff0c\u5982\u63a8\u9001\u7b49 \u6211\u5f88\u60ca\u8bb6\u80fd\u591f\u4f7f\u7528 svelte 5 \u91cd\u65b0\u5b66\u4e60\u597d\u7684\u65e7 js \u6a21\u5f0f\u3002 \u65e0\u9700\u4e00\u76f4\u4fdd\u6301\u53cd\u5e94 \u800c\u4e14\u6211\u53ef\u80fd\u4e5f\u88ab svelte 4 \u4e2d\u7684 let \u5ba0\u574f\u4e86\uff0c\u6ca1\u6709\u8003\u8651\u53cd\u5e94\u6027\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\u5b83\u4f1a\u88ab\u5305\u542b\u5728\u5185\u3002 \u4f46\u5e76\u975e\u6240\u6709\u53d8\u91cf\u90fd\u5fc5\u987b\u662f\u53cd\u5e94\u6027\u7684\u3002\u975e\u53cd\u5e94\u6027\u53d8\u91cf\u4e5f\u53ef\u4ee5\u5728\u53cd\u5e94\u6027\u751a\u81f3\u201c\u4f20\u7edf\u53d8\u5f02\u4ee3\u7801\u201d\u4e2d\u66f4\u65b0\u3002\u53cd\u5e94\u5f0f\u53d8\u91cf\u7684\u771f\u6b63\u9700\u8981\u662f\u5f53\u6211\u4eec\u5728 ui \u4e2d\u4f7f\u7528\u5b83\u65f6\uff08\u5f53\u8fd9\u4e2a\u53d8\u91cf\u5728 html\/\u9875\u9762\u4e2d\u5448\u73b0\u5e76\u4e14\u6211\u4eec\u9700\u8981\u5b83\u7a0d\u540e\u66f4\u65b0\u65f6\uff09\u3002 \u60a8\u53ef\u80fd\u4f1a\u5728 svelte [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3925","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3925"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3925\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}