小编san*_*a-p的帖子

React - 如何动态创建属性

我有2个属性连接到2道具:aria-hiddenaria-label.
什么时候aria-hidden是真的,它只显示一个.如果它是假的,它只显示aria-label.

我写了这段代码,但它并不干......我怎样才能改进它?

 render() {
     let svgMarkup = '';

        if (this.props.hidden) {
            svgMarkup = (
                <svg role="img" aria-hidden="true">
                    ...
                </svg>
            );
        } else {
            svgMarkup = (
                <svg role="img" aria-label={ this.props.label }>
                    ...
                </svg>
            );
        }

        return svgMarkup;
 }
Run Code Online (Sandbox Code Playgroud)

wai-aria reactjs react-jsx

3
推荐指数
1
解决办法
777
查看次数

动态更改 Vue JS 2 上的父元素标签

如何根据道具条件在不同的父元素之间切换,保持其内容相同?

示例:如果ItemisRouterprop,则渲染router-link所需的属性,否则渲染一个简单的li元素。

/* Item.vue */

<router-link v-if="isRouter" tag="li" :to="to" active-class="is-active">
<li v-else>
    /* some html */
    <slot></slot>
    /* more html */
</li>
</router-link>

// View.vue
<list>
  <item isRouter to="/new">
    Breaking News
  </item>
  <item>
    Orange
  </item>
</list>
Run Code Online (Sandbox Code Playgroud)

这是可能的吗?你建议我遵循什么方法?

javascript vue-component vuejs2

3
推荐指数
1
解决办法
2980
查看次数

gulp-include与gulp-uglify不起作用

我有一个gulp.task 'scripts'使用gulp-uglify.然后我也补充gulp-import说它没用.如果我这样做只会uglify起作用.如果我这样做只会import起作用.但如果我同时做两件事都行不通.这是功能:

gulp.task('scripts', function(){
    gulp.src([
            'testgulp/**/*.js', '!testgulp/**/_*.js', '!testgulp/**/*.min.js'
        ])
        .pipe(include()) //if I comment only this line, uglify works
            .on('error', console.log)
        .pipe(uglify()) //if I comment only this line, include works
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest(function(file) { return file.base; }));

});
Run Code Online (Sandbox Code Playgroud)

然后在终端上我尝试这两个时都有这个响应:

/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/map-stream/index.js:103
        throw err
        ^
GulpUglifyError: unable to minify JavaScript
    at createError (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/lib/create-error.js:6:14)
    at wrapper (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/node_modules/lodash/_createHybrid.js:87:15)
    at trycatch (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/minifier.js:26:12)
    at DestroyableTransform.minify [as _transform] (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/gulp-uglify/minifier.js:76:19)
    at DestroyableTransform.Transform._read (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:159:10)
    at DestroyableTransform.Transform._write (/Applications/MAMP/htdocs/git/personal-sandrina-p/_main/v3/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:147:83)
    at …
Run Code Online (Sandbox Code Playgroud)

javascript gulp gulp-uglify

2
推荐指数
1
解决办法
2247
查看次数

React:在不使用this.props.children的情况下将组件作为prop传递

我有一个组件Foo.js:

// a svg component with a star.svg icon
import { IconStar } from 'react-svg-icons'; 

// call a button with a custom icon
<Button icon={ IconStar }> Click to trigger </Button>
Run Code Online (Sandbox Code Playgroud)

然后button.js我有:

render() {
  const theIcon = this.props.icon;
  return (
    <button>
      {this.props children}
      {icon() /*this works but i can't pass props inside it*/} 
      <theIcon className={ Styles.buttonIcon } />
    </button>
  )
}
Run Code Online (Sandbox Code Playgroud)

我想在<IconStar>里面渲染<Button>,我该怎么做?

我不能通过这个,this.prop.children因为它在图标道具周围有更多的逻辑,但在这种情况下我只显示一个演示.

谢谢

javascript jsx reactjs

2
推荐指数
1
解决办法
3741
查看次数

Jest 模拟 document.activeElement

我有一个使用 DOM 的函数

const trap = {
  // ...
  init() {
    if (document.activeElement === this.firstTabStop) {
      return this.lastTabStop.focus();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

module.exports = trap.init;

我尝试模拟document.activeElement但它抛出了一个错误。

global.document.activeElement = mockFirstTabStop;
Run Code Online (Sandbox Code Playgroud)

mockFirstTabStop 只是功能模拟,但无论我放在哪里,错误都是一样的。

类型错误:无法设置只有 getter 的 [object Object] 的属性 activeElement

那么,我如何测试条件期望this.lastTabStop.focus()被调用?

javascript dom unit-testing jestjs

2
推荐指数
1
解决办法
6633
查看次数

ES6模板文字 - 从字符串中删除\n

我正在将我的多行变量更改为Template Literals令人惊讶,但后来我注意到我所做的缩进被转换(缩小)到\n我在原始代码上做的缩进.我怎么能避免这种情况?

例如:

var $div = $(`<div class='proj' id='projects'>
                 <div class='bot-nav'>${txt}</div>
           </div>`);
Run Code Online (Sandbox Code Playgroud)

它被转换为:

var $div = $("<div class='proj' id='projects'>\n                 <div class='bot-nav'>"+txt+"</div>\n           </div>");
Run Code Online (Sandbox Code Playgroud)

我想要这个:

var $div = $("<div class='proj' id='projects'><div class='bot-nav'>"+txt+"</div></div>");
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

javascript variables ecmascript-6

1
推荐指数
2
解决办法
4217
查看次数

交叉路口观察者不与兄弟姐妹一起工作

我有一个带有 4 个孩子的 flexbox 容器,每个孩子都有with:50%.

我想为每个孩子创建一个 IO(Intersection Observer),但由于某种原因,它只适用于 1º 和 3º 元素......但是如果我为 2º 和 4º 元素创建一个新的 IO,它就可以工作。

我究竟做错了什么?

这是演示:https : //codepen.io/sandrina-p/pen/mddzpzW

javascript intersection-observer

1
推荐指数
1
解决办法
518
查看次数