Angular Best Practices 20180412
Angular Best Practices 20180412
Best Practices
Architecture
• Split application into at least three different modules:
• CoreModule
• All services which have to have one and only one instance per application
• Includes: Singleton services such as user service, authentication service
• SharedModule
• All the “dumb” components and pipes
• Doesn’t have any dependency to the rest of the application.
• FeatureModule(s)
• Multiple feature modules for every independent feature
• Feature modules should only import services from CoreModule
Aliases
• Use aliases for cleaner imports
Aliases
• Add baseUrl and paths properties to our tsconfig.json file
Organize Imports
• Arrange your imports based on scope
1. 3rd party libraries
2. Globally accessible app code (in every feature module)
3. Locally accessible feature module code, always use relative path
Organize Imports
• Member sequence
• Properties up top followed by methods.
• Private members after public members, alphabetized
Relative path: ./<path>
• A component relative URL requires no change when you move the
component files, as long as the files stay together.
Use Sass
• Use a style preprocessor to save your
life
• Allows you to use variables, functions,
mixins
Single responsibility
• Make the code more reusable, easier to read, and less mistake prone
• How:
• One component per file
• Small functions as a primary means of abstraction
• Extract templates and styles into a separate file, when more than 3 lines.
ßAvoid
ßPrefer
Consistency
• File structure conventions
• Naming: feature.type.ts