svelte 相关问题


带有 Tailwind 风格的 Sveltekit 套件

我正在尝试使用 svelte 打包库创建一个 Svelte 组件包(您可以使用 npm create svelte@latest 并选择 svelte 库进行设置。然后我在


Svelte 中的单例

我正在尝试制作一个 Svelte 应用程序,其中我有一个类(不是 Svelte 组件)来创建音频源并管理它。 我想跨多个组件获取此类实例。 现在...


如何在 svelte 中创建 cookie 并访问它?

我正在尝试在 svelte 中创建一个 cookie(我也在使用 svelte 套件)并访问它。我想使用 cookie 进行身份验证,更具体地说,是存储 JWT 令牌。 我已经试过了


在 Svelte 中保持页面的页面滚动位置

我正在使用 Svelte,并尝试将滚动位置保持在单个页面上,例如当用户导航到另一个页面然后导航回(通过浏览器或导航栏返回)页面时...


为什么我无法使用 use:action Svelte 添加/删除主体类

我有这个功能 const bodyClass = (节点) => { if (open && !node.classList.contains("is-search-show")) { node.classList.add("is-search-show&quo...


在组件中添加传单的map.on('click', ...)会导致参数列表后出现语法错误:缺少 )

我在使用Leaflet和Svelte时遇到了一些奇怪的错误,我不确定这是否是我的错误。 我有一个封装我的 Leaflet 地图的组件,位于 Svelte 的 $lib 目录中。随时...


如何测量 Svelte 组件的像素高度?

我正在尝试测量组件的高度,但始终未定义。如果我在组件周围放置一个 html 容器,它会起作用,但这对我来说似乎不是正确的解决方案。 这是我的


如何以 Svelte 方式设置导出和开槽零件的样式?

Svelte slot 的功能是否像 vanilla-js/dom 一样(我似乎无法让它工作)。 在 html/js 中我可以这样做: 身体{颜色:红色;} /* 从外部设置暴露部分的样式 */ ...</desc> <question vote="0"> <p>Svelte <pre><code>slot</code></pre>的功能是否像 vanilla-js/dom 一样(我似乎无法让它工作)。</p> <p>在 html/js 中我可以做:</p> <p></p><div data-babel="false" data-lang="js" data-hide="false" data-console="true"> <div> <pre><code>&lt;style&gt; body {color: red;} /* style exposed part from outside */ my-element::part(header) {color: green;} &lt;/style&gt; &lt;h1&gt;Hello World&lt;/h1&gt; &lt;my-element&gt; &lt;div slot=&#34;header&#34;&gt;&lt;strong&gt;Header&lt;/strong&gt;&lt;/div&gt; &lt;div slot=&#34;body&#34;&gt;Body&lt;/div&gt; &lt;/my-element&gt; &lt;script&gt; customElements.define(&#39;my-element&#39;, class extends HTMLElement { constructor() { super(); const shadowRoot = this.attachShadow({mode: &#39;open&#39;}); shadowRoot.innerHTML = ` &lt;style&gt; .container { border: solid 1px blue; padding: 5px; position: relative; &amp;:after { content: &#34;my-element&#34;; display: block; position: absolute; bottom: -.5em; right: 5px; border: inherit; background-color: white; padding: inherit; } } /* style inserted/slotted part from inside */ [part=&#34;body&#34;] ::slotted(div) { background-color: lightyellow; } &lt;/style&gt; &lt;div class=&#34;container&#34;&gt; &lt;header part=&#34;header&#34;&gt;&lt;slot name=&#34;header&#34;&gt;&lt;/slot&gt;&lt;/header&gt; &lt;hr&gt; &lt;div part=&#34;body&#34;&gt;&lt;slot name=&#34;body&#34;&gt;&lt;/slot&gt;&lt;/div&gt; &lt;/div&gt; `; } }); &lt;/script&gt;</code></pre> </div> </div> <p></p> <p>其中 <pre><code>h1</code></pre> 的全局样式为红色,标有 <pre><code>part=&#34;header&#34;</code></pre> 的元素从外部设置为绿色,插入 <pre><code>slot=&#34;body&#34;</code></pre> 的内容从内部(shadow dom)设置为绿色有浅黄色背景。</p> <p>我不知道如何在 svelte 中执行任何这种(受控)跨界样式? (例如,当使用 <pre><code>AppShell::part(content)</code></pre> 时,我收到错误:</p> <pre><code>[plugin:vite-plugin-svelte] C:/srv/svelte/yoda5/src/routes/+layout.svelte:23:18 Expected a valid CSS identifier C:/srv/svelte/yoda5/src/routes/+layout.svelte:23:18 21 | 22 | &lt;style&gt; 23 | AppShell::part(content) { | ^ </code></pre> </question> <answer tick="false" vote="0"> <p>这里有关于<a href="https://learn.svelte.dev/tutorial/slots" rel="nofollow noreferrer">老虎机的课程</a>。最好参考该工具的文档。 Svelte 的学习网站非常棒。</p> <pre><code>// Slotted component, say Test.svelte. &lt;div class=&#34;private-parent&#34;&gt; &lt;slot /&gt; &lt;/div&gt; &lt;style&gt; div.private-parent { color: blue; } &lt;/style&gt; </code></pre> <p>然后,您使用该组件:</p> <pre><code>&lt;script&gt; import Test from &#39;./Test.svelte&#39;; &lt;/script&gt; &lt;Test&gt; &lt;!-- This is the slot&#39;s content --&gt; &lt;p class=&#34;outside-style&#34;&gt;Hello!&lt;/p&gt; &lt;/Test&gt; &lt;style&gt; p.outside-style { color: darkred; } &lt;/style&gt; </code></pre> </answer> </body></html>


如何处理 Vite 项目中同一个库的 3 个版本冲突?

我有以下奇怪的情况:我正在使用 Vite 和 svelte-ts 模板设置开发一个网络应用程序。在此应用程序中,我必须为基于 WebXR 的增强现实使用不同的库,这样您就可以


如何在页面加载时为 Svelte 组件设置动画而不导致布局移位

问题 我想在加载 SvelteKit 应用程序的登陆页面后对标题进行动画处理,导致标题的每个字母稍微出现在另一个之后,但我所提供的所有解决方案...


为什么 svelte 派生存储总是在 get() 上重新创建?

现在我想这更针对 Svelte 作者,但我最近才完全意识到派生商店是在获取时不断重新创建的。 例子 导入{导出、获取、写入...</desc> <question vote="0"> <p>现在我想这更针对 Svelte 作者,但我最近才完全意识到派生商店会在 <pre><code>get</code></pre> 上不断重新创建。</p> <p><a href="https://svelte.dev/repl/142e6716b65647f69f660613b39d0386?version=4.2.12" rel="nofollow noreferrer">示例</a></p> <pre><code>&lt;script&gt; import { derived, get, writable } from &#39;svelte/store&#39; const store = writable(0) const derivedA = derived(store, s =&gt; { console.log(&#39;derivedA recreated!&#39;) return { name: &#39;A&#39;, s } }) const derivedB = derived(derivedA, d =&gt; { console.log(&#39;derivedB recreated!&#39;) return { name: &#39;B&#39;, s: d.s } }) function getB() { console.log(get(derivedB)) } &lt;/script&gt; &lt;section class=&#34;mx-4 md:mx-0&#34;&gt; &lt;button on:click={getB}&gt;GetB&lt;/button&gt; &lt;/section&gt; </code></pre> <p>我认为它们只会在输入发生变化时才会重新创建 - 而不是每次调用 <pre><code>get</code></pre> 时都会重新创建。特别奇怪的是,如果派生存储被链接,则整个树都会被遍历。我假设 <pre><code>get</code></pre> 返回了对值的引用,当然,如果你那么愚蠢,你可能会变异并导致各种错误。</p> <p>我确实知道派生存储应该始终为相同的输入返回完全相同的值,但是如果<em>某人</em>没有时间深入思考它,则依赖于派生存储仅在原始存储更改时重新计算它会导致相当奇怪的错误。</p> </question> <answer tick="true" vote="1"> <p>来自文档</p> <blockquote> <p>从一个或多个其他商店派生出一个商店。 <strong>回调最初在第一个订阅者订阅时运行</strong>,然后每当存储依赖项发生变化时运行。</p> </blockquote> <blockquote> <p>...您可能需要检索您未订阅的商店的值。 <strong>get</strong> 允许您这样做。 <strong>这可以通过创建订阅</strong>、读取值,然后取消订阅来实现。</p> </blockquote> <p>在您的示例中,派生值未在任何地方使用,因此调用 <pre><code>get</code></pre> 创建第一个订阅者。添加时</p> <pre><code>{$derivedA} {$derivedB} </code></pre> <p>日志将在组件初始化时运行,而在调用 <pre><code>get</code></pre></p> 时不再运行 </answer> </body></html>


svelte:第一次加载时窗口内部宽度未定义

我有这个组件来检查设备大小 从“$lib/stores”导入{deviceSize}; 让内部宽度; $:如果(内部宽度> = 1652){ ...</desc> <question vote="0"> <p>我有这个组件来检查设备尺寸</p> <pre><code>&lt;script lang=&#34;ts&#34;&gt; import { deviceSize } from &#34;$lib/stores&#34;; let innerWidth; $: if (innerWidth &gt;= 1652) { $deviceSize = { xl: true, lg: false, md: false, dsm: false, sm: false, }; } else if (innerWidth &gt;= 1240 &amp;&amp; innerWidth &lt; 1652) { $deviceSize = { xl: false, lg: true, md: false, dsm: false, sm: false, }; } else if (innerWidth &gt;= 794 &amp;&amp; innerWidth &lt; 1240) { $deviceSize = { xl: false, lg: false, md: true, dsm: false, sm: false, }; } else if (innerWidth &gt;= 640 &amp;&amp; innerWidth &lt; 794) { $deviceSize = { xl: false, lg: false, md: false, dsm: true, sm: false, }; } else { $deviceSize = { xl: false, lg: false, md: false, dsm: false, sm: true, }; } $: console.log(innerWidth); &lt;/script&gt; &lt;svelte:window bind:innerWidth /&gt; </code></pre> <p>和像这样的应用程序组件</p> <p><App.svelte></p> <pre><code>&lt;script&gt; const { lg, xl } = $deviceSize; $: isDesktop = xl || lg; &lt;/script&gt; {#if isDesktop} &lt;DesktopComponent/&gt; {/if} {#if !isDesktop} &lt;MobileComponent/&gt; {/if} </code></pre> <p><a href="https://i.stack.imgur.com/6iNXn.png" target="_blank"><img src="https://cdn.txt58.com/i/AWkuc3RhY2suaW1ndXIuY29tLzZpTlhuLnBuZw==" alt="enter image description here"/></a></p> <p>我的问题是innerWidth在初始加载中总是未定义。所以 isDesktop = false,那么即使我使用桌面,也始终渲染 MobileComponent。请帮我解决这个问题。</p> <p>我尝试为 <pre><code>deviceSize</code></pre> 商店设置默认值,但无法按我想要的方式工作,它始终呈现为我使用的任何设备(PC、移动设备)的默认条件。</p> </question> <answer tick="false" vote="0"> <p>根据<a href="https://svelte.dev/docs/svelte-components#:%7E:text=Reactive%20statements%20run%20after%20other%20script%20code%20and%20before%20the%20component%20markup%20is%20rendered%2C" rel="nofollow noreferrer">svelte 文档</a>:</p> <blockquote> <p>反应式语句在其他脚本代码之后、渲染组件标记之前运行</p> </blockquote> <p>意味着 if-else 块在创建 <pre><code>svelte:window</code></pre> 绑定之前运行一次,此时 <pre><code>innerWidth</code></pre> 未定义。</p> <p>为了避免这种情况,您可以将 <pre><code>innerWidth</code></pre> 初始化为正确的值,例如更换</p> <pre><code>let innerWidth; </code></pre> <p>与</p> <pre><code>let innerWidth = window.innerWidth; </code></pre> <p>也就是说,通过使用 <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Using_media_queries" rel="nofollow noreferrer">CSS 媒体查询</a>(而不是 JavaScript)来显示和隐藏标记,您可能会让您的生活变得更轻松。</p> </answer> </body></html>


ESLint:解析错误:意外的标记:

大家好我正在将我的 vue3 项目从 js 迁移到 typescript,我遇到了这个问题: 这是我在 .vue 文件中的代码 const toto = (msg: string) => { </desc> <question vote="7"> <p>大家好,我正在将我的 vue3 项目从 js 迁移到 typescript,我遇到了这个问题:</p> <p><a href="https://i.stack.imgur.com/y5tG8.png" target="_blank"><img src="https://cdn.txt58.com/i/AWkuc3RhY2suaW1ndXIuY29tL3k1dEc4LnBuZw==" alt=""/></a></p> <p>这是我在 .vue 文件中的代码</p> <pre><code>&lt;script setup lang=&#34;ts&#34;&gt; const toto = (msg: string) =&gt; { console.log(msg) } &lt;/script&gt; </code></pre> <p>这是我的 eslintrc.js</p> <pre><code>module.exports = { &#39;env&#39;: { &#39;browser&#39;: true, &#39;es2021&#39;: true }, &#39;extends&#39;: [ &#39;eslint:recommended&#39;, &#39;plugin:vue/vue3-essential&#39; ], &#39;parserOptions&#39;: { &#39;ecmaVersion&#39;: 13, &#39;sourceType&#39;: &#39;module&#39; }, &#39;plugins&#39;: [ &#39;vue&#39; ], &#39;rules&#39;: { &#39;vue/multi-word-component-names&#39;: &#39;off&#39;, &#39;vue/object-curly-spacing&#39;: [2, &#39;always&#39;], &#39;vue/html-closing-bracket-spacing&#39;: [2, { &#39;selfClosingTag&#39;: &#39;always&#39; }], &#39;vue/max-attributes-per-line&#39;: [2, { &#39;singleline&#39;: { &#39;max&#39;: 1 }, &#39;multiline&#39;: { &#39;max&#39;: 1 } }], &#39;semi&#39;: [2, &#39;never&#39;] } } </code></pre> </question> <answer tick="true" vote="10"> <p>您需要配置 eslint 以支持 typescript,因为 eslint 不支持开箱即用。 首先,您需要安装<a href="https://www.npmjs.com/package/@typescript-eslint/parser" rel="nofollow noreferrer">@typescript-eslint/parser</a>,然后安装<a href="https://www.npmjs.com/package/@typescript-eslint/eslint-plugin" rel="nofollow noreferrer">@typescript-eslint/eslint-plugin</a>。 安装完这些后,请按如下方式更新您的配置 - </p> <pre><code>module.exports = { &#39;env&#39;: { &#39;browser&#39;: true, &#39;es2021&#39;: true, node: true }, &#39;extends&#39;: [ &#39;eslint:recommended&#39;, &#39;plugin:vue/vue3-essential&#39; ], &#39;parserOptions&#39;: { &#39;ecmaVersion&#39;: 12, &#39;sourceType&#39;: &#39;module&#39;, parser: &#39;@typescript-eslint/parser&#39; }, &#39;plugins&#39;: [ &#39;vue&#39;, &#39;@typescript-eslint&#39; ], &#39;rules&#39;: { &#39;vue/multi-word-component-names&#39;: &#39;off&#39;, &#39;vue/object-curly-spacing&#39;: [2, &#39;always&#39;], &#39;vue/html-closing-bracket-spacing&#39;: [2, { &#39;selfClosingTag&#39;: &#39;always&#39; }], &#39;vue/max-attributes-per-line&#39;: [2, { &#39;singleline&#39;: { &#39;max&#39;: 1 }, &#39;multiline&#39;: { &#39;max&#39;: 1 } }], &#39;semi&#39;: [2, &#39;never&#39;] } } </code></pre> </answer> <answer tick="false" vote="1"> <p>就我而言,问题是我使用解析器选项作为数组,而不是字符串:</p> <pre><code> parserOptions: { - parser: [&#39;@typescript-eslint/parser&#39;], + parser: &#39;@typescript-eslint/parser&#39;, }, </code></pre> </answer> <answer tick="false" vote="0"> <p>如果你在项目中同时使用 JS 和 TS,此配置有帮助</p> <pre><code> overrides: [ { files: [&#39;*.vue&#39;], parser: &#39;svelte-eslint-parser&#39;, parserOptions: { parser: { // Specify a parser for each lang. ts: &#39;@typescript-eslint/parser&#39;, js: &#39;espree&#39;, typescript: &#39;@typescript-eslint/parser&#39; } } } ], </code></pre> </answer> <answer tick="false" vote="-1"> <p>我在节点 v12.22.9 上遇到了这个问题。通过升级到 v14.21.2,我不再遇到解析错误。您可以使用命令升级/安装</p> <pre><code>nvm install v14.21.2 </code></pre> </answer> </body></html>


© www.soinside.com 2019 - 2024. All rights reserved.