Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue: <tag></tag> vs <tag />

In the Vue documentation, I have seen opening and closing tags, but I've seen in other places where authors write components as self closing tags, like <some-component />

Is the practice of self-closing tags legal in Vue?

like image 433
J.Ko Avatar asked Sep 30 '18 17:09

J.Ko


People also ask

What is V HTML Vue?

The v-html directive is a Vue. js directive used to update a element's inner HTML with our data. This is what separates it from v-text which means while v-text accepts string and treats it as a string it will accept string and render it into HTML.

What is Vue template tag?

Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers.

What is the difference between V if and V show?

The key difference is that v-if conditionally renders elements and v-show **conditionally displays **elements. This means that v-if will actually destroy and recreate elements when the conditional is toggled. Meanwhile, v-show will always keep the element in the DOM and will only toggle its display by changing its CSS.

Should always be multi word VueJS?

Component names should always be multi-word, except for root App components, and built-in components provided by Vue, such as <transition> or <component> . This prevents conflicts with existing and future HTML elements, since all HTML elements are a single word.


2 Answers

From the Vue style guide:

Components with no content should be self-closing in single-file components, string templates, and JSX - but never in DOM templates.

It's legal and strongly recommended by the Vue style guide: Vue Style Guide #self-closing components

like image 175
elad frizi Avatar answered Oct 18 '22 21:10

elad frizi


Both the questions are answered above. But, I would like to point out what exactly is meant by no content in self closing tags.

  1. When we use <div><p>Something</p></div>, <p> tag here is the content and hence, we cannot use div as a self closing tag.

  2. Similarly in case of Vue JS components, you can also include content inside the component tags. e.g., <MyComponent><p>Something Else</p></MyComponent>. Then, in the component definition of <MyComponent>, you have to includes to render the content passed wherever <MyComponent> is used.

  3. If you intend to not have any content to be passed from the <MyComponent>. i.e. If you do not have the <slot> tag in the definition of your component, then your <MyComponent> can be a self closing tag.
like image 31
alroh Avatar answered Oct 18 '22 23:10

alroh