简体   繁体   English

渲染组件时未调用 Init() 方法?

[英]Init() method not being called when Component gets rendered?

Im trying to migrate from ember classic syntax to more modern syntax, but struggling to understand why my init() method call does not get called?我试图从 ember 经典语法迁移到更现代的语法,但很难理解为什么我的init()方法调用没有被调用?

template:模板:

<HomeCard>
  {{#if this.loadThemeEditorDeepLink.lastSuccessful}}
    <PolarisBanner
      @status="critical"
      @action={{hash
        text="Turn on"
        onAction=(route-action "openUrl" this.themeEditorAppEmbedUrl)
      }}
      @secondaryAction={{hash
        text="Watch quick tutorial"
        onAction=(fn (mut this.showModal) true)
      }}
    />
  {{/if}}
</HomeCard>

{{#if this.showModal}}
  <PolarisModal
    @onClose={{fn (mut this.showModal) false}}
  />
{{/if}}

counter .js component file: .js组件文件:

import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { dropTask } from 'ember-concurrency-decorators';
import { tracked } from '@glimmer/tracking';
export default class CheckComponent extends Component {
  @service ajax;

  @tracked themeEditorAppEmbedUrl = '';
  showModal = false;

  @dropTask
  loadThemeEditorDeepLink = function* loadThemeEditorDeepLink() {
    let url = yield this.fetchThemeSettingsAppEmbedDeepLink();
    this.themeEditorAppEmbedUrl = url;
  };

  async fetchThemeSettingsAppEmbedDeepLink() {
    try {
      let result = await this.ajax.request(
        `apiUrl`
      );
      return result.theme_editor_deep_link_url;
    } catch (err) {
      this.errorHandler.handle(err);
    }
  }

  init() {
    console.log('testing');
    super.init(...arguments);
    this.loadThemeEditorDeepLink.perform();
  }
}

Even tough my component gets rendered just fine, the init() call does not happen, thus my component malfunctions即使我的组件渲染得很好, init()调用也不会发生,因此我的组件出现故障

Any idea on what I might be missing?知道我可能会遗漏什么吗?

glimmer components do not have an init method. glimmer 组件没有 init 方法。 They are mostly "basic classes", so you'd want to use the constructor instead.它们大多是“基本类”,所以你会想改用构造函数。

import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { dropTask } from 'ember-concurrency-decorators';
import { tracked } from '@glimmer/tracking';

export default class CheckComponent extends Component {
  @service ajax;

  @tracked themeEditorAppEmbedUrl = '';
  showModal = false;

  constructor(owner, args) {
    super(owner, args);
    
    this.loadThemeEditorDeepLink.perform();
  }

  // ...
}

for more information:想要查询更多的信息:

this may be helpful as well: https://ember-learn.github.io/ember-octane-vs-classic-cheat-sheet/这也可能有帮助: https://ember-learn.github.io/ember-octane-vs-classic-cheat-sheet/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM