简体   繁体   English

当 Emnber 辛烷值发生变化时如何触发组件内部的函数

[英]How trigger a function inside component whenever arguments change in Emnber octane

so i have a parent in controller like this所以我在控制器中有一个像这样的父母

import Controller from '@ember/controller';

export default class IndexController extends Controller {
@service firebaseApp;
@service fastboot;
@tracked user =false;

async init(){
 super.init(...arguments);
 if (!this.fastboot.isFastBoot){
  const auth =  await this.firebaseApp.auth();
  auth.onAuthStateChanged((user)=>{
     if(user){
       this.user = true
     } else {
       this.user = false
     }
      })
     }
   }

then call a component loadData like this <LoadData @user={{this.user}}/>然后像这样调用一个组件 loadData <LoadData @user={{this.user}}/>

the question is how to execute a function inside component loadData when @user change?问题是如何在@user 更改时在组件 loadData 中执行函数?

Triggering an action when an argument changes is not that well supported by Ember Octane primitives yet. Ember Octane 原语还不太支持在参数更改时触发操作。 A common approach is to use either @ember/render-modifiers or ember-render-helpers .一种常见的方法是使用@ember/render-modifiersember-render-helpers

@ember/render-modifiers provide a {{did-update}} modifier . @ember/render-modifiers提供了一个{{did-update}}修饰符 ember-render-helpers provide a {{did-update}} helper . ember-render-helpers提供了一个{{did-update}}助手 Both the modifier and the helper except a function as first position argument.除了作为第一个位置参数的函数之外,修饰符和助手都可以。 That function is executed whenever one of the other positional arguments changes.只要其他位置参数之一发生变化,就会执行该函数。

{{did-update}} modifier is helpful when the function executed needs access to a DOM element. {{did-update}}修饰符在执行的函数需要访问 DOM 元素时很有用。 It sets the DOM element, which it is attached to, as an argument on the function when called.它将它所附加的 DOM 元素设置为调用时函数的参数。

{{did-update}} helper is helpful when the function executed does not need access to a DOM element. {{did-update}}帮助器在执行的函数不需要访问 DOM 元素时很有用。

{{! A Glimmer template }}

{{! did-update helper }}
{{did-update this.loadData @user}}

{{! did-update modifier }}
<div class="loading" {{did-update this.showLoadingSpinner @user}} />

The main use case for {{did-update}} modifier is to ease the migration from classical @ember/component to @glimmer/component . {{did-update}}修饰符的主要用例是简化从经典@ember/component@glimmer/component @ember/component的迁移。 In nearly all cases a specific modifier containing the logic, which should be executed, itself is a better solution.在几乎所有情况下,包含应该执行的逻辑的特定修饰符本身就是更好的解决方案。 It provides better reusability, can be tested in isolation and has clear boundaries to the components in which it is used.它提供了更好的可重用性,可以单独测试,并且对使用它的组件有明确的界限。

The main use case for {{did-update}} helper is to fill a gap in current Ember Octance programming model. {{did-update}}助手的主要用例是填补当前 Ember Octance 编程模型中的空白。 Ember Octance provides an awesome developer experience for derived state thanks to autotracking and native getters.由于自动跟踪和本机 getter,Ember Octance 为派生状态提供了很棒的开发人员体验。 It provides a great experience to modify a DOM element depending on a value.它提供了根据值修改 DOM 元素的绝佳体验。 But it does not have great primitives yet to trigger actions like data loading when an argument changes.但是它还没有很好的原语来在参数更改时触发数据加载等操作。

The community current experiments with different approaches to fill that gap.社区目前正在尝试使用不同的方法来填补这一空白。 It seems to settle on @use decorator and resources as proposed by Chris Garret (pzuraq) in an RFC and in a recent blog post .它似乎解决了 Chris Garret (pzuraq) 在RFC最近的博客文章中提出的@use装饰器和资源。 It's available for experiments as part of ember-could-get-used-to-this package.它可作为ember-could-get-used-to-this包的一部分用于实验。

The {{did-update}} helper provided by ember-render-helpers is the most established solution to fill that gap until something like resources settle in Ember. ember-render-helpers提供的{{did-update}}助手是填补这一空白的最成熟的解决方案,直到诸如资源之类的东西在 Ember 中稳定下来。

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

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