根据Reactjs.org处理事件并防止在代码下面使用默认值:
function ActionLink() {
function handleClick(e) {
e.preventDefault();
console.log('The link was clicked.');
}
return (
<a href="#" onClick={handleClick}>
Click me
</a>
);
}
Run Code Online (Sandbox Code Playgroud)
但是,这还需要在构造函数中添加绑定,如:
this.handleClick = this.handleClick.bind(this);
Run Code Online (Sandbox Code Playgroud)
我能够通过以下代码获得所需的行为:
<span>
<a href="#" onClick={()=>doSomething(arg1,agr2)}>Click here</a>
</span>
Run Code Online (Sandbox Code Playgroud)
问题:哪一个是更好的选择?似乎第一个需要使用有状态组件而第二个选项可以做任何事情而不管组件是有状态的还是无状态的.
由于异步总是返回promise,因此我们必须解决它才能获得价值。我需要导出它的值(返回的对象),以便我们可以在另一个模块中使用它。
export const getClient = async () => {
return await HttpService.getValueFromSettings('durl').then((response) => {
if(isValidResponse(response)) {
endpoint = response.data;
export const client = createClient(response.data);
//This is where getting error that we can not export in side the function
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
我需要在另一个模块中使用客户端。
我尝试在调用之前声明和初始化客户端对象,但没有成功:
export let client = null;
export const getClient = async () => {
return await HttpService.getValueFromSettings('durl').then((response) => {
if(isValidResponse(response)) {
endpoint = response.data;
client = createClient(response.data);
return client;
}
}
})
}
Run Code Online (Sandbox Code Playgroud) Docker 新手。我正在 Win 10 计算机上运行 Visual Studio 2019 社区。安装了 Docker 桌面并创建了两个解决方案(service1 和 service2)。我正在尝试在自己的容器上运行这两个解决方案。
我能够使用以下命令构建并运行 service1:
docker run -it --rm -p 3000:80 --name mymicroservicecontainer mymicroservice
Run Code Online (Sandbox Code Playgroud)
请问3000:80是什么?80是端口吗?因为我能够使用浏览器中的http://localhost:3000/api/product/1运行我的 api 。
接下来,我尝试通过以下方式在它自己的容器上运行 service2:
docker run -it --rm -p 2000:80 --name myanotherservicecontainer myanotherservice
Run Code Online (Sandbox Code Playgroud)
由于端口是 2000,我想它应该可以工作,但是我收到以下错误:
docker: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:2000: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
Run Code Online (Sandbox Code Playgroud)
time="2020-04-08T14:22:41-04:00" level=error msg="等待容器时出错:上下文已取消"
那是因为我有 :80 与 service1 相同吗?解决办法是什么?我在命令提示符下以管理模式运行命令。 …
该项目使用 ReactJS、Typescript、Webpack 和 Jest。\n为了实现模块解析(以最小化导入),我们进行了以下更改:
\n\nTSConfig.js:
"compilerOptions": { "baseUrl": "src",}
Webpack.config.js
alias: {\n Common: path.resolve(__dirname, \'src/Common/\'),\n},\nRun Code Online (Sandbox Code Playgroud)\n\n代码工作正常,但是 Jest 测试开始失败并出现错误:
\n\nCannot find module \'Common/js/utils\' from \'MyContact.ts\'
这是因为,Jest 还不知道如何将 Common 解析为 Src/common,而不是在 node_module 中查找。为了解决这个问题,进行了以下更改:
\n\njestConfig.js
moduleNameMapper: {"Common": "<rootDir>/src/Common"}
这似乎已经解决了,但我还有另一个问题[这就是我在这里的原因:)]
\n\nJest 遇到意外令牌
\n\nThis usually means that you are trying to import a file which Jest cannot parse, e.g. it\'s not plain JavaScript.\n\nBy default, if Jest sees a Babel config, it will use …Run Code Online (Sandbox Code Playgroud) javascript ×2
async-await ×1
asynchronous ×1
docker ×1
jestjs ×1
reactjs ×1
typescript ×1
webpack ×1