Ember.js Component elementId Property
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 many websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.
The elementId Property of the Component class is the property that is used to set the element of the component in the DOM. We can define the value of this property but the value should be unique.
Syntax:
elementId : value ;
Parameters:
- value: It is the unique value that will assign to the Id of the component element in DOM.
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 a component and bind the name ‘tutorial’ with the id attributes of the component element. the following code is to generate the route for this example:
ember generate route file1
app/components/first.js
Javascript
import Component from '@glimmer/component' ; import Ember from 'ember' ; export default Ember.Component.extend({ tagName: 'Section' , elementId: 'tutorial' , }) |
app/components/first.hbs
HTML
{{page-title "Component ElementId"}} < h3 >{{yield}}</ h3 > |
app/templates/file1.hbs
HTML
< h1 >w3wiki</ h1 > < h2 >Hello Beginner keep reading and Grow up</ h2 > < First > w3wiki </ First > |
app.css
CSS
#tutorial{ font-family : Tahoma , Geneva, Verdana , sans-serif ; font-size : 25pt ; color : #ae2549 } h 1 { font-family : Tahoma , Geneva, Verdana , sans-serif ; font-size : 25pt ; color : #2fd778 } |
Output:
Example 2: In this example, we will define Element Id in a javascript file and define inline elementId to component element. Type of the following code to generate the route for this example:
ember generate route file2
app/components/second.js
Javascript
import Component from '@glimmer/component' ; import Ember from 'ember' ; export default Ember.Component.extend({ tagName: 'div' , elementId: 'tutorial' , }) |
app/components/second.hbs
HTML
{{page-title "Component ElementId"}} < h3 >{{yield}}</ h3 > |
app/templates/file2.hbs
HTML
< h1 >w3wiki</ h1 > < h2 >Hello Beginner keep reading and Grow up</ h2 > < Second @ elementId = "component" > This is Component templates tag. </ Second > |
app.css
CSS
#tutorial{ font-family : Tahoma , Geneva, Verdana , sans-serif ; font-size : 25pt ; color : #ae2549 } h 1 { font-family : Tahoma , Geneva, Verdana , sans-serif ; font-size : 25pt ; color : #2fd778 } #component{ font-family : -apple-system; font-size : 22pt ; background-color : #ae2549 ; } |
Output:
Reference: https://api.emberjs.com/ember/4.9/classes/Component/properties/elementId?anchor=elementId