我有一个用于state向用户提供数据的组件.例如<div>this.state.variableInState</div>.该组件可以调度某些方法(例如on onClickaction).我目前使用react-redux的connect方法映射store到props.setState派遣后我有办法吗?
// actions
export function executeAction() {
return function (dispatch, getState) {
dispatch({
type: 'MY_ACTION',
payload: axios.get('/some/url')
});
};
}
// reducer
export default function (state = {}, action) {
switch (action.type) {
case 'MY_ACTION_FULFILLED':
return {...state, myVariable: action.payload.data}
}
}
//component
class MyComponent extends Component {
render() {
(<div onClick={this.props.executeAction.bind(this)}>
{this.state.variableInState}
</div>)
}
someOtherMethod() {
// I want to operate with state here, …Run Code Online (Sandbox Code Playgroud) 我正在创建一个 npm 包,它默认导出一个类。这个模块是用es3写的,但是我想提供types.ts
index.js(在我的包中)
function MyClass(p1, p2) {
// code
}
module.exports = MyClass;
Run Code Online (Sandbox Code Playgroud)
好的,然后我创建了类型index.d.ts(在我的包中,除了 index.js):
declare module 'mypackageid' {
interface MyClass {
new (div: HTMLElement, conf: any): MyClass;
}
export var MyClass: MyClass;
}
Run Code Online (Sandbox Code Playgroud)
我可以轻松地在 es3 中使用此代码并且它有效。(在我的包裹之外,例如来自另一个包裹)
var myInstance = new MyClass()
Run Code Online (Sandbox Code Playgroud)
但是如果我想从 .ts 文件中的另一个包中使用它:
import MyClass from 'mypackageid';
new MyClass(divReference, { // <-- error points to this line
onBlobPaste: e=> {
console.log(e);
},
});
Run Code Online (Sandbox Code Playgroud)
打字稿加载器拒绝编译和打印:
TS2351:不能对类型缺少调用或构造签名的表达式使用“new”。
编辑 1:
您在 .ts 文件中使用了 commonjs 默认导出,您的 …
如何分别使用不同的参数使 vue-router 保持活动状态?
电话:DR:
让我们考虑一个例子,当我们正在开发一个像 facebook 这样的网站时。每个用户都有一个个人资料页面。因为有很多用户,我们不想迭代所有用户并在加载时加载所有个人资料页面,如下所示
<template v-for="profile in profilePages">
<profile-page :data="profile" v-show="this.route.params['id'] === channel.id"/>
</template>
Run Code Online (Sandbox Code Playgroud)
常见的方法是:
路由器.js:
{
component: ProfileWrapper,
path: '/profile',
children: [
{
path: ':id',
component: ProfilePage
}
]
}
Run Code Online (Sandbox Code Playgroud)
频道页面:
<keep-alive>
<router-view :=key="$route.fullPath"></router-view>
</keep-alive>
Run Code Online (Sandbox Code Playgroud)
但问题就在这里。由于用户访问某人的页面并导航离开,我希望路由器将其保持在缓存中的某个地方,或者只是将其隐藏。在我的特殊情况下,用户最多访问 2-3 个配置文件并在它们之间切换很多。而且切换操作很费时间,因为里面有很多DOM。
我可以用vue-router和keep-alive来做吗?
编辑:
请检查沙箱。每次在页面(#1、#2、#3、#4)之间切换时,Vue都会ProfileInnerComponent从头开始创建新组件(而不是像 v-show 那样从缓存中创建)。通过检查红色 div 可以明显看出,调用了 的create钩子ProfileInnerComponent,它发出事件,并App添加具有当前时间的 div。
如何使用Mocha测试异步代码?我想使用多个await内部摩卡咖啡
var assert = require('assert');
async function callAsync1() {
// async stuff
}
async function callAsync2() {
return true;
}
describe('test', function () {
it('should resolve', async (done) => {
await callAsync1();
let res = await callAsync2();
assert.equal(res, true);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
这将产生以下错误:
1) test
should resolve:
Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.
at Context.it (test.js:8:4)
Run Code Online (Sandbox Code Playgroud)
如果删除done(),我得到:
1) test
should resolve:
Error: Timeout of 2000ms exceeded. For async tests and …Run Code Online (Sandbox Code Playgroud) 考虑以下情况:
有没有办法只恢复 1 个线程?演示
有没有办法将 css 类应用于所有字段,而不是对每个字段执行以下操作。
表格.py
class UserColorsForm(forms.ModelForm):
class Meta:
model = UserColors
exclude = ('userid',)
widgets = {
'text_color': forms.TextInput(attrs={'class': 'color'}),
'background_color': forms.TextInput(attrs={'class': 'color'}),
... 10 more
}
Run Code Online (Sandbox Code Playgroud) 按照django 文档,我尝试在我的models.py 中做一个选择字段 :
class MyModel(Model):
ACTION_CHOICES = (
('d', 'delete'),
('c', 'create'),
('u', 'update'),
)
action = CharField(max_length=1, choices=ACTION_CHOICES)
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时
action = MyModel(action='create')
action.save()
Run Code Online (Sandbox Code Playgroud)
并检查数据库的动作不是c,而是create。此外,即使对 max_length 的限制也不会对它产生影响。好的,我继续前进并尝试以这种方式制作它:
模型.py
class MyModel(Model):
ACTION_CHOICES = (
(1, 'delete'),
(2, 'create'),
(3, 'update'),
)
action = IntegerField(max_length=1, choices=ACTION_CHOICES)
Run Code Online (Sandbox Code Playgroud)
代码.py:
action = MyModel(action='create')
action.save()
Run Code Online (Sandbox Code Playgroud)
但是我得到一个错误,str 字段不能像 int 一样保存(数据库有 int 字段,但 MyModel.action 是 str)我在这里想念什么?选择不是应该在保存时转换价值吗?
此致,
如何等待 docker 中创建文件?我正在尝试下面的代码,但它不起作用。如果我bash -c [ -f /tmp/asdasdasd ]与 docker shell 分开执行,它会给出正确的结果。
Dockerfile测试:
FROM alpine:3.6
RUN apk update && apk add bash
Run Code Online (Sandbox Code Playgroud)
docker-compose.yml:
version: '2.1'
services:
testserv:
build:
context: .
dockerfile: ./Dockerfiletest
command:
bash -c "rm /tmp/a && sleep 5 && touch /tmp/a && sleep 100"
healthcheck:
# I tried adding '&& exit 1', '|| exit `' it doesn't work.
test: bash -c [ -f /tmp/a ]
timeout: 1s
retries: 20
Run Code Online (Sandbox Code Playgroud)
docker-compose up+ 等待 10 秒 …
前奏:
这是显示ImageField的最简单方法.让我们假设我的表单中有10个字段,我不想迭代它们template.html只是为了检查它是否是一个imageField并以不同的方式显示它.我想通过形式或插件之类的东西来处理它这个.所以我想要离开template.html,因为它在下面.
template.html
<table>
{{ form.as_table }}
</table>
Run Code Online (Sandbox Code Playgroud)
并向models.py添加一个image_tag方法:
class UserProfile(Model):
photo = ImageField(upload_to=PHOTO_DIRECTORY)
contacts = TextField(null=True)
... others
def image_tag(self):
return u'<img src="%s" />' % self.photo.url
image_tag.short_description = 'Image'
image_tag.allow_tags = True
Run Code Online (Sandbox Code Playgroud)
forms.py:
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('contacts', 'photo', 'image_tag', ...others)
Run Code Online (Sandbox Code Playgroud)
我得到了一个Unknown field(s) (image_tag) specified for UserProfile公平的错误.
我也尝试将它放入,readonly_fields但它们不会以某种方式显示.我是否必须创建list_view?如果是的话应该在哪里?
我在这里想念什么?为什么该image_tag方法应该是模型的一部分而不是形式?模型描述了数据应该如何在数据库中持久化,而不是如何通过表单呈现给用户.这应该是形式的一部分,不应该吗?如果我错了,请纠正我.我该如何解决这个问题?任何帮助将不胜感激!
我有<input type="file"我的网页上。我想在 chrome 开发工具中查看文件的按钮样式。不幸的是,如果我使用这个元素,它会显示元素整个输入的样式(我的意思是文本 + 按钮)。我想检查样式::-webkit-file-upload-button. 例如,Firefox显示选择元素时的所有样式。有什么办法可以在 chrome 中做到这一点吗?
下面的图片代表一个例子。很明显,图像中的按钮使用 进行了样式设置::-webkit-file-upload-button,但样式列表中没有background-color/ bacgrkound-image。如何查看 webkit-element 的样式?

django ×3
django-forms ×2
css ×1
debugging ×1
docker ×1
javascript ×1
mocha.js ×1
pdb ×1
pycharm ×1
react-redux ×1
reactjs ×1
redux ×1
typescript ×1
vue-router ×1
vue.js ×1