简体   繁体   English

将全局 CSS 应用于注入的 HTML (v-html)

[英]Applying global CSS to injected HTML (v-html)

I'm trying to inject HTML from my database using Vue.我正在尝试使用 Vue 从我的数据库中注入 HTML。 I'm able to successfully inject HTML however the problem I'm having is my framework stylesheet (Vuetify) isn't being applied.我能够成功注入 HTML,但是我遇到的问题是我的框架样式表(Vuetify)没有被应用。
It's applied to the rest of the page just not the injected HTML.它应用于页面的其余部分,而不是注入的 HTML。

How can I get the injected HTML to use my Vuetify CSS?如何获取注入的 HTML 以使用我的 Vuetify CSS?

Also, the images in my HTML aren't rendered either although I can see them when inspecting the HTML.此外,虽然我在检查 HTML 时可以看到它们,但我的 HTML 中的图像也没有呈现。

<div v-html="post.format"></div>

Output HTML:输出 HTML:

<div>
  <v-container grid-list-md="" text-xs-center="">
    <v-layout row="" wrap="">
      <v-flex xs12="" md12="" lg6="">
        <p class="text-lg-left">Post description text</p>
      </v-flex>
      <v-flex xs12="" md12="" lg6="">
        <v-img class="center" style="max-width:250px" src="/img/1.jpg"></v-img>
      </v-flex>
    </v-layout>

  </v-container>

  <v-container grid-list-md="" text-xs-center="">
    <v-layout row="" wrap="">
      <v-flex xs12="" md12="" lg6="">
        <v-img width="250px" class="center" src="/img/2.jpg">
        </v-img>
      </v-flex>
      <v-flex xs12="" md12="" lg6="">
        <p class="text-lg-left">More post description text</p>
      </v-flex>
    </v-layout>
  </v-container>
</div>

As @Husam mentioned, Vue won't allow you to do this.正如@Husam 提到的,Vue 不允许你这样做。 It's ultimately for the best, and even v-html should be used with caution.它最终是最好的,甚至应该谨慎使用v-html。 Regarding the css, from the Vue.js site :关于 css,来自 Vue.js 网站

In single-file components, scoped styles will not apply to content inside v-html, because that HTML is not processed by Vue's template compiler.在单文件组件中,作用域样式不适用于 v-html 中的内容,因为 Vue 的模板编译器不会处理该 HTML。 If you want to target v-html content with scoped CSS, you can instead use CSS modules or an additional, global element with a manual scoping strategy such as BEM.如果您想使用范围 CSS 来定位 v-html 内容,您可以改用 CSS 模块或具有手动范围策略(例如 BEM)的附加全局元素。

As for a better way, judging from the look of your output, I'd suggest storing your straight-text data in a database/collection, and making a component template for this type of post.至于更好的方法,从您的输出外观来看,我建议将您的纯文本数据存储在数据库/集合中,并为此类帖子制作组件模板。 In your database/collection, you could store the title, description, imageURL, and perhaps a componentType.在你的数据库/集合中,你可以存储标题、描述、imageURL,也许还有一个componentType。 Then, when rendering, you could render the particular component type from what was stored in the db.然后,在渲染时,您可以从 db 中存储的内容中渲染特定的组件类型。

That would be cleaner, safer, and perhaps easier to maintain.这样会更清洁、更安全,也许更容易维护。 I created a sample of what I'm talking about here on CodeSandbox.我在 CodeSandbox 上创建了我正在谈论的内容的示例。 The sandbox is preventing the images from loading but it's there.沙盒正在阻止图像加载,但它就在那里。 I split your output into two different components to see also.我也将您的输出分成两个不同的组件来查看。

编辑 Vue 模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM