Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Angular equivalent of Vue <slot/>?

Example:

Parent component:

<div> 
  Hello <slot/>!
</div>

Child Component:

<div> 
  World 
</div>

App component:

<Parent> 
  <Child/> 
</Parent>

Output: Hello World!

like image 550
Mike Shiv Avatar asked Aug 17 '19 13:08

Mike Shiv


People also ask

What is slot in Angular?

Single-slot content projection refers to creating a component into which you can project one component. To create a component that uses single-slot content projection: Create a component. In the template for your component, add an <ng-content> element where you want the projected content to appear.

What is the slot in Vue?

Slot props allow us to turn slots into reusable templates that can render different content based on input props. This is most useful when you are designing a reusable component that encapsulates data logic while allowing the consuming parent component to customize part of its layout.

Can I use Vue with Angular?

If you know how to use HTML, CSS, and have basic JavaScript knowledge, you are good to go to start learning Vue. Angular uses Typescript which is a superset of JavaScript and the learning curve is pretty steep. You should know you have to invest a lot of time into learning Angular.

Does Vue have services like Angular?

js — Using a stateful Angular-like service to organize your code.


2 Answers

We have something like this in Angular.

The directive <ng-content> is used to project outer content into the current component, and you can use a selector to project specific content with a CSS style query.

app-example.html:

<div>
   <ng-content></ng-content>
</div>
<div>
   <ng-content selector=".child"></ng-content>
</div>

app-component.html

<app-example>
   <span class="child">World</span>
   Hello
</app-example>

Renders this output:

<app-example>
  <div>Hello</div>
  <div><span class="child">World</span></div>
</app-example>
like image 109
Reactgular Avatar answered Sep 28 '22 08:09

Reactgular


Content Projection

In angular its called "Content Projection", and there are three types

1) Single-slot content projection With this type of content projection, a component accepts content from a single source.

Is content projection cool?

2) Multi-slot content projection Is content projection cool?

Let's learn about content projection!

In this scenario, a component accepts content from multiple sources.

3) Conditional content projection Components that use conditional content projection render content only when specific conditions are met.

Source: https://angular.io/guide/content-projection

like image 23
Muhammad Tariq Avatar answered Sep 28 '22 06:09

Muhammad Tariq