我有一个带参数的函数,bar它有一个默认参数"". 如何bar用值覆盖默认参数undefined?
const foo = (bar = "") => {
console.log(bar)
}
foo(null) // null
foo(undefined) // "" <-- I want this to log `undefined`
Run Code Online (Sandbox Code Playgroud)
如果使用默认参数这是不可能的,那么编写foo实现此目的的合适方法是什么?
我正在请求用户的地理位置navigator.geolocation,但是我被抛出错误:
" https://www.googleapis.com/ "上的网络位置提供商:已返回错误代码400.
Chrome 54.0.2840.98上发生此错误.这在Firefox 50.0上没有问题.我正在使用http-server运行本地服务器
这是功能:
_getLocation: function() {
if (!navigator.geolocation) {
alert('Geolocation is not supported by this browser, maybe use a pop-up and ask user for zipcode?');
}
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
this.setState({
location: {
latitude: latitude,
longitude: longitude
}
});
this._getWeather(this.state.location.latitude, this.state.location.longitude);
}
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
this.setState({
location: 'ERROR(' + err.code + '): ' + err.message …Run Code Online (Sandbox Code Playgroud) 我安装了 nvm 来管理我的节点版本。如果我全局安装一个包,npm install -g fkill然后运行fkill,我会收到错误zsh: command not found: fkill。
以下是一些运行命令以帮助调试此问题
$ npm root -g
/home/jchi/.nvm/versions/node/v10.15.3/lib/node_modules
Run Code Online (Sandbox Code Playgroud)
看看我的路径...
$ echo $PATH
/home/jchi/.pyenv/shims:/home/jchi/.pyenv/bin:/home/jchi/.nix-profile/bin:/home/jchi/.autojump/bin:/home/jchi/.nvm/versions/node/v10.15.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/games
Run Code Online (Sandbox Code Playgroud)
我在我的 PATH 中没有看到 的输出npm root -g。
我认为 nvm 有责任将其添加到我的路径中,以便可以运行全局安装的软件包。所以我看看我的.zshrckickstarts nvm 中有什么。
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
105 [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Run Code Online (Sandbox Code Playgroud)
感谢建议
在webpack的入门页面中,有关webpack-dev-server它的部分下提到-
开发服务器使用webpack的监视模式。它还可以防止webpack将生成的文件发送到磁盘。相反,它会保留并从内存中提供结果文件。
这是否意味着捆绑的文件webpack-dev-server仅保留在内存中,并且我还必须webpack --watch在后台运行以及与dev-server进程一起将编译后的文件实际保存到硬盘中?
我在关于npm peerDependencies警告的stackoverflow上看到了类似的问题-但没有一个问题解决了实际安装依赖项的最佳实践。即,我们现在应该将它们与我们的dependencies和一起保存devDependencies吗?如果是这样,peerDependenciesin 的目的是package.json什么?
安装了其他一些npm软件包后,我收到了一系列警告:
npm WARN slate-prop-types@0.4.32 requires a peer of slate@>=0.32.0 but none is installed. You must install peer dependencies yourself.
Run Code Online (Sandbox Code Playgroud)
因此,我要做的是在中设置一个peerDependencies对象package.json,并包含其要求的内容:
...
"peerDependencies": {
"slate": "0.32.0"
},
...
Run Code Online (Sandbox Code Playgroud)
重新运行npm i,但警告仍然存在。
仅当我将peerDependency包含在devDependencies或依赖项中时,警告才消失,我并不想这样做,因为它使我的项目直接依赖的包变得混乱。
解决这个问题的正确方法是什么?
随着时间的推移,在Gatsby上进行开发的速度显着放慢-有时需要20到30秒来使热加载器刷新页面。在对页面进行10-20次更新后-它最终总是会遇到以下错误:
error UNHANDLED REJECTION
RuntimeError: memory access out of bounds
- source-map-consumer.js:335 BasicSourceMapConsumer._parseMappings
[frontend]/[gatsby]/[react-hot-loader]/[source-map]/lib/source-map-consumer.js:335:44
- source-map-consumer.js:315 BasicSourceMapConsumer._getMappingsPtr
[frontend]/[gatsby]/[react-hot-loader]/[source-map]/lib/source-map-consumer.js:315:12
- source-map-consumer.js:387 _wasm.withMappingCallback
[frontend]/[gatsby]/[react-hot-loader]/[source-map]/lib/source-map-consumer.js:387:57
- wasm.js:95 Object.withMappingCallback
[frontend]/[gatsby]/[react-hot-loader]/[source-map]/lib/wasm.js:95:11
- source-map-consumer.js:371 BasicSourceMapConsumer.eachMapping
[frontend]/[gatsby]/[react-hot-loader]/[source-map]/lib/source-map-consumer.js:371:16
- source-node.js:87 Function.fromStringWithSourceMap
[frontend]/[gatsby]/[react-hot-loader]/[source-map]/lib/source-node.js:87:24
- webpack.development.js:150 onSourceMapReady
[frontend]/[gatsby]/[react-hot-loader]/dist/webpack.development.js:150:61
- next_tick.js:68 process._tickCallback
internal/process/next_tick.js:68:7
Run Code Online (Sandbox Code Playgroud)
我们的网站大约有250多个静态页面/pages,并以编程方式从*.yml生成的文件中构建约30页netlify-cms。
我怀疑它是与sourcemaps变得非常大-野生的猜测是,它可能是与我们正在做什么gatsby-node.js或cms.js对netlify-CMS管理页-虽然我不知道从哪里开始开始调试这样的问题。任何指针将不胜感激。
我们实施的一个临时解决方案是将Gatsby的默认源映射从更改为cheap-module-eval-source-map,eval从而使热模块重新编译时间从4-6秒减少到1-2秒。但是,我们显然希望拥有性能正常的适当源映射。
// For local development, we disable proper sourcemaps to speed up
// performance.
if (stage === "develop") {
config = { …Run Code Online (Sandbox Code Playgroud) 假设我有一个 Fields 类型
export type NodeId =
| "a-index"
| "b-index"
| "c-index"
export type Fields = {
[id in NodeId]?: {
[sectionId: string]: {
[name: string]: string;
};
}
};
Run Code Online (Sandbox Code Playgroud)
为什么第一组出现错误Object is possibly 'undefined',而第二组则没有?
const test: Fields = {};
const id = "a-index";
// Why does this error
let resultA;
if (test[id]) {
resultA = test[id]["somesection"]
^^^^^^^^
error TS2532: Object is possibly 'undefined'.
}
// But this works fine?
let resultB;
if (test["a-index"]) {
resultB = test["a-index"]["somesection"] …Run Code Online (Sandbox Code Playgroud) 注册时,Keycloak 提供了通过 Keycloak 主题的注册页面模板通过“输入”元素添加自定义用户属性的功能,如下所示:
<input id="user.attributes.[the_custom_attribute]" name="user.attributes.[the_custom_attribute]" />
Run Code Online (Sandbox Code Playgroud)
但是,这似乎只有在用户创建帐户并提交表单时才有效。
如果用户通过社交身份提供商(谷歌、微软、github 等)登录,我们如何添加自定义用户属性?
我有一系列已解决的承诺:
[Promise, Promise, Promise, ...]
Run Code Online (Sandbox Code Playgroud)
这些承诺中的每一个在解决时都会返回一个值。如何创建一个函数,将这个承诺数组的映射返回到每个承诺解析到的值?
[value_1, value_2, value_3, ...]
Run Code Online (Sandbox Code Playgroud)
这是我迄今为止最接近的:
const mapPromisesToValues = promises => {
Promise.all(promises, values => {
// how do I return these values from mapPromisesToValues
return values
});
}
Run Code Online (Sandbox Code Playgroud)
更具体地说,我问的原因是因为我有一组 Promise 数组。
const collection_1 = [Promise, Promise, Promise, ...];
const collection_2 = [Promise, Promise, Promise, ...];
//...
Run Code Online (Sandbox Code Playgroud)
并且我想将映射到一组值的这些承诺集合批处理到 1 个对象中,然后我将传递给另一个将被调用一次的函数 - 在这种情况下是 React 的setState:
this.setState({
...this.state,
values_1,
values_2,
//...
});
Run Code Online (Sandbox Code Playgroud) 为了制作动画,stroke-dashoffset我知道使用 CSS@keyframes来移动stroke-dashoffsetSVG 路径。但是,因为我想用 来调整 SVG 的大小background-size: cover,所以我无法定位 SVG 中的各个元素,因为它background-image在 CSS 中被引用为 a 。
有没有办法使用 SVG 的内置<animate />标签来制作动画stroke-dashoffset?
javascript ×3
npm ×2
npm-install ×2
webpack ×2
css ×1
ecmascript-6 ×1
es6-promise ×1
gatsby ×1
geolocation ×1
keycloak ×1
netlify-cms ×1
node.js ×1
nvm ×1
reactjs ×1
svg ×1
typescript ×1