如何在Ember控制器中访问变量?
我已经尝试过这些方法,但是不起作用:

import ObjectController from '@ember/controller';
export default ObjectController.extend({

  currentPhase: 1,

  init: function () {
      var selfReference = this;
      this.set("currentPhase",1);
  },

  strings: {
      title: "Title",
      phase1: "Fase " + selfReference.get("currentPhase"),
      phase2: "Fase " + currentPhase
  }
});

最佳答案

在这种情况下,您可以使用computed property

import Controller from '@ember/controller';
import { computed } from '@ember/object';

export default Controller.extend({
  currentPhase: 1,

  strings: computed('currentPhase', function() {
    let currentPhase = this.get('currentPhase');
    return {
      title: "Title",
      phase1: `Phase ${currentPhase}`,
      phase2: `Phase ${currentPhase}`
    }
  }
});

关于javascript - Ember Controller 中的访问变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47272046/

10-12 13:42