【问题标题】:How to get tw-in-js/twind plugins to work in Svelte如何让 tw-in-js/twind 插件在 Svelte 中工作
【发布时间】:2021-04-28 22:07:55
【问题描述】:

我在 Svelte 中使用 tw-in-js/twind。 当使用 Svelte App 的 vanilla 设置并包含 Twind 后,我找不到任何方法让插件工作:

<script>
    import {tw, apply, setup} from 'twind/css';

    setup ({
        plugins: {
            h1style: apply`
                uppercase
                text-6xl
                font-thin
            `,
        },
    });

    const h1style2 = apply `uppercase text-6xl font-thin`;

    export let name;
</script>

<main>
    <h1 class={tw`h1style`}>Hello {name}!</h1>
    <p>Visit the <a href="https://svelte.dev/tutorial" class={tw`italic`}>Svelte tutorial</a> to learn how to build Svelte apps.</p>
</main>

在上面的代码示例中,h1style2(const 声明)有效,但插件 h1style 无效。

如果有人可以让它工作,我将不胜感激。

【问题讨论】:

    标签: svelte tailwind-css twind tailwind-in-js


    【解决方案1】:

    我在 Svelte Repl 中尝试了您的示例,它似乎有效:https://svelte.dev/repl/e153da51e31a4b6c90cce63d9b6b3194?version=3.31.2

    在这里您可以找到我们的示例苗条项目:https://github.com/tw-in-js/example-svelte

    需要注意的一点是,setup 应该位于 app initializer 中的组件之外或 &lt;script context="module"&gt; 中。这样可以确保每个应用只调用一次 setup

    【讨论】:

    • 感谢@sastan 回答我的问题。看来我可能有环境问题。它对我不起作用。我什至下载了 repl 的 zip,但这也不起作用。我在 Windows 10 上运行,一旦放入“设置”,我就会在浏览器中看到一个空白页面 - 任何地方都没有错误 - 我现在正试图找出原因。正如我上面所说,它适用于 const appraoch,但在使用 setup 时失败(我也在 main.js 中尝试过
    • 好的,再次感谢 - 奇怪的行为来自潜在的混乱配置。我的 package.json 有依赖 twind:"^0.14.1"。据我所知,这不应该是一个问题。我运行 npm install twind 将其带到 0.14.2 并将依赖项更改为“最新”(与来自 twind git 站点的示例 svelte-snowpack 一致)。我将导入和设置移到 main.js 的顶部,并且由于 npm install 它现在可以工作了。
    猜你喜欢
    • 2013-09-06
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2012-08-27
    相关资源
    最近更新 更多