Open In App

Ember.js Component tagName Property

Last Updated : 02 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Ember.js is an open-source JavaScript framework used for developing large client-side web applications which are based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.

The tagName property of the Component is the property used to give the tag name to the outer view element. By default, views use the div tag of HTML. 

Syntax: 

tagName : NameOfTag ;

 

Parameters: 

  • NameOfTag: It is the name of the tag which is used in the view’s element. 

Steps to Install and Run Ember.js:

Step 1: To run the following examples you will need to have an ember project with you. To create one, you will need to install ember-cli first. Write the below code in the terminal:

npm install ember-cli

Step 2: Now you can create the project by typing in the following piece of code:

ember new <project-name> --lang en

To start the server, type:

ember server

Example 1: In this example, we will create the component with tagName span and see some CSS properties Type the following code to generate the route for this example:

ember generate route test1

app/components/first.js

Javascript




import Component from '@glimmer/component';
import Ember from 'ember';
  
export default Ember.Component.extend({
    tagName: 'Span'
})


app/components/first.hbs

HTML




{{page-title "Component tagName"}}
  
<h3>{{yield}}</h3>


app/components/test1.hbs

HTML




<h1>This is main templates tag</h1>
<h2>Hello Geeks keep reading and Grow up</h2>
<First>
    This is Component templates tag.
</First>


app.css

CSS




span {
    font-family: arial, Verdana, sans-serif;
    font-size: 20pt;
    color: #2fd778;
}


Output:

output

Example 2: In this example, we will define the tag Name of the component using tagName in javaScript and inline in tag and see which has more precedence. Type the following code to generate the route for this example:

ember generate route test2

app/components/second.js

Javascript




import Component from '@glimmer/component';
import Ember from 'ember';
  
export default Ember.Component.extend({
    tagName: 'Span',
});


app/components/second.hbs

HTML




{{page-title "Component tagName"}}
  
<h3>{{yield}}</h3>


app/templates/test2.hbs

HTML




<h1>This is main templates tag</h1>
<h2>Hello Geeks keep reading and Grow up</h2>
<Second @tagName="section">
    This is Component templates tag.
</Second>


app.css

CSS




section {
    font-family: arial, Verdana, sans-serif;
    font-size: 25pt;
    color: #6b6bd7;
}
span {
    font-family: arial, Verdana, sans-serif;
    font-size: 20pt;
    color: #2fd778;
}


Output:

output3

Reference: https://api.emberjs.com/ember/4.9/classes/Component/properties/tagName?anchor=tagName



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads