does angular have the “computed property” feature like in vue.js?

前端 未结 6 1509
我在风中等你
我在风中等你 2020-12-28 12:24

I learnt Vue.js first, and now have a project in Angular 4 so I just learnt Angular. I find everything is not that different from Vue except the \"Computed Property\". In Vu

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-28 12:53

    yes, you can.

    In TS file:

    export class MyComponent {
    
      get name() {
         return  this.firstname + ' ' + this.lastname;
      }
    }
    

    and after that in html:

    {{name}}

    here is an example:

    @Component({
      selector: 'my-app',
      template: `{{name}}`,
    })
    export class App  {
      i = 0;
      firstN;
      secondN;
    
      constructor() {
        setInterval(()=> {
          this.firstN = this.i++;
          this.secondN = this.i++;
        }, 2000);
      }
      get name() {
        return  this.firstN + ' ' + this.secondN;
      }
    }
    

提交回复
热议问题