Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ember customizes the HTML label for the package component


May 09, 2021 Ember.js Reference documents


Table of contents


As a rule, get ready and use the Ember CLI command to generate the files you need for your presentation:

  1. ember g route customizing-component-element
  2. ember g component customizing-component-element
  3. ember g route home
  4. ember g route about

By default, components are div label. For example, after the component is rendered, you get the following code:

  1. <div id="ember180" class="ember-view">
  2. <h1>My Component</h1>
  3. </div>

h1 is the content of the component. B oth ember id class are automatically generated by Ember. If you need to modify the HTML generated after rendering is not div label, id such as id class to be custom values, you can set it in the component class.

1, custom package component HTML label

By default, a component is div label, and if you need to modify this default, you can specify the HTML label for the package in the component class.

  1. // app/components/customizing-component-element.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. // 使用tabName属性指定渲染之后HTML标签
  5. // 注意属性的值必须是标准的HTML标签名
  6. tagName: 'nav'
  7. });

Customize a component below.

  1. <ul>
  2. <li>{{#link-to 'home'}}Home{{/link-to}}</li>
  3. <li>{{#link-to 'about'}}About{{/link-to}}</li>
  4. </ul>

The following is the template code for calling the component. Note that the component is wrapped in that HTML label and, correctly, should be wrapped in nav label.

  1. {{customizing-component-element}}

Look at the source code of the page after it is loaded. As follows:

Ember customizes the HTML label for the package component

You can customizing-component-element are indeed nav label, and if the HTML label for the package is not specified in the component class using the property tagName div can delete the tagName the component class and then view the HTML source code of the page.

2, customize the class name of the HTML element of the package component

By default, Ember automatically adds a class name that starts ember to the HTML element of the package component, and if you need to add a custom CSS class, className properties, and you can specify multiple CSS classes at once. For example, the following code example:

  1. // app/components/customizing-component-element.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. // 使用tabName属性指定渲染之后HTML标签
  5. // 注意属性的值必须是标准的HTML标签名
  6. tagName: 'nav',
  7. classNames: ['primary', 'my-class-name'] //指定包裹元素的CSS类
  8. });

After the page reloads, you can nav are two more CSS classes in the nav primary one my-class-name

  1. ……

If you want to decide whether to add a CSS class based on the value of a data, such true class urgent when the urgent is urgent otherwise you do not increase the class. To do this, you can set classNameBindings

  1. // app/components/customizing-component-element.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. // 使用tabName属性指定渲染之后HTML标签
  5. // 注意属性的值必须是标准的HTML标签名
  6. tagName: 'nav',
  7. classNames: ['primary', 'my-class-name'], //指定包裹元素的CSS类
  8. classNameBindings: ['urgent'],
  9. urgent: true
  10. });

After the page reloads the source code, nav is an additional CSS class urgent in the nav urgent if the value urgent false the CSS urgent not render on the nav label.

  1. ……

Note: classNameBindings be consistent with the property name used to determine the data, for classNameBindings is urgent and the user determines whether to add a urgent I f this property is only hump-named then the rendered CSS class name will be - for classNameBindings name secondClassName and the second-class-name For example, the following demo code:

  1. // app/components/customizing-component-element.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. // 使用tabName属性指定渲染之后HTML标签
  5. // 注意属性的值必须是标准的HTML标签名
  6. tagName: 'nav',
  7. classNames: ['primary', 'my-class-name'], //指定包裹元素的CSS类
  8. classNameBindings: ['urgent', 'secondClassName'],
  9. urgent: true,
  10. secondClassName: true
  11. });

After the page is reloaded, look at nav and you can see that there is an additional CSS class second-class-name

  1. ……

If you do not want the post-render CSS class name to be modified to be inline separated form, you can specify the post-rendered CSS class name in the classNameBindings property. For example, the following code:

  1. // app/components/customizing-component-element.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. // 使用tabName属性指定渲染之后HTML标签
  5. // 注意属性的值必须是标准的HTML标签名
  6. tagName: 'nav',
  7. classNames: ['primary', 'my-class-name'], //指定包裹元素的CSS类
  8. classNameBindings: ['urgent', 'secondClassName:scn'], //指定secondClassName渲染之后的CSS类名为scn
  9. urgent: true,
  10. secondClassName: true
  11. });

After the page is reloaded and you look nav you can see that the original CSS class in the nav tag was second-class-name scn

  1. ……

Does it feel like Ember is flexible and powerful!! er's design philosophy is "Convention is better than configuration"! So many of the default settings for properties are the most common formats we normally develop.

In addition to the CSS class names that can be specified classNameBindings especially when dealing with some dynamic effects.

  1. // app/components/customizing-component-element.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. // 使用tabName属性指定渲染之后HTML标签
  5. // 注意属性的值必须是标准的HTML标签名
  6. tagName: 'nav',
  7. classNames: ['primary', 'my-class-name'], //指定包裹元素的CSS类
  8. classNameBindings: ['urgent', 'secondClassName:scn', 'isEnabled:enabled:disabled'],
  9. urgent: true,
  10. secondClassName: true,
  11. isEnabled: true //如果这个属性为true,类enabled将被渲染到nav标签上,如果属性值为false类disabled将被渲染到nav标签上,类似于三目运算
  12. });

As the code notes, isEnabled:enabled:disabled as a three-eye operation that isEnabled nav based on the value of isEnabled.

The following HTML code is true true isEnabled and for cases isEnabled false let the reader try it out for themselves:

  1. ……

Note: If the property value used to determine is Boolean value but a string and the result is different from the result above, Ember renders the value of the string directly to the package label as the CSS class name. For example, the following code:

  1. // app/components/customizing-component-element.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. // 使用tabName属性指定渲染之后HTML标签
  5. // 注意属性的值必须是标准的HTML标签名
  6. tagName: 'nav',
  7. classNames: ['primary', 'my-class-name'], //指定包裹元素的CSS类
  8. classNameBindings: ['urgent', 'secondClassName:scn', 'isEnabled:enabled:disabled', 'stringValue'],
  9. urgent: true,
  10. secondClassName: true,
  11. isEnabled: true, //如果这个属性为true,类enabled将被渲染到nav标签上,如果属性值为false类disabled将被渲染到nav标签上,类似于三目运算
  12. stringValue: 'renderedClassName'
  13. });

At this point the HTML source code of the page is a little different. renderedClassName rendered on the nav label as a nav class name.

  1. ……

Special attention needs to be paid to this point. Ember's judgment of the Boolean value and other values is different.

3, customize the properties of the HTML elements of the package component

Ember also provides a way to customize the properties of package HTML elements by introducing the label name of the HTML element of the package component, the CSS class name, and the other most commonly used property of the CSS class on the HTML tag. S pecified attributeBindings property, the properties of this property are classNameBindings To distinguish it from the previous example, create a new link-items ember g component link-items

  1. 这是个组件

The component is called in the template.

  1. {{customizing-component-element}}
  2. <br><br>
  3. {{link-items}}

Set the component class below, specify that the HTML label for the a label, and add a href

  1. // app/components/link-items.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. tagName: 'a',
  5. attributeBindings: ['href'],
  6. href: 'http://www.google.com.hk'
  7. });

When the page reloads, you get the following results:

Ember customizes the HTML label for the package component

It's simpler, and I'll explain the results after rendering more, classNameBindings

At this end, there is an introduction to the HTML labeling of the packaged component after the component is rendered. T here is not classNameBindings and attributeBindings used in much the same way. I f you have any questions, please leave me a message or check the official tutorial directly.

The full code of the blog post is placed in Github (the blog post has been modified several times, and the code on the blog post may be different from the github code, but the impact is small!). I f you think the blog post is a bit useful to you, please give me a star on the star project. Yours is definitely the biggest motivation for me!!