General Q&A for angular application

本文解释了ngserve与ngbuild在开发和服务端部署中的区别,ngserve用于开发服务,ngbuild用于部署;同时详细讨论了@Component装饰器在Angular中的作用,以及<app-root>的默认功能和standalonecomponents的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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: 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌托邦钢铁侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值