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
Comments
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 |
I didn't see this in the component documentation, which caused me a little time wasting. It would be good to add it. |
How about set an option like <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> |
I think yyx990803 is saying something far more fundamental. He doesn't want to go against HTML5 |
@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. |
it would be great if this is mentioned somewhere in the documentation. |
@asumaran See http://vuejs.org/guide/components.html#Template-Parsing:
|
Using tags that are self-closing is not invalid HTML5, though. (As far as I know, |
@harbulot FYI self-closing tags works in 2.0 as long as you don't use in-dom templates. |
@fnlctrl Is this because templates are converted to render functions? So we can use self-closing tags when compiling vue (single file components)? |
@alexsasharegan I too would like to know the answer to your question. |
It works with vue-loader and |
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: However, the following defines a string template as a single-file component too: It seems as though a number of individuals are confused by this: Perhaps the docs could be changed to kind of clear up this confusion? |
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>',
}) |
String templates should probably be defined kind of like "Any template not defined in the DOM". But that could confuse people why Not easy to get right ^^ |
I consider Vue components to be HTML5 foreign elements. |
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 |
it just works now, anybody here to give advice on using it or not? |
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 |
@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? |
Please check https://vuejs.org/v2/style-guide/#Self-closing-components-strongly-recommended I'm locking this as it is a resolved old thread |
How about allowing self-closed component tags like
<my-component />
? Currently, it does not work.The text was updated successfully, but these errors were encountered: