General Q&A for angular application

Q:   what's the difference between ng serve and ng build ?

A:   The difference between ng serve and ng build is that ng serve is used to support development service while ng build is used to deploy the finished project.


Q: what's the difference between ng serve and npm start ?

A:  The difference between ng serve and npm start is  that ng serve starts an embedded server whereas npm start starts the node server, but if you have defined for the start command of scripts object in your package.json file.  so if it looks like this:

"scripts":{

    "start" : "ng serve"

}

The npm start will run ng serve.


Q:  What does the decorator @Component exactly do in angular , can you give  more detail informations about it?

A: As the official explantation : https://angular.io/api/core/Component

From the official docs we know that:

        The decorator that mark a class as an angular component and provide configuration metadata that determines how the component should be processed, instantiated and used at runtime.

        what we can see below is combination with mentioned above, we can conclude that the makeDecorator function manufacture the core concepts:  Directive , component, NgModule and so on.

        As we can see makeDecorator  function recept the params from @Component metadata,  @Component decorator function is the component metadata, so that is the main responsibility, to provide metadata.

        The easiest way, if you want to reproduce something similar is to

  1. Install the reflect-metadata.

    npm install --save reflect-metadata
    
  2. Create the decorator function1

    import 'reflect-metadata';
    
    interface MyComponentMetadata {
      template?: string;
      selector?: string;
    }
    
    function MyComponent(metadata: MyComponentMetadata) {
      return function(ctor: Function) {
        // add metadata to constructor
        Reflect.defineMetadata('annotations', metadata, ctor);
      }
    }
    
  3. Use it

    @MyComponent({
      selector: 'component',
      template: '<hello></hello>'
    })
    class HelloComponent {
    }
    
  4. Now when you need to get the metadata, just do

    let metadata = Reflect.getMetadata('annotations', HelloComponent);
    

        The purpose of the metadata is to provide information for Angular to know what to do with the class. So the decorator doesn't really need to be anything more than just a metadata provider. Angular decides what to do with the metadata, not the decorator function.


Q: What's the functionality of <app-root> in angular ?

A:  we can find a selector  <app-root> in index.html file, this selector is regarded as a default root component for using load html template and css styles, we knew that if <app-root> selector is mismatched in the corresponding ts file, an error will occurs because Angular can't find the corresponding node in the DOM.


Q: What do you think about  standalone components?

A: The standalone components provide a simpl way to build angular applications.  Angular class marked as standalone do not need to be declared in an NgModule, for example, if  PhotoGalleryComponent is a standalone component, it can directly import another component ImageGridComponent 

@Component({
  standalone: true,
  selector: 'photo-gallery',
  imports: [ImageGridComponent],
  template: `
    ... <image-grid [images]="imageList"></image-grid>
  `,
})
export class PhotoGalleryComponent {
  // component logic
}

secondly,  if a component is standalone, you shouldn't declare it in @NgModule, otherwise you will get an error like this: 

 

Guess you like

Origin blog.csdn.net/qq_33036061/article/details/132362849