Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comment out a part of Vue template element

Sometimes it is needed to comment out some element attribute without having to remember it in order to restore it quickly after some tests.

Commenting out whole element is achievable with HTML commenting syntax

<div>
    <!-- <h2>Hello</h2> -->
    <span>hi</span>
</div>

However this won't work with a single attribute (causes rendering error)

<my-comp id="my_comp_1"
         v-model="value"
         <!-- :disabled="!isValid" -->
         @click="handleClick">
</my-comp>

The best approach I could see and used before was to make a tag backup by copying whole element and settings v-if="false" for it (or comment out whole copied element) and continue to experiment with original one

like image 971
humkins Avatar asked Dec 20 '17 08:12

humkins


People also ask

How do I comment in Vue template?

To comment out code in HTML templates you can use HTML comment tag <--! Your comment --> . HTML comments will only be visible to the developer, they won't appear in the page source code.

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 are the 3 parts of a component in Vue?

Components in Vue are composed of three parts; a template (which is like HTML), styles and JavaScript. These can be split into multiple files or the same .

What is interpolation in VUE JS?

The most basic form of Vue interpolation is text interpolation. It's simply the process of displaying a string that's defined within your component logic.

How to comment out code in Vue JS templates?

In this tutorial, you’ll learn how to comment out code in Vue.js templates. To comment out code in HTML templates you can use HTML comment tag <--! Your comment -->. HTML comments will only be visible to the developer, they won’t appear in the page source code.

How to add comments outside the root tags in Vue?

I wish vue supported comments outside the root tags because it's the most sensible place to create READMEs and such, but oh well. Instead of using comments outside the supported root tabs, use valid tags there. <comment>Commenting here</comment. You will have to add these to your webpack config. vue-loader.vuejs.org/guide/custom-blocks.html#example

How to comment out code in HTML templates?

To comment out code in HTML templates you can use HTML comment tag . HTML comments will only be visible to the developer, they won’t appear in the page source code.

How to display HTML content in vuejs using V-HTML?

If we see the html content is displayed the same way we have given in the variable htmlcontent, this is not what we want, we want it to be displayed in a proper HTML content on the browser. For this, we will have to use v-html directive. The moment we assign v-html directive to the html element, VueJS knows that it has to output it as HTML content.


2 Answers

I got these solutions to work. I thought of solution 1.

Starting code:

<div 
v-for="foo in foos" 
:key="foo.id" 
@click="foo.on = !foo.on /* JavaScript comment. */"
>
  <label>
    {{ foo.name }} {{foo.on}}
  </label>
</div>

The Vue directive HTML attribute that needs to be disabled: @click="foo.on = !foo.on"

How the final div tag will run:

<div 
v-for="foo in foos" 
:key="foo.id" 
>

Solutions 1 and 2 keep the disabled attribute inside its tag. I didn't find a good way to make a "raw string". To keep the attribute in the tag, the outer and inner quotes must be different.

sol. 1: I made a new v-bind attribute (:lang) to put the disabled attribute in.

:lang='en  /* @click="foo.on = !foo.on" */'

Sol. 2: Pick a Vue directive. Put the attribute in.

v-for="foo in foos /* @click='foo.on = !foo.on' */"

Solutions 3 and 4 put the attribute outside the tag.

Sol. 3:

<div v-if="false">
  @click="foo.on = !foo.on"
</div>

Sol. 4: <!-- @click="foo.on = !foo.on" -->

like image 98
David Smolinski Avatar answered Sep 23 '22 02:09

David Smolinski


I don't think you can put an HTML comment inside a component tag, for much the same reason you can't put comments inside an HTML element opening tag. It's not valid markup in either situation. I think the closest you could come would be to place the comment in the quotes:

:disabled="// !isValid"

Which would have the same effect as:

:disabled=""

Depending on whether your component can handle that value being missing, that might fit your needs.

like image 36
Rich Churcher Avatar answered Sep 24 '22 02:09

Rich Churcher