我正在尝试使用 svelte 打包库创建一个 Svelte 组件包(您可以使用 npm create svelte@latest 并选择 svelte 库进行设置。然后我在
我正在尝试制作一个 Svelte 应用程序,其中我有一个类(不是 Svelte 组件)来创建音频源并管理它。 我想跨多个组件获取此类实例。 现在...
我正在尝试在 svelte 中创建一个 cookie(我也在使用 svelte 套件)并访问它。我想使用 cookie 进行身份验证,更具体地说,是存储 JWT 令牌。 我已经试过了
我的下面的代码有问题,我有命运之轮。但是,我只想旋转一圈。 这段代码 感谢帮助 // 过程 box.style.setProperty("transition", "轻松 5...
首页 &... <div class="menu-bar"> <ul> <li><a href="/Home/index.html"> <i class="fa fa-house"></i>Home</a> </i></li> < ul > </div> <style> .menu-bar ul li a:hover { color: black; transition: all 0.2s; border-inline-end: 2px solid; border-block-end: 2px solid; </style> 我想从 fa fa-house 的 cdn 图标中删除 :hover 效果。 <div class="menu-bar"> <ul> <li> <i class="fa fa-home"></i> <a href="/Home/index.html"> Home </a> </li> </ul> </div> <style> .menu-bar ul li a:hover { color: black; transition: all 0.2s; border-inline-end: 2px solid; border-block-end: 2px solid; } .menu-bar ul li i{ pointer-events: none; } </style> <div class="menu-bar"> <ul> <li><i class="fa fa-house"></i><a href="/Home/index.html"> Home</a> </i></li> <ul > 只需从 <i> 标签中取出 <a> 元素(图标)即可。 你可以试试这个:- <div class="menu-bar"> <ul> <li><i class="fa fa-house"><a href="/Home/index.html"> </i>Home</a> </i></li> </ul> </div> <style> .menu-bar ul li a:hover { color: black; transition: all 0.2s; border-inline-end: 2px solid; border-block-end: 2px solid; } </style>
我正在使用 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 目录中。随时...
<div class="flex flex-col items-center justify-center rounded-xl bg-[#101638] px-5 pb-8 pt-7"> <div class="relative mb-5 cursor-pointer"> <img class="mx-auto max-w-[90px] rounded-full p-1 ring ring-offset-8 ring-offset-[#101638] hover:ring-offset-0 ring-[#133e8d] md:max-w-[114px]" src="/images/Avatar1.png" alt="Bordered avatar"> <div class="absolute bottom-1 right-2 rounded-full bg-blue-600 px-2 py-1 ring-4 ring-[#0d1129]"> <svg class="svg-inline--fa fa-message-dots text-white" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="message-dots" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"> <path class="" fill="currentColor" d="M0 64C0 28.7 28.7 0 64 0H448c35.3 0 64 28.7 64 64V352c0 35.3-28.7 64-64 64H309.3L185.6 508.8c-4.8 3.6-11.3 4.2-16.8 1.5s-8.8-8.2-8.8-14.3V416H64c-35.3 0-64-28.7-64-64V64zM128 240a32 32 0 1 0 0-64 32 32 0 1 0 0 64zm128 0a32 32 0 1 0 0-64 32 32 0 1 0 0 64zm160-32a32 32 0 1 0 -64 0 32 32 0 1 0 64 0z"></path> </svg> </div> </div> <div> <h1 class="font-bold">First Name</h1> </div> <div> <h1>Name</h1> </div> <div> <h1 class="text-[#636da8]">Project Mngr</h1> </div> </div> 如何为任何 css/tailwind 解决方案的偏移环的过渡设置动画? 例如,这 2 个缩略图,左边的缩略图悬停在其上,右边的缩略图是初始状态。我希望它在缩略图悬停时有一个缩小的动画,而不是我当前的动画,因为它非常突然 您可以考虑应用包含 transition-property 的 box-shadow CSS 值,例如 transition 或 transition-all Tailwind 类,以及非 0 transition-duration,上述类名也设置了: <script src="https://cdn.tailwindcss.com/3.4.1"></script> <div class="flex flex-col items-center justify-center rounded-xl bg-[#101638] px-5 pb-8 pt-7"> <div class="relative mb-5 cursor-pointer"> <img class="mx-auto max-w-[90px] rounded-full p-1 ring ring-offset-8 ring-offset-[#101638] hover:ring-offset-0 ring-[#133e8d] md:max-w-[114px] transition" src="https://picsum.photos/90/90" alt="Bordered avatar"> <div class="absolute bottom-1 right-2 rounded-full bg-blue-600 px-2 py-1 ring-4 ring-[#0d1129]"> <svg class="svg-inline--fa fa-message-dots text-white" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="message-dots" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"> <path class="" fill="currentColor" d="M0 64C0 28.7 28.7 0 64 0H448c35.3 0 64 28.7 64 64V352c0 35.3-28.7 64-64 64H309.3L185.6 508.8c-4.8 3.6-11.3 4.2-16.8 1.5s-8.8-8.2-8.8-14.3V416H64c-35.3 0-64-28.7-64-64V64zM128 240a32 32 0 1 0 0-64 32 32 0 1 0 0 64zm128 0a32 32 0 1 0 0-64 32 32 0 1 0 0 64zm160-32a32 32 0 1 0 -64 0 32 32 0 1 0 64 0z"></path> </svg> </div> </div> <div> <h1 class="font-bold">First Name</h1> </div> <div> <h1>Name</h1> </div> <div> <h1 class="text-[#636da8]">Project Mngr</h1> </div> </div> 否则,您可以考虑通过 transition-property: box-shadow Tailwind 类应用 transition-duration: 150ms; 和 transition-[box-shadow](以及其他属性)以仅转换 box-shadow: <script src="https://cdn.tailwindcss.com/3.4.1"></script> <div class="flex flex-col items-center justify-center rounded-xl bg-[#101638] px-5 pb-8 pt-7"> <div class="relative mb-5 cursor-pointer"> <img class="mx-auto max-w-[90px] rounded-full p-1 ring ring-offset-8 ring-offset-[#101638] hover:ring-offset-0 ring-[#133e8d] md:max-w-[114px] transition-[box-shadow]" src="https://picsum.photos/90/90" alt="Bordered avatar"> <div class="absolute bottom-1 right-2 rounded-full bg-blue-600 px-2 py-1 ring-4 ring-[#0d1129]"> <svg class="svg-inline--fa fa-message-dots text-white" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="message-dots" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"> <path class="" fill="currentColor" d="M0 64C0 28.7 28.7 0 64 0H448c35.3 0 64 28.7 64 64V352c0 35.3-28.7 64-64 64H309.3L185.6 508.8c-4.8 3.6-11.3 4.2-16.8 1.5s-8.8-8.2-8.8-14.3V416H64c-35.3 0-64-28.7-64-64V64zM128 240a32 32 0 1 0 0-64 32 32 0 1 0 0 64zm128 0a32 32 0 1 0 0-64 32 32 0 1 0 0 64zm160-32a32 32 0 1 0 -64 0 32 32 0 1 0 64 0z"></path> </svg> </div> </div> <div> <h1 class="font-bold">First Name</h1> </div> <div> <h1>Name</h1> </div> <div> <h1 class="text-[#636da8]">Project Mngr</h1> </div> </div>
我正在尝试测量组件的高度,但始终未定义。如果我在组件周围放置一个 html 容器,它会起作用,但这对我来说似乎不是正确的解决方案。 这是我的
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><style> body {color: red;} /* style exposed part from outside */ my-element::part(header) {color: green;} </style> <h1>Hello World</h1> <my-element> <div slot="header"><strong>Header</strong></div> <div slot="body">Body</div> </my-element> <script> customElements.define('my-element', class extends HTMLElement { constructor() { super(); const shadowRoot = this.attachShadow({mode: 'open'}); shadowRoot.innerHTML = ` <style> .container { border: solid 1px blue; padding: 5px; position: relative; &:after { content: "my-element"; display: block; position: absolute; bottom: -.5em; right: 5px; border: inherit; background-color: white; padding: inherit; } } /* style inserted/slotted part from inside */ [part="body"] ::slotted(div) { background-color: lightyellow; } </style> <div class="container"> <header part="header"><slot name="header"></slot></header> <hr> <div part="body"><slot name="body"></slot></div> </div> `; } }); </script></code></pre> </div> </div> <p></p> <p>其中 <pre><code>h1</code></pre> 的全局样式为红色,标有 <pre><code>part="header"</code></pre> 的元素从外部设置为绿色,插入 <pre><code>slot="body"</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 | <style> 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. <div class="private-parent"> <slot /> </div> <style> div.private-parent { color: blue; } </style> </code></pre> <p>然后,您使用该组件:</p> <pre><code><script> import Test from './Test.svelte'; </script> <Test> <!-- This is the slot's content --> <p class="outside-style">Hello!</p> </Test> <style> p.outside-style { color: darkred; } </style> </code></pre> </answer> </body></html>
我有以下奇怪的情况:我正在使用 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><script> import { derived, get, writable } from 'svelte/store' const store = writable(0) const derivedA = derived(store, s => { console.log('derivedA recreated!') return { name: 'A', s } }) const derivedB = derived(derivedA, d => { console.log('derivedB recreated!') return { name: 'B', s: d.s } }) function getB() { console.log(get(derivedB)) } </script> <section class="mx-4 md:mx-0"> <button on:click={getB}>GetB</button> </section> </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>
我有这个组件来检查设备大小 从“$lib/stores”导入{deviceSize}; 让内部宽度; $:如果(内部宽度> = 1652){ ...</desc> <question vote="0"> <p>我有这个组件来检查设备尺寸</p> <pre><code><script lang="ts"> import { deviceSize } from "$lib/stores"; let innerWidth; $: if (innerWidth >= 1652) { $deviceSize = { xl: true, lg: false, md: false, dsm: false, sm: false, }; } else if (innerWidth >= 1240 && innerWidth < 1652) { $deviceSize = { xl: false, lg: true, md: false, dsm: false, sm: false, }; } else if (innerWidth >= 794 && innerWidth < 1240) { $deviceSize = { xl: false, lg: false, md: true, dsm: false, sm: false, }; } else if (innerWidth >= 640 && innerWidth < 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); </script> <svelte:window bind:innerWidth /> </code></pre> <p>和像这样的应用程序组件</p> <p><App.svelte></p> <pre><code><script> const { lg, xl } = $deviceSize; $: isDesktop = xl || lg; </script> {#if isDesktop} <DesktopComponent/> {/if} {#if !isDesktop} <MobileComponent/> {/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>
在 Sveltekit/Typescript 中使用 pdfjs
我需要在 sveltekit 4 typescript 项目中使用 pdfjs (当前 4.1.392)进行文本提取。这就是我尝试将 pdfjs 导入到我的 src/routes/+page.svelte 中的方法: ...</desc> <question vote="0"> <p>我需要在 sveltekit 4 typescript 项目中使用 pdfjs(当前版本 4.1.392)进行文本提取。这就是我尝试将 pdfjs 导入到我的 src/routes/+page.svelte 中的方法:</p> <pre><code><script lang="ts"> import * as pdfjs from 'pdfjs-dist/build/pdf'; import pdfjsWorker from 'pdfjs-dist/build/pdf.worker'; pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker; </script> </code></pre> <p>但是,我收到错误消息“无效的'workerSrc'类型”。</p> <p>如何在 sveltekit/typescript 中使用 pdfjs?</p> </question> <answer tick="false" vote="0"> <p>你可以尝试做这样的事情:</p> <pre><code>import * as pdfjs from "pdfjs-dist"; pdfjs.GlobalWorkerOptions.workerSrc = new URL("pdfjs-dist/build/pdf.worker.mjs", import.meta.url); </code></pre> <p>更多使用示例,可以访问PDF.js Github仓库: <a href="https://github.com/mozilla/pdf.js#online-demo" rel="nofollow noreferrer">https://github.com/mozilla/pdf.js#online-demo</a></p> </answer> <answer tick="true" vote="0"> <p>以下代码适用于 pdfjs-dist 4.2.67、svelte 4.2.15 和 typescript 5.4.5:</p> <pre><code><script lang="ts"> // @ts-nocheck import * as pdfjs from 'pdfjs-dist'; import * as pdfWorker from 'pdfjs-dist/build/pdf.worker.mjs'; pdfjs.GlobalWorkerOptions.workerSrc = import.meta.url + 'pdfjs-dist/build/pdf.worker.mjs'; </script> </code></pre> </answer> </body></html>
大家好我正在将我的 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><script setup lang="ts"> const toto = (msg: string) => { console.log(msg) } </script> </code></pre> <p>这是我的 eslintrc.js</p> <pre><code>module.exports = { 'env': { 'browser': true, 'es2021': true }, 'extends': [ 'eslint:recommended', 'plugin:vue/vue3-essential' ], 'parserOptions': { 'ecmaVersion': 13, 'sourceType': 'module' }, 'plugins': [ 'vue' ], 'rules': { 'vue/multi-word-component-names': 'off', 'vue/object-curly-spacing': [2, 'always'], 'vue/html-closing-bracket-spacing': [2, { 'selfClosingTag': 'always' }], 'vue/max-attributes-per-line': [2, { 'singleline': { 'max': 1 }, 'multiline': { 'max': 1 } }], 'semi': [2, 'never'] } } </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 = { 'env': { 'browser': true, 'es2021': true, node: true }, 'extends': [ 'eslint:recommended', 'plugin:vue/vue3-essential' ], 'parserOptions': { 'ecmaVersion': 12, 'sourceType': 'module', parser: '@typescript-eslint/parser' }, 'plugins': [ 'vue', '@typescript-eslint' ], 'rules': { 'vue/multi-word-component-names': 'off', 'vue/object-curly-spacing': [2, 'always'], 'vue/html-closing-bracket-spacing': [2, { 'selfClosingTag': 'always' }], 'vue/max-attributes-per-line': [2, { 'singleline': { 'max': 1 }, 'multiline': { 'max': 1 } }], 'semi': [2, 'never'] } } </code></pre> </answer> <answer tick="false" vote="1"> <p>就我而言,问题是我使用解析器选项作为数组,而不是字符串:</p> <pre><code> parserOptions: { - parser: ['@typescript-eslint/parser'], + parser: '@typescript-eslint/parser', }, </code></pre> </answer> <answer tick="false" vote="0"> <p>如果你在项目中同时使用 JS 和 TS,此配置有帮助</p> <pre><code> overrides: [ { files: ['*.vue'], parser: 'svelte-eslint-parser', parserOptions: { parser: { // Specify a parser for each lang. ts: '@typescript-eslint/parser', js: 'espree', typescript: '@typescript-eslint/parser' } } } ], </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>