Detailed explanation of the steps to develop Angular2 components
This time I will bring you a detailed explanation of the steps for developing Angular2 components. What are the precautions for developing Angular2 components? The following is a practical case, let’s take a look.
Let’s briefly talk about the responsibilities and status of components under the ng1 to ng2 framework:
One of the major features of ng1 is instructions, which are divided into attribute types and tags. Type, css type and annotation type. The components written in css classes and comments are probably not used by most people, while attribute-type instructions and label-type instructions are one of the contributors to the popularity of ng1 throughout the universe. In ng2, the tag-type instructions are simply separated, upgraded following the style of emerging forces such as vue and called components, and used to handle all things that deal with the view (DOM), including displaying data andanimation. Attribute instructions are used to improve the functions of components, such as receiving user input and responding to user clicks and other events. In fact, many functions embedded in ng2 are attribute-based instructions - ngFor, ngIf, etc., and components are more dependent on specific projects, so it is suitable to use components based on projects to write interfaces visible to users. Come. At the same time, components also need to deal with routing. Front-end routing can be regarded as dynamically changing the DOM. After formulating the rules in ng2 routing, it actually dynamically renders or destroys different components to achieve front-end page switching.
Then let’s talk about how I used ng2 components to build a website in my previous project:
1. According to the official practice, each application must have a root component.
2. Differentiate front-end routing. Each routing entry points to a component, and each component renders a page.
3. As the project expands, a single module cannot meet the business classification, so the root route first guides lazy loading of each sub-module, and then the sub-routes of the sub-modules point to specific sub-components, and Render their respective pages.
4. Focusing on a single page rendered by a single sub-component, you can actually encapsulate some reusable tag blocks into a new component (such as complex data items that require ngFor traversal).
5. I found that some components (such as side buttons or message modal boxes) may actually be used by the entire project, and these must be encapsulated into globally shared components.
I still feel a little confused. The reason is probably the name of the component, because under the ng2 framework, everything from routing pages to data entries uses components. Declaration The methods are all the same, so it will inevitably be confusing.
I have no choice but to continue to subdivide the components myself, so the author divided the components of ng2 into four categories:
Page components
When dealing with routing, we only care about matching routing rules and rendering the interface. This type of component does not require a selector parameter when declaring (it is only positioned by routing and does not require specific tags)
Receive routing parameters or resolve data, try not to make other business interaction requests, and do not set Input and Output variables
Layout components
Modules used to subdivide pages. If the page is simple, you can skip using the unit component directly. There must be specific selector parameters because it must be used in the page component
is not responsible for obtaining routing or resolve data, and try not to request data but pass in data through Input or respond to events through Output. All business interaction requests should be completed in the layout component as much as possible (not as difficult as placing it in the page component) chaos, and each layout component can not affect each other)
Unit component
Must have its own selector , generally reusable in its own module, modified by various attribute-type instructions
is used to simplify repeated tags at the unit level, such as each data item in the obtained list data. The interface display can be encapsulated into a unit component
-
It is only responsible for passing in data and displaying it through Input, and responding to events through Output to the outer layout component or page component for processing
Shared components
There must be a selector, a component shared by the entire project. It is relatively free to implement and focuses on reducing the complexity of the entire project. Duplicate code and facilitate maintenance
are more suitable, such as message pop-up boxes, loading progress bars, etc.
most attribute-type instructions, in fact, the functions are generally It is relatively general and can be treated the same as shared components, except that shared components have specific views and shared instructions are only used to implement common functions
In fact, there are no technical difficulties at all. It just implies one thing - project directory arrangement is very important.
ng2 doesn’t know the many types of components that the author has subdivided for it. These classifications are for you to see. The specific embodiment is a project derived from this classification systemDirectory structure .
The author added some plus signs and some minus signs in front of the folder names, which can be regarded as his own seemingly strange directory structure arrangement.
The explanation is: the outermost term represents the directory of the entire lazy loading module. The components in the directory with a plus sign inside represent page components, and the components without any symbols represent layout components with a minus sign. The component representatives under the directory are unit components. The unit components may be used in the entire lazy loading module, so they are directly at the outermost layer of the lazy loading directory, while the layout components are all in the same level directory of the specific page components.
The summary is that after putting aside the appearance of ES6 and TypeScript, the use of ng2 components is not technically difficult. This article is purely a clumsy experience. Of course, the author’s current experience is still It is far from enough. I hope to have deeper insights after studying the ng2 source code thoroughly in future development.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:js realizes the uniform fade-in and fade-out of pictures
How to realize the mutual conversion between numbers and strings in JS
The above is the detailed content of Detailed explanation of the steps to develop Angular2 components. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Android development is a busy and exciting job, and choosing a suitable Linux distribution for development is particularly important. Among the many Linux distributions, which one is most suitable for Android development? This article will explore this issue from several aspects and give specific code examples. First, let’s take a look at several currently popular Linux distributions: Ubuntu, Fedora, Debian, CentOS, etc. They all have their own advantages and characteristics.

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces
