Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-container equivalent in vue.js

In Angular there is a tag called ng-container used like so

<ng-container *ngIf="false">this wont be shown</ng-container>

now as per the angular docs

The Angular is a grouping element that doesn't interfere with styles or layout because Angular doesn't put it in the DOM.

Now this is really handy in angular since there are often times I would like to group a set of html elements without using a <div></div>

For example

<div class="flex-div">
    <ng-container *ngIf="true">
       <img src="cool-img" alt="awesome">
       <h1>Cool Title</h1>
       <p>Cool Text</p>
    </ng-container>
    <ng-container *ngIf="false">
       <img src="not-so-cool-img" alt="awesome">
       <h1>Not So Cool Title</h1>
       <p>Not So Cool Text</p>
    </ng-container>
</div>

here I have a div that has a position of flex on it and also rules about what the elements inside do..

Now If I wrapped the elements in a normal div it will break my flex styles but with the ng-container it contains my elements but is not rendered to them DOM

Is there an equivalent in Vue??

like image 650
Smokey Dawson Avatar asked Apr 24 '19 10:04

Smokey Dawson


People also ask

What's the equivalent of Angular service in VUE JS?

Mixins are a flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options.

What is Angular ng-container?

What is ng-container ? ng-container allows us to create a division or section in a template without introducing a new HTML element. The ng-container does not render in the DOM, but content inside it is rendered. ng-container is not a directive, component, class, or interface, but just a syntax element.

What is difference between ng-container and Div?

ng-container serves as a container for elements which can also accept structural directives but is not rendered to the DOM, while ng-template allows you to create template content that is not rendered until you specifically (conditionally or directly) add it to the DOM.

When should I use NG-container?

<ng-container> is rendered as an HTML comment. So ng-container is useful when you want to conditionaly append a group of elements (ie using *ngIf="foo" ) in your application but don't want to wrap them with another element.


1 Answers

You could use conditional grouping on a template as mentioned in the docs here. The template will serve as an invisible wrapper.

<div class="flex-div">
<template v-if="true">
   <img src="cool-img" alt="awesome">
   <h1>Cool Title</h1>
   <p>Cool Text</p>
</template>
<template v-if="false">
   <img src="not-so-cool-img" alt="awesome">
   <h1>Not So Cool Title</h1>
   <p>Not So Cool Text</p>
</template>
</div>
like image 143
Stephen S Avatar answered Oct 17 '22 09:10

Stephen S