首页 > 解决方案 > Where to initialize variables in Stencil

问题描述

I have just started Stencil, and I was wondering what was a good practise for initizializing variables. As I see it, I have 3 possibilities:

1) @State() private page: Boolean = true;

2)

constructor() {
    this.page = true
}

3)

componentWillLoad() {
    this.page = true;
}

What's the best way to do it ?

标签: javascripttypescriptstenciljs

解决方案


根据模板样式指南,如果可以的话,您应该在声明时初始化@State变量:

  /**
   * 3. State() variables
   * Inlined decorator, alphabetical order.
   */
  @State() isValidated: boolean;
  @State() status = 0;

推荐阅读