【问题标题】:svelte style HTML that was appended to a page附加到页面的苗条样式 HTML
【发布时间】:2021-05-22 00:13:58
【问题描述】:

我有一个苗条的文件,它以字符串格式(HTML 标记)获取数据:

  <script>
    import { onMount } from 'svelte';
    import { loadData } from '../stores.js';
    
    let content
    
    onMount(async() => {
        content = await loadData();
    })
  </script>

我随后将这些数据附加到页面:

    <div>
      {#await content}
        <p>waiting for content...</p>
      {:then content}
        {@html content}
      {/await}
      <h1>Test heading</h1>
    </div>

但是即使有样式:

    <style>
      h1 {
        font-size: 5rem;
        font-style: oblique;
      }
    
      p {
        font-size: 2rem;
        font-style: oblique;
      }
    </style>

转换为 html 的附加字符串显然不是用适当的类生成的:

    <div class="svelte-1a8cxpf"> 
      <h1 class="svelte-1a8cxpf">Test heading</h1>
      <h1>Appended heading - not styled</h1> <!-- here is no style -->
    </div>

有什么办法,如何让样式也非分类项目,或者说在那个特定的 svelte 文件中创建更通用的 css 规则?

【问题讨论】:

    标签: svelte


    【解决方案1】:

    this github issue 回答您的问题。你要做的是

    <style global>
    /*styles*/
    </style>
    

    <style>
    a:global {
     /* styles */
    }
    </style>
    

    【讨论】:

    • div :global(p) 和 div :global(h1) 对我有帮助。
    猜你喜欢
    • 2021-01-31
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 2018-08-23
    相关资源
    最近更新 更多