小编Tra*_*ace的帖子

onEnter未在React-Router中调用

好吧,我厌倦了尝试.
onEnter方法不起作用.知道为什么会这样吗?

// Authentication "before" filter
function requireAuth(nextState, replace){
  console.log("called"); // => Is not triggered at all 
  if (!isLoggedIn()) {
    replace({
      pathname: '/front'
    })
  }
}

// Render the app
render(
  <Provider store={store}>
      <Router history={history}>
        <App>
          <Switch>
            <Route path="/front" component={Front} />
            <Route path="/home" component={Home} onEnter={requireAuth} />
            <Route exact path="/" component={Home} onEnter={requireAuth} />
            <Route path="*" component={NoMatch} />
          </Switch>
        </App>
      </Router>
  </Provider>,
  document.getElementById("lf-app")
Run Code Online (Sandbox Code Playgroud)

编辑:

我打电话时执行该方法onEnter={requireAuth()},但显然这不是目的,我也不会得到所需的参数.

javascript reactjs react-router react-router-v4

57
推荐指数
2
解决办法
4万
查看次数

在yaml中使用占位符

有没有办法在yaml中使用占位符,如下所示:

foo: &FOO
    <<propname>>: 
        type: number 
        default: <<default>>

bar:
    - *FOO 
       propname: "some_prop"
       default: "some default" 
Run Code Online (Sandbox Code Playgroud)

yaml indirection computed-properties template-variables

26
推荐指数
2
解决办法
6万
查看次数

VBA引用计数 - 对象销毁

最近我碰到了一个让我更健康的问题; 它让我忙碌,我无法在网上找到透明的解释.
它与Excel对象的破坏有关(我一直使用它,以前从未真正质疑过).

导致我的问题的背景:
使用常规对象,您可以使用关键字SET和NEW来实例化对象.例如:

Set classInstance = New className
Run Code Online (Sandbox Code Playgroud)

每当我们以这种方式实例化时,对象就会在堆内存中创建,并且引用计数器会增加1.
如果我不添加更多引用,则以下语句会将引用计数返回到零:

Set classInstance = Nothing 
Run Code Online (Sandbox Code Playgroud)

当引用计数变为0时,对象将被销毁并从内存中清除,并且"classInstance"指向.

我读过的内容:
当我们使用"CREATEOBJECT"函数时,它返回对COM对象的引用.

Set oApp = CreateObject("Excel.Application")
Run Code Online (Sandbox Code Playgroud)

即使我们可以说:

Set oApp = nothing 
Run Code Online (Sandbox Code Playgroud)

对象的引用计数将变为0,oApp将不再指向该对象.

我的问题:
1)为什么这种类型的对象需要在实际从内存中删除对象之前调用方法.Quit?
添加对需要.close方法的工作簿对象(workbooks.add或workbook.open)的引用时也是如此.为什么在将引用计数设置为零时不能自动销毁这些对象?
当我们说例如:

set oRange = nothing 
Run Code Online (Sandbox Code Playgroud)

2)有必要说:

oApp.Quit
set oApp = nothing 
Run Code Online (Sandbox Code Playgroud)

由于在应用.Quit时已经从内存中清除了Application对象,因此不再有对象被释放.
我想出的唯一原因是,为什么oApp在Quit之后会被设置为Nothing,因为它可能指向一个未使用的内存位置(在堆上),如果重新分配这个内存,可能会导致混乱(虽然在VBA中我发现这很难想象).我问自己这个结论是否正确,我想从知道答案的人那里得到确认.
请告诉我,如果我看错了.

3)他们在VBA中称之为"对象的引用"(例如上面代码中的oApp),我将它们视为C中的指针变量.使用此语句是否安全或再次使用,我是否错误地看到了这一点?

一般来说并不难应用.确定并设置为空,但收到关于该主题的一些准确信息会很好.所以我知道为什么我这样做的百分之百.

excel vba excel-vba

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

Function.prototype是一个函数

我正在深入研究Javascript原型链.
为了记录我的发现,我画了以下方案:

在此输入图像描述

虽然大多数概念都很清楚,但我只剩下两个相关的问题.我猜测将这些问题集中在这个问题上可能会更好,而不是将它们分开.

  1. 有没有理由Function.prototype成为类型函数而不是对象?
    typeof Function.prototype; //"function"
  2. Function.prototype在JS中是一个'独特的函数',因为它没有像其他函数那样拥有自己的原型属性吗?(是否有一个普遍接受的"名称"来指代它?)

javascript prototype

23
推荐指数
2
解决办法
2851
查看次数

Android架构组件网络线程

我目前正在查看以下指南:https://developer.android.com/topic/libraries/architecture/guide.html

networkBoundResource类:

// ResultType: Type for the Resource data
// RequestType: Type for the API response
public abstract class NetworkBoundResource<ResultType, RequestType> {
    // Called to save the result of the API response into the database
    @WorkerThread
    protected abstract void saveCallResult(@NonNull RequestType item);

    // Called with the data in the database to decide whether it should be
    // fetched from the network.
    @MainThread
    protected abstract boolean shouldFetch(@Nullable ResultType data);

    // Called to get the cached data from the …
Run Code Online (Sandbox Code Playgroud)

java architecture multithreading android

20
推荐指数
1
解决办法
2394
查看次数

Javascript:为函数原型添加方法

是否有更短的方式来写这个:

var controller = function(){ 
    /*--- constructor ---*/
}; 

controller.prototype.function1 = function(){ 
    //Prototype method1
}

controller.prototype.function2 = function(){ 
    //Prototype method2
}

controller.prototype.function3 = function(){ 
    //Prototype method3
} 

return controller
Run Code Online (Sandbox Code Playgroud)

我正在使用require.js.我想知道我是否可以避免controller.prototype代码重复.

javascript methods prototype

14
推荐指数
1
解决办法
1万
查看次数

Espresso,Dagger2在BaseActivity上设置了ViemodelProvider.Factory

我有一个像这样的抽象AccountRequiredActivity(并且工作正常):

public abstract class AccountRequiredActivity extends LifecycleActivity {

    @Inject
    ViewModelProvider.Factory viewModelFactory;

    private AccountViewModel accountViewModel;

    public abstract void doOnCreate(Bundle savedInstanceState);
    public abstract void doOnResume();

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading_app);
        AndroidInjection.inject(this);
        accountViewModel = ViewModelProviders.of(this, viewModelFactory).get(AccountViewModel.class);

        if(!accountViewModel.isAuthenticated()) {
            redirectToLogin();
        } else {
            doOnCreate(savedInstanceState);
        };

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!accountViewModel.isAuthenticated()) {
            redirectToLogin();
        } else {
            doOnResume();
        };
    }

    private void redirectToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

}
Run Code Online (Sandbox Code Playgroud)

测试期间的问题是我没有办法设置viewModelFactory活动.

当一个活动有片段时,我可以做类似的事情:

@Before
public …
Run Code Online (Sandbox Code Playgroud)

android android-espresso dagger-2

14
推荐指数
1
解决办法
613
查看次数

存储时间戳,时间00:00:00

我想在时间组件中将时间戳存储在数据库中00:00:00.
以下代码:

$start_date = Carbon::createFromFormat('d-m-Y', $date_interval["start_date"]); 
$end_date = Carbon::createFromFormat('d-m-Y', $date_interval["end_date"]); 
Run Code Online (Sandbox Code Playgroud)

将当前时间添加到日期,当我仅提供没有时间规范的日期时.
我需要这个,因为我需要从数据库中检索内部整数,这应该是一天的开始.

存储这个"一天开始"的最佳方法是什么?

php mysql datetime php-carbon

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

始终调用Mono switchIfEmpty()

我有两种方法.
主要方法:

@PostMapping("/login")
public Mono<ResponseEntity<ApiResponseLogin>> loginUser(@RequestBody final LoginUser loginUser) {
    return socialService.verifyAccount(loginUser)
            .flatMap(socialAccountIsValid -> {
                if (socialAccountIsValid) {
                    return this.userService.getUserByEmail(loginUser.getEmail())
                            .switchIfEmpty(insertUser(loginUser))
                            .flatMap(foundUser -> updateUser(loginUser, foundUser))
                            .map(savedUser -> {
                                String jwts = jwt.createJwts(savedUser.get_id(), savedUser.getFirstName(), "user");
                                return new ResponseEntity<>(HttpStatus.OK);
                            });
                } else {
                    return Mono.just(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
                }
            });

}
Run Code Online (Sandbox Code Playgroud)

这个被调用的方法(该服务调用外部api):

public Mono<User> getUserByEmail(String email) {
    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(USER_API_BASE_URI)
            .queryParam("email", email);
    return this.webClient.get()
            .uri(builder.toUriString())
            .exchange()
            .flatMap(resp -> {
                if (Integer.valueOf(404).equals(resp.statusCode().value())) {
                    return Mono.empty();
                } else {
                    return resp.bodyToMono(User.class);
                }
            });
} …
Run Code Online (Sandbox Code Playgroud)

java rx-java spring-boot reactive spring-webflux

13
推荐指数
2
解决办法
3455
查看次数

ES6阻止在切换中使用

在ES6中,我可以实现每个案例的块范围:

switch(somVar){
    case 'first': 
    {
        let itemId='foo'; 
    }
    break; 
    case 'second': 
    { 
        let itemId='bar'; 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

显然,itemId不妨在最高层宣布.
对于我的用例,本地范围的变量更有意义,因为在我的整体代码中,它更容易识别正在发生的事情,并且存在许多case,而一些块包含有问题的变量而其他块则没有.

我没有看到switch/case用作常用的块作用域.
我的问题很简单,是否有理由不这样做,风格明智或其他.

编辑,更新示例代码以避免混淆:

const someFunc(action) => { 
    switch(action.type){ 
        case 'first': 
        { 
            let itemId=action.someObj.someProp.id; 
            //Do something with itemId
        } 
        break; 
        case 'second': 
        { 
            let itemId=action.someObj.someProp.id; 
            //Do something with itemId
        } 
        break; 
        case 'third': 
            //No use of itemId 
    } 
} 
Run Code Online (Sandbox Code Playgroud)

itemId可以在顶部声明,但我更愿意查看每个案例的属性.似乎没有直接的理由在不同的情况下共享变量.对于基本相同的东西来说,"发明"一个不同的名称似乎也是无稽之谈.

这可能以不同的方式编写,但此示例是Flux架构中的常见模式.

javascript ecmascript-6

8
推荐指数
1
解决办法
2112
查看次数