小编jvr*_*rnt的帖子

运行基类函数的函数引用时,Angular 会丢失上下文

假设我有CtrlOne一个 extends CtrlTwo,还有一个componentOneCtrlOne模板中实例化的。一些代码(我跳过了大部分不相关的代码,这样问题会更清楚):

class CtrlOne extends CtrlTwo {
    constructor() { super(); }
}

class CtrlTwo {

    sayMyName(name: string) {
        console.log(this.getMyLastName() + name);
    }

    getMyLastName() {
       return 'squarepants';
    }  
}
Run Code Online (Sandbox Code Playgroud)

这是与以下相关联的模板CtrlOne

<component-one data-say-my-name="vm.sayMyName"></component-one>
Run Code Online (Sandbox Code Playgroud)

这是无状态的componentOne

angular.module('mymodule').component('componentOne', {
     bindings: {
         sayMyName: '&'
     },
     template: '<button data-ng-click="$ctrl.sayMyName()('spongebob')></button>'
});
Run Code Online (Sandbox Code Playgroud)

点击之后,我顺利拿到该功能sayMyNameCtrlTwo,但它拒绝承认this.getMyLastName,投掷TypeError: this.getMyLastName is not a function

如果我只是使用sayMyNamegetMyLastName直接从CtrlOne模板中使用,则一切正常。但是,如果我通过传递给 的绑定使用它,则会componentOne出现错误。

我在这里缺少什么?

javascript angularjs typescript

6
推荐指数
1
解决办法
1827
查看次数

reactPHP 承诺同步执行

我正在尝试使用 reactPHP 实现类似 js 的承诺。但是由于某些原因方法同步执行,end_at只有在承诺解决后才打印。

代码:

function iterate() {


    $deferred = new \React\Promise\Deferred();

    sleep(2);

    $deferred->resolve();

    return $deferred->promise();

}

Route::get('test/async', function() {


    echo "start execution at ".time()."<br>"; // this executed first

    iterate()->then(function($result) {
        echo "got result result at ". time() . "<br>"; // this is second
    }, function($error) {

    }, function ($finally) {

    });


    echo "end at " . time(); // this is executed only after then().


}); 
Run Code Online (Sandbox Code Playgroud)

php reactphp

4
推荐指数
1
解决办法
2433
查看次数

使用 app()-&gt;make 创建的模拟类

我的 __constructurct 中有这段代码:

public function __construct(Guard $auth)
{
    $this->auth = $auth;
    $this->dbUserService = app()->make('DBUserService');
}
Run Code Online (Sandbox Code Playgroud)

现在,当我进行单元测试时,我知道我可以模拟 Guard 并将其模拟传递给 $auth,但我如何模拟dbUserService?它是通过 IoC 容器实例化的。

php phpunit unit-testing laravel

3
推荐指数
2
解决办法
2800
查看次数

phpunit检查是否在测试的类上调用了方法

可以说我有以下代码块:

$i = 1;

if ($i > 1) {
    $this->methodOne();
} else {
    $this->methodTwo();
}
Run Code Online (Sandbox Code Playgroud)

如何检查我的PHPUnit测试中是否从已测试的类调用methodOne或methodTwo?

php phpunit

2
推荐指数
1
解决办法
7447
查看次数