例如,您可以使用角度指令,如下所示:
angular.module('app')
.directive('classy', function() {
return {
restrict: 'A',
link: function($scope, $el) {
$el.addClass('stay-classy');
}
}
}
Run Code Online (Sandbox Code Playgroud)
并执行如下:
<div classy></div>
Run Code Online (Sandbox Code Playgroud)
在阅读了大多数文档和谷歌搜索之后,我在React中似乎没有相应的内容.我希望有类似的东西:
...
render: function() {
return (
<MyComponent classy></MyComponent>
);
}
Run Code Online (Sandbox Code Playgroud)
有没有这样的事情我可能会失踪?是否有不同但功能相似的等价物?或许这个问题只是表明我错过了"反应方式"的某些部分,我不应该这样做.谢谢!
我在React 0.14中有一个无状态功能组件,它在React 0.13中工作,但现在返回以下错误:
render在返回的组件实例上找不到任何方法:您可能忘记定义render,从无状态组件返回null/false,或者尝试呈现类型为不是React组件的函数的元素.
这是我的组成部分:
function ToggleDisplay(props) {
//should render a <noscript> per React's implementation
if(props.if === false) {
// return <noscript></noscript>; //do I have to do this manually now?
return null;
}
let style = {};
if(shouldHide(props)) {
style.display = 'none';
}
return (
<span style={style} {...props} />
);
}
Run Code Online (Sandbox Code Playgroud)
我现在必须手动退回<noscript>吗?还有另一种方法可以在无状态组件中返回null吗?
我试图以可读格式返回字符串的前5个单词,没有""或逗号分隔单词.我不确定它是一个正则表达式的东西还是什么,但我想不出来虽然它可能很简单.谢谢!
看看我到目前为止:http: //jsfiddle.net/ccnokes/GktTd/
这是我正在使用的功能:
function getWords(string){
var words = string.split(/\s+/).slice(1,5);
return words;
}
Run Code Online (Sandbox Code Playgroud) 在我的远程服务器(Ubuntu 14.04 x64)上,每当我尝试uglify我的包时,该过程只返回"Killed".当我不丑化时,它就没事了.
有没有人碰到这个?当我在我的本地Mac上这样做时,它很好(虽然我只是测试了它,花了1.4分钟).
这是我的webpack.config:
var webpack = require('webpack');
function makeConfig(opts) {
var config = {
entry: {
app: ['./public/scripts/main.js'],
vendor: ['lodash', 'react', 'react/lib/ReactCSSTransitionGroup', 'react-router', 'reqwest', 'd3']
},
stats: {
colors: true,
reasons: true
},
output: {
devtool: (opts.env === 'dev' ? '#eval-source-map' : ''),
path: 'dist/scripts',
filename: '[name].bundle.js'
},
plugins: [
new webpack.DefinePlugin({
ENV: opts.env
}),
new webpack.optimize.CommonsChunkPlugin('vendor.bundle.js')
],
module: {
loaders: [
{ test: /\.jsx?$/, loader: 'jsx-loader' }
]
}
};
if(opts.env === 'prod') {
config.plugins.push(
new webpack.optimize.UglifyJsPlugin(), …Run Code Online (Sandbox Code Playgroud) 我有一个简单的React组件,它接受一个隐藏/显示组件子项的prop.我使用style属性隐藏/显示display: none.如果我使用display: none !important,组件在收到新道具时不再重新渲染.
在render方法中,它看起来像这样:
var style = {};
if(shouldHide) {
//this works
style = {
display: 'none'
};
//this does not
//style = {
// display: 'none !important'
//};
}
return (
<span style={style} {...this.props} />
);
Run Code Online (Sandbox Code Playgroud)
以下是完整的示例:https://jsfiddle.net/ccnokes/LnrnrLy2/(相关行从第27行开始)
这里发生了什么?我错过了什么吗?
在d3.js图表中,x轴线(条形和条形标签之间的黑线)排序默认为:| ---------------- |,见截图下面:

我如何将其更改为一条直线(两端没有垂直线)?
查看生成的SVG,此代码似乎是确定样式:<path class="domain" d="M0,6V0H824V6"></path>,由D3自动生成.
谢谢!
在我的Ubuntu VM上的项目上从本地安装运行Karma时,Karma将无法运行,因为PhantomJS无法启动.
版本信息:
同样的脚本在我的Mac OS上运行得很好(我在运行它之前删除了全局的业力安装,但它仍然有效).
在Ubuntu上,如果我直接通过./node_modules/phantomjs/bin/phantomjs启动phantomjs,它就会正常启动.
这是shell输出:
> ./node_modules/karma/bin/karma start test/karma.conf.js
DEBUG [config]: autoWatch set to false, because of singleRun
DEBUG [plugin]: Loading karma-* from /srv/detectiveui/node_modules
DEBUG [plugin]: Loading plugin /srv/detectiveui/node_modules/karma-jasmine.
DEBUG [plugin]: Loading plugin /srv/detectiveui/node_modules/karma-phantomjs-launcher.
DEBUG [plugin]: Loading plugin /srv/detectiveui/node_modules/karma-spec-reporter.
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
DEBUG [temp-dir]: Creating temp dir at /tmp/karma-42625347
DEBUG [launcher]: /srv/detectiveui/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/bin/phantomjs /tmp/karma-42625347/capture.js
DEBUG [launcher]: Process PhantomJS exited with code 2 …Run Code Online (Sandbox Code Playgroud) 我对这整个"节点"js的事情有点新鲜.在尝试从http://mean.io/安装MEAN软件包时,我忠实地遵循了以下步骤:
我"cd"到我的目录,运行"npm install",然后"grunt"命令,并得到此错误:
Error: Cannot find module 'coffee-script'
Run Code Online (Sandbox Code Playgroud)
所以我用拳打滚.npm安装coffee-script,再次运行"grunt".
Error: Cannot find module 'getobject'
Run Code Online (Sandbox Code Playgroud)
有耐心,我决定"好吧,忘了咕噜".我运行"节点服务器".然后我收到这个错误:
Error: Cannot find module 'merge-descriptors'
Run Code Online (Sandbox Code Playgroud)
怎么了?我怎样才能让MEAN在这里工作?(我认为npm应该让生活更轻松).
更新12/31/13:当我查看npm install的输出时,会出现一些"Error:EMFILE"错误.据我所知,这些错误与打开的文件太多有关,而graceful-fs是解决这个问题的方法.下面粘贴了示例EMFILE错误:
npm ERR! System Darwin 13.0.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/cnokes/Desktop/mean-master
npm ERR! node -v v0.10.7
npm ERR! npm -v 1.2.21
npm ERR! path /Users/cnokes/Desktop/mean-master/node_modules/grunt/node_modules/iconv-lite/README.md
npm ERR! code EMFILE
npm ERR! errno 20
npm ERR! tar.unpack error reading /var/folders/wv/xrm8qphn5bsgfj5w45kqrwl9lgq2j2/T/npm-27019/1388513549132-0.22752901865169406/tmp.tgz
Run Code Online (Sandbox Code Playgroud)
安装了graceful-fs后,我重新运行npm install并得到以下几个错误:
npm ERR! Error: ENOENT, utime '/Users/cnokes/Downloads/mean-master2/node_modules/express/node_modules/fresh/index.js'
Run Code Online (Sandbox Code Playgroud) 我正在玩一个递归生成器函数,它异步返回值。我正在使用协程包装函数来调用它。代码和JSBin如下:
http://jsbin.com/nuyovay/edit?js,控制台
let log = console.log.bind(console);
let err = console.error.bind(console);
function coroutine(generatorFn){
return function co() {
let generator = generatorFn.apply(this, arguments);
function handle(result) {
console.log(result);
if (result.done) {
return Promise.resolve(result.value);
}
return Promise.resolve(result.value)
.then(
res => handle(generator.next(res)),
err => handle(generator.throw(err))
);
}
try {
return handle(generator.next());
} catch (err) {
return Promise.reject(err);
}
};
}
function sleep(dur) {
return new Promise(res => {
setTimeout(() => { res() }, dur);
});
}
function* recurse(limit = 5, count = 0) {
if(count …Run Code Online (Sandbox Code Playgroud) javascript ×3
reactjs ×3
arrays ×1
d3.js ×1
ecmascript-6 ×1
gulp ×1
jquery ×1
karma-runner ×1
node.js ×1
phantomjs ×1
recursion ×1
string ×1
svg ×1
ubuntu-14.04 ×1
webpack ×1