Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-closing component tags #1036

Closed
karevn opened this issue Jul 11, 2015 · 21 comments
Closed

Self-closing component tags #1036

karevn opened this issue Jul 11, 2015 · 21 comments

Comments

@karevn
Copy link

karevn commented Jul 11, 2015

How about allowing self-closed component tags like <my-component />? Currently, it does not work.

@yyx990803
Copy link
Member

Vue templates need to be valid HTML. There are no "self closing tags" in HTML5, it's an XHTML syntax which is now outdated and you should never use it. See http://stackoverflow.com/questions/3558119/are-self-closing-tags-valid-in-html5

@CodeOfDavid
Copy link

I didn't see this in the component documentation, which caused me a little time wasting. It would be good to add it.

@ediamin
Copy link

ediamin commented Jan 15, 2016

How about set an option like void_element: true to allow this? Like this:

<div id="demo">
    <my-component>
</div>

<script>
var MyComponent = Vue.extend({
  template: '<div>A custom component!</div>',
  void_element: true // allow the template to be a void element
});

Vue.component('my-component', MyComponent)
</script>

@CodeOfDavid
Copy link

I think yyx990803 is saying something far more fundamental. He doesn't want to go against HTML5

@karevn
Copy link
Author

karevn commented Jan 18, 2016

@ediamin actually, @CodeOfDavid he actually can't go against HTML5, as Vue uses browser's built-in HTML parser. I did not know that when posted this issue.

indirectlylit added a commit to indirectlylit/kolibri that referenced this issue Jun 13, 2016
@asumaran
Copy link

it would be great if this is mentioned somewhere in the documentation.

@phanan
Copy link
Member

phanan commented Jul 22, 2016

@asumaran See http://vuejs.org/guide/components.html#Template-Parsing:

Vue.js template engine is DOM-based and uses native parser that comes with the browser instead of providing a custom one. There are benefits to this approach when compared to string-based template engines, but there are also caveats. Templates have to be individually valid pieces of HTML.

@harbulot
Copy link

@yyx990803

Vue templates need to be valid HTML. There are no "self closing tags" in HTML5, it's an XHTML syntax which is now outdated and you should never use it. See http://stackoverflow.com/questions/3558119/are-self-closing-tags-valid-in-html5

Using tags that are self-closing is not invalid HTML5, though. (As far as I know, <br /> is valid HTML5, for example.) Even the SO question you link to doesn't say they should never be used.
Being able to handle valid XHTML (or at least well-formed XML) can actually be quite important, since there are still a few good server-side template engines and other tools that rely on XML (and XHTML). XML can actually help with structure and pre/post-processing (depending on the overall build environment).

@fnlctrl
Copy link
Member

fnlctrl commented Dec 13, 2016

@harbulot FYI self-closing tags works in 2.0 as long as you don't use in-dom templates.

@alexsasharegan
Copy link

@fnlctrl Is this because templates are converted to render functions? So we can use self-closing tags when compiling vue (single file components)?

@RobertBColton
Copy link

@alexsasharegan I too would like to know the answer to your question.

@Akryum
Copy link
Member

Akryum commented Jul 25, 2017

It works with vue-loader and .vue files.

@RobertBColton
Copy link

I see, thank you, now I suppose part of the confusion results from use of the term "string template"

The documentation seems to be inconsistent because the following implies that they are only templates defined as string literals:
https://vuejs.org/v2/guide/single-file-components.html

However, the following defines a string template as a single-file component too:
https://vuejs.org/v2/guide/components.html#DOM-Template-Parsing-Caveats

It seems as though a number of individuals are confused by this:
https://www.google.com/#q=vuejs+%22string+templates%22

Perhaps the docs could be changed to kind of clear up this confusion?

@Akryum
Copy link
Member

Akryum commented Jul 25, 2017

It should also work with the template-compiler included in the full build of Vue that compiles string templates into render functions (which is the same code used by vue-loader). Basically, the issue the OP had is the browser parsing the HTML before Vue had a chance to read it when using inline templates.

Vue.component('test', {
  template: '<div><my-component/></div>',
})

@LinusBorg
Copy link
Member

String templates should probably be defined kind of like "Any template not defined in the DOM". But that could confuse people why <script type="x-template"> templates in the DOM are string templates, too...

Not easy to get right ^^

@zukilover
Copy link

I consider Vue components to be HTML5 foreign elements.

@sheriffderek
Copy link

sheriffderek commented Nov 18, 2018

I certainly don't mind adding the closing tag - but as a newcomer to Vue - I spent a few hours second guessing myself and pulling my project apart trying to figure out what was wrong. Because I've been using components like this {{compoent-name}} or <ComponentName /> in other projects - where there is no outlet or slot content / I just didn't even think about that.

https://vuejs.org/v2/guide/components.html

Example: https://codepen.io/sheriffderek/pen/rQGLve

@Pezhvak
Copy link

Pezhvak commented Apr 20, 2019

it just works now, anybody here to give advice on using it or not?

@LinusBorg
Copy link
Member

It works ever since Vue 2.0, because that version uses a virtual dom.

The only exception are templates that are defined in an actual .html document.

@pedrokermitgoncalves
Copy link

@LinusBorg / @yyx990803, My question here is, in your opinion, should we be using self-closed component tags or not?

1:

<template>
    <dummy-component></dummy-component>
</template>

2:

<template>
    <dummy-component />
</template>

Option 1 or 2?

@posva
Copy link
Member

posva commented May 29, 2019

Please check https://vuejs.org/v2/style-guide/#Self-closing-components-strongly-recommended

I'm locking this as it is a resolved old thread

@vuejs vuejs locked as resolved and limited conversation to collaborators May 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests