使用react-redux中的connect()方法使用Redux thunk时遇到问题.我有以下代码:
function Component(props) {
return (
<button onClick={props.callback}>Click here</button>
);
}
function actionCreator() {
console.log('#1');
return dispatch => console.log('#2'); // = thunk
}
const mapDispatchToProps = dispatch => ({
callback: actionCreator
});
export const Container = connect(null, mapDispatchToProps)(Component);
Run Code Online (Sandbox Code Playgroud)
当我渲染Container组件并单击按钮时,控制台中仅显示"#1".所以'thunk'没有被执行.
当我明确发送时actionCreator(),它确实有效:
const mapDispatchToProps = dispatch => ({
callback: dispatch => dispatch(actionCreator())
});
Run Code Online (Sandbox Code Playgroud)
如果传递了一个对象,则其中的每个函数都将被假定为Redux动作创建者
那么为什么mapDispatchToProps不是dispatch()我的actionCreator()?我是React的新手,所以也许我不明白吧?
更新
bindActionCreators()从redux 使用它确实有效:
const mapDispatchToProps = dispatch => {
return bindActionCreators({ …Run Code Online (Sandbox Code Playgroud) 我有两个数据库表:用户和学生。学生是用户,但拥有额外的数据(地址)。我的学生模型中有一个 ownTo('User') 关系。
如果我现在像这样查询 Student 模型:
$this->student
->with('user')
->first();
Run Code Online (Sandbox Code Playgroud)
我得到这个结果:
{
"id": 1,
"user_id": 12,
"street": "Petershof",
"house_number": "3787",
"postal_code": "8161 NM",
"city": "Tienhoven",
"user": {
"id": 12,
"email": "bvierdag@yahoo.nl",
"first_name": "Nathan",
"last_name": "van Dijk",
"name": "Nathan van Dijk"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想展平结果,以便用户字段位于父(学生)对象中。像这样:
{
"id": 1,
"user_id": 12,
"street": "Petershof",
"house_number": "3787",
"postal_code": "8161 NM",
"city": "Tienhoven",
"email": "bvierdag@yahoo.nl", // = user field
"first_name": "Nathan", // = user field
"last_name": "van Dijk", // = user field
"name": "Nathan van Dijk" // …Run Code Online (Sandbox Code Playgroud) 我想将ng-model与外部模型服务一起使用.该模型有两种方法:getValue(变量)和setValue(变量).
所以在我的HTML中,我希望能够做到:
<input type="text" ng-model="balance">
Run Code Online (Sandbox Code Playgroud)
注意:我的控制器中的$ scope上没有定义balance.而且因为我们处理的是4000多个不同的变量,所以我不想在$ scope上定义它们.
然后在更改时,它必须调用模型的setValue()方法.所以在我的控制器中我希望有类似的东西:
$catchAllGetter = function(variable) { // e.g. variable = 'balance'
var value = Model.getValue(variable);
return value;
}
$catchAllSetter = function(variable, value) { // called on change
Model.setValue(variable, value);
}
Run Code Online (Sandbox Code Playgroud)
Angular可以这样吗?
说,我在 Vue 中有以下单文件组件:
// Article.vue
<template>
<div>
<h1>{{title}}</h1>
<p>{{body}}</p>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
将此组件导入另一个文件后,是否可以将其模板作为字符串获取?
import Article from './Article.vue'
const templateString = // Get the template-string of `Article` here.
Run Code Online (Sandbox Code Playgroud)
现在templateString应该包含:
<div>
<h1>{{title}}</h1>
<p>{{body}}</p>
</div>
Run Code Online (Sandbox Code Playgroud) 如何使以下功能无点(使用Ramda)?
const prefixAll = R.curry((prefix, list) => R.map(R.concat(prefix), list))
Run Code Online (Sandbox Code Playgroud)