Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing html into Vue component

At the moment I pass some parameters into a vue component

 <Slider
      :images= "['/img/work/slide2.png', '/img/work/slide2.png', '/img/work/slide3.png']"
      :html="['<div>hello</div>', '<div>goodbye</div>']"
    </Slider>

The slider is either an 'html' slider or one with images.

This works fine although the html I pass in is going to get a lot more complex, maybe 30 lines and this will be harder to read and manage as params.

Can I pass in an external reference and pull that into the component?

<div v-for="content in html">
    <div class="work-slide">{{ content }}</div>
</div>

As you can see the loop in the component is a very simple v-for.

like image 540
LeBlaireau Avatar asked Jul 05 '17 10:07

LeBlaireau


People also ask

Why should I use Vue instead of HTML?

It's common for an app to be organized into a tree of nested components: This is very similar to how we nest native HTML elements, but Vue implements its own component model that allow us to encapsulate custom content and logic in each component. Vue also plays nicely with native Web Components.

What is a Vue component?

When not using a build step, a Vue component can be defined as a plain JavaScript object containing Vue-specific options: The template is inlined as a JavaScript string here, which Vue will compile on the fly.

How do I export components from a Vue template?

You can also use an ID selector pointing to an element (usually native <template> elements) - Vue will use its content as the template source. The example above defines a single component and exports it as the default export of a .js file, but you can use named exports to export multiple components from the same file.

How to use special is attribute in Vue?

We can use the special is attribute as a workaround: When used on native HTML elements, the value of is must be prefixed with vue: in order to be interpreted as a Vue component. This is required to avoid confusion with native customized built-in elements.


2 Answers

You can inject raw html into Vue components with the v-html directive.

like image 137
David Mold Avatar answered Oct 22 '22 13:10

David Mold


Don't pass HTML using attributes but using Slots:

Suppose we have a component called my-component with the following template:

<div>
  <h2>I'm the child title</h2>
  <slot>
    This will only be displayed if there is no content
    to be distributed.
  </slot>
</div>

And a parent that uses the component:

<div>
  <h1>I'm the parent title</h1>
  <my-component>
    <p>This is some original content</p>
    <p>This is some more original content</p>
  </my-component>
</div>

The rendered result will be:

<div>
  <h1>I'm the parent title</h1>
  <div>
    <h2>I'm the child title</h2>
    <p>This is some original content</p>
    <p>This is some more original content</p>
  </div>
</div>

You can also use Named Slots if you want to pass more than one field containing HTML.

like image 31
str Avatar answered Oct 22 '22 15:10

str