我在React中使用ES6语法,并编写如下组件:
export default class Loginform extends React.Component {
getInitialState() {
return {
name: '',
password: ''
};
};
}
Run Code Online (Sandbox Code Playgroud)
但浏览器引发了我的警告:
警告:getInitialState是在Loginform上定义的,这是一个普通的JavaScript类.这仅适用于使用React.createClass创建的类.你的意思是改为定义一个州财产吗?
我可以用传统的语法来处理它,var Loginform = React.createClass但是什么是正确的ES6语法?
另一个小东西,我认为在传统语法中React.createClass是一个对象,所以它中的函数用逗号分隔,但是对于extends它需要分号的类,我不太了解它.
在某些情况下,当我从promise对象获得返回值时,我需要then()根据值的条件启动两个不同的precesses,例如:
promise().then(function(value){
if(//true) {
// do something
} else {
// do something
}
})
Run Code Online (Sandbox Code Playgroud)
我想也许我可以这样写:
promise().then(function(value){
if(//true) {
// call a new function which will return a new promise object
ifTruePromise().then();
} else {
ifFalsePromise().then();
}
})
Run Code Online (Sandbox Code Playgroud)
但有了这个,我有两个问题:
我不确定在承诺中开始一个新的承诺 - 然后处理是否是一个好主意;
如果我需要两个进程在最后调用一个函数怎么办?这意味着他们拥有相同的"终端"
我试图回复新的承诺,保持原来的链:
promise().then(function(value){
if(//true) {
// call a new function which will return a new promise object
// and return it
return ifTruePromise();
} else {
// do something, no new promise
// hope to stop …Run Code Online (Sandbox Code Playgroud) 在React中,状态不会立即更新,因此我们可以使用回调setState(state, callback).但是如何在Redux中做到这一点?
在调用之后this.props.dispatch(updateState(key, value)),我需要立即对更新后的状态做一些事情.
有什么方法可以用最新状态调用回调,就像我在React中做的那样?
我正在按照一些教程来构建一个具有表达和反应的同构应用程序.我对webpack-dev-server感到困惑.
webpack教程讲述了webpack-dev-server:
这将绑定localhost:8080上的一个小型快速服务器,它为您的静态资产和捆绑包(自动编译)提供服务.
它在重新编译包时自动更新浏览器页面(socket.io).在浏览器中打开http:// localhost:8080/webpack-dev-server/bundle.
既然我有快递服务器,我真的需要webpack-dev-server吗?或者使用它的优点和缺点是什么?如果我想使用react-hot-loader,那么webpack-dev-server是否必要?
我正在使用Babel 6 for es2015并做出反应,需要babel-preset-es2015和babel-preset-react.
我添加了presets属性,.babelrc但它给我一个错误:
ERROR in ./src/client/entry.js
Module build failed: ReferenceError: [BABEL] /Users/brick/Dropbox/learncoding/node.js/isomorphic/src/client/entry.js: Unknown option: /Users/brick/Dropbox/learncoding/node.js/isomorphic/.babelrc.presets
at Logger.error (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/logger.js:58:11)
at OptionManager.mergeOptions (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:126:29)
at OptionManager.addConfig (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:107:10)
at OptionManager.findConfigs (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:168:35)
at OptionManager.init (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:229:12)
at File.initOptions (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/index.js:147:75)
at new File (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/index.js:137:22)
at Pipeline.transform (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/pipeline.js:164:16)
at transpile (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-loader/index.js:12:22)
at Object.module.exports (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-loader/index.js:69:12)
@ multi main
Run Code Online (Sandbox Code Playgroud)
我的.babelrc档案是:
{
"presets": [
"es2015",
"react"
]
}
Run Code Online (Sandbox Code Playgroud)
我可以运行babel src -d lib命令,它的工作原理.但如果我跑来npm start运行babel通道 …
我刚发现,如果我使用new Date('2015-1-1'),时间没有时区效应,但如果我new Date('2015-01-01')在Node.js中使用时间有时区效果.
我输出4 Date():
console.log(new Date('2015-1-1'));
console.log(new Date('2015-01-1'));
console.log(new Date('2015-1-01'));
console.log(new Date('2015-01-01'));
Run Code Online (Sandbox Code Playgroud)
输出是
Thu Jan 01 2015 00:00:00 GMT+0800 (CST)
Thu Jan 01 2015 00:00:00 GMT+0800 (CST)
Thu Jan 01 2015 00:00:00 GMT+0800 (CST)
Thu Jan 01 2015 08:00:00 GMT+0800 (CST)
Run Code Online (Sandbox Code Playgroud)
你可以看到最后一次是08:00:00因为我在+8时区.
我认为输出取决于月份数或日期数.当它是10,11或12时,输出总是如此08:00:00
我想知道为什么以及是否有更好的方法来处理这个,除了手动检查月份和日期的位数?
我想用来Promise.all()处理两个promise对象,但第二个是内部一个if表达式。如何处理这种情况?
它看起来像这样:
functionA();
if(true) {
functionB();
}
Run Code Online (Sandbox Code Playgroud)
functionA()并且functionB()都返回一个 promise 对象。在正常情况下,我可以使用
Promise.all([
functionA(),
functionB()
]).then(resule => {
console.log(result[0]); // result of functionA
console.log(result[1]); // result of functionB
})
Run Code Online (Sandbox Code Playgroud)
但是如何处理if表达式呢?我应该if(true){functionB()}用 a包裹new Promise()吗?
我在TypeScript中使用队列lib Bull。其定义是:
node_modules/@types/bull/index.d.ts
declare const Bull: {
(queueName: string, opts?: Bull.QueueOptions): Bull.Queue;
// something like above
};
declare namespace Bull: {
interface Queue {}
interface Job {}
// some other non-exported interfaces
}
export = Bull
Run Code Online (Sandbox Code Playgroud)
我想Bull在我的库中合并命名空间,并在另一个应用程序中使用它。
node_modules / myLib / index.d.ts
import { Queue } from 'bull'
declare namespace Bull: {
export interface Queues {}
}
export interface myInterface {
foo: Queue | Bull.Queues
}
export = Bull
Run Code Online (Sandbox Code Playgroud)
myApp / foo.ts
import { Job, Queues } from …Run Code Online (Sandbox Code Playgroud) 我想检查Node.js中是否存在集合.我使用db.collectionNames获取数据库中的名称列表但没有任何反应.代码:
connectDB(DBURL).then(function(db) {
console.log('db connect ok');
db.collectionNames('test', function(err, collectionNames) {
console.log('get collection names');
if(err) console.log(err);
else console.log(collectionNames);
});
}, function(err) {
console.log(err);
});
Run Code Online (Sandbox Code Playgroud)
这connectDB(DBURL)是一个承诺对象,它完美地运作.输出:
app-0 try to connect db
app-0 db connect ok
Run Code Online (Sandbox Code Playgroud)
你可以看到函数中没有任何输出collectionNames.我不知道为什么.
我可以通过以下方式获取Mongo shell中的集合名称db.getCollectionNames:
> db.getCollectionNames()
[ "system.indexes", "test" ]
Run Code Online (Sandbox Code Playgroud) 我跟着这个文档上的CentOS 7安装的MongoDB 3.2.1安装完成后,我改变了所有者和组var/lib/mongo和var/log/mongodb/mongod.log到root:root.
当我启动mongodb时service mongod start,它就会显示出来
Starting mongod (via systemctl): Job for mongod.service failed. See 'systemctl status mongod.service' and 'journalctl -xn' for details.[FAILED]
Run Code Online (Sandbox Code Playgroud)
我已经运行了两个命令来显示细节.
systemctl status mongod.service 节目
mongod.service - SYSV: Mongo is a scalable, document-oriented database.
Loaded: loaded (/etc/rc.d/init.d/mongod)
Active: failed (Result: exit-code) since Wen 2016-01-27 18:32:46 CST; 14s ago
Process: 24913 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=1/FAILURE)
Main PID: 23711 (code=exited, status=0/SUCCESS)
1? 27 18:32:45 server1 systemd[1]: Starting SYSV: Mongo is a …Run Code Online (Sandbox Code Playgroud) 我正在尝试向mouseenter元素添加事件监听器以更改其子节点的不透明度.我for用来添加监听器,并且可以正确输出子节点,但在函数中它是"未定义的".我不知道为什么.
HTML:
<article class="index-image-article rel">
<section class="index-image-info">
// some elements
</section>
<div class="index-image-excerpt mask table">
// some elements
</div>
</article>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
var indexImageArticle = document.querySelectorAll(".index-image-article");
var indexImageInfo = document.querySelectorAll(".index-image-info");
var indexImageExcerpt = document.querySelectorAll(".index-image-excerpt");
for(var i = 0; i < indexImageArticle.length; i++) {
console.log(indexImageInfo[i]); // output properly
indexImageArticle[i].addEventListener("mouseenter", function() {
indexImageInfo[i].style.opacity = 1;
indexImageExcerpt[i].style.opacity = 0;
});
}
Run Code Online (Sandbox Code Playgroud)
输出console.log是:
<section class=?"index-image-info">?…?</section>?
<section class=?"index-image-info">?…?</section>?
<section class=?"index-image-info">?…?</section>?
<section class=?"index-image-info">?…?</section>
Run Code Online (Sandbox Code Playgroud)
和错误:
Uncaught TypeError: Cannot read property 'style' of …
node.js ×5
javascript ×4
mongodb ×2
promise ×2
reactjs ×2
asynchronous ×1
babeljs ×1
bull.js ×1
centos ×1
centos7 ×1
collections ×1
date ×1
ecmascript-6 ×1
express ×1
redux ×1
typescript ×1
webpack ×1