我想import fs from 'fs'在JavaScript中使用.这是一个示例:
import fs from 'fs'
var output = fs.readFileSync('someData.txt')
console.log(output)
Run Code Online (Sandbox Code Playgroud)
我运行文件时遇到的错误node main.js是:
(function (exports, require, module, __filename, __dirname) { import fs from 'fs
'
^^^^^^
SyntaxError: Unexpected token import
Run Code Online (Sandbox Code Playgroud)
我应该在节点中安装什么才能从其他地方导入模块和功能?
我有以下代码块:
class App extends Component {
constructor(props) {
super(props);
this.state = {
avatar: '',
...some more data...
}
this.fetchUser = this.fetchUser.bind(this);
}
render() {
return (
<div>
<SearchBox onSubmit={this.fetchUser}/>
<Card data={this.state} />
<BaseMap center={this.state.address}/>
</div>
);
}
componentDidMount() {
function fetchUser(username) {
let url = `https://api.github.com/users/${username}`;
this.fetchApi(url);
};
function fetchApi(url) {
fetch(url)
.then((res) => res.json())
.... some data that is being fetched ....
});
};
let url = `https://api.github.com/users/${this.state.username}`;
}
}
export default App;
Run Code Online (Sandbox Code Playgroud)
但是,我得到了TypeError: Cannot read property 'bind' …
我似乎不理解这段代码的输出:
function fib(x) {
return (x === 0 || x === 1) ? x : fib(x - 1) + fib(x - 2);
}
fib(7);
// output is 13
Run Code Online (Sandbox Code Playgroud)
这是我的思考过程:
该函数如何得到13的结果?
我有三个静态Web项目,我想在我的GitHub帐户上托管.
我在文档中注意到你必须为repo名称写你的用户名,以便告诉GitHub这将是一个静态网站(例如bluebird/bluebird.github.io).
是否可以使用除用户名之外的其他名称来托管我的项目?
在这里反应JS新手,所以请耐心等待.
我在我的index.html文件中添加了Google Maps API脚本,如下所示:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">
</head>
<body>
<div id="root"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=MY-API-IS-HERE"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
然后,我安装google-maps-react了npm所以我可以使用它的组件.我在我的组件中导入了这个库,如下所示:
import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';
class BaseMap extends Component {
constructor() {
super();
this.state = {
center: {lat: 42, lng: 14},
zoom: 10
}
};
render() {
let location = …Run Code Online (Sandbox Code Playgroud) 我有以下字符串:
08/07/2012 04:17:18
如何date使用 PHP 检查它是否是一种数据类型?例如,如果我正在检查integer,我会这样做:if(is_int($checkVar)。我怎样才能对这种日期格式做同样的事情?
在使用 Python 和 Windows 7 时遇到许多问题后,我终于找到了一种为 python 安装 Mysql 的方法。我是通过(wheel)完成的.whl,更具体地说,是这个: https: //sourceforge.net/projects/mysql-python/。
但是,当我尝试使用以下命令将其导入到我的项目中时,它安装成功:
from MySQLdb import mysqldb或者from MySQLdb import mysql,对于我尝试过的任何变体,我在 Git Bash 中收到此错误:
ImportError: cannot import name MySQLdb
我似乎无法定位问题。任何想法都会非常有益!
我使用该print_r函数来打印从数据库中的表中获取的数组.而不是像这样的格式良好的数组:
Array
(
[0] => Array
(
[id] => 1
[firstname] => Firstname one
[lastname] => Lastname one
)
[1] => Array
(
[id] => 2
[firstname] => Firstname two
[lastname] => Lastname two
)
[2] => Array
(
[id] => 3
[firstname] => Firstname three
[lastname] => Lastname three
)
)
Run Code Online (Sandbox Code Playgroud)
我得到了内联结果,如下所示:
Array ([0] => Array ([id] => 1 [firstname] => Firstname one [lastname] => Lastname one ) [1] => Array ([id] => 2 [firstname] => Firstname …Run Code Online (Sandbox Code Playgroud) 我使用以下代码在Chrome开发工具中测试了一些内容:
const one = {a: "a", b: "b"};
const two = { ...one, c: "c" };
VM417:1 Uncaught SyntaxError: Unexpected token ...
Run Code Online (Sandbox Code Playgroud)
为什么我在扩展运算符上出现此错误?
我想每1000毫秒更新一次React组件的状态。但是,我尝试在setInterval上进行操作componentDidMount,但是没有运气。目前,我有两个结果console.log,一个是构造函数中的空状态对象,另一个是从API提取的对象。如何使用setInterval每隔1000 ms更新一次组件的状态?
这是我的代码:
let url = 'some-link-bla-bla';
class Basemap extends React.Component {
constructor(props) {
super(props);
this.state = {};
console.log(this.state);
}
render() {
return (
<Scene style={{ width: '100vw', height: '100vh' }}
mapProperties={{ basemap: 'satellite' }}
viewProperties={ this.state } />
);
}
componentDidMount() {
fetch(url)
.then(d => d.json().then(function(d) {
console.log(d);
}))
.then(d => function(d) {
this.setState({
center: [
{latitude : d.iss_position.latitude} + ', ' +
{longitude: d.iss_position.longitude}
]
})
});
}
}
export default Basemap;
Run Code Online (Sandbox Code Playgroud) 我正在阅读React + Redux教程,其中有一个代码片段,如下所示:
class ExampleComponent extends Component {
constructor() {
super();
this.state = {
articles: [
{ title: "React Redux Tutorial for Beginners", id: 1 },
{ title: "Redux e React: cos'è Redux e come usarlo con React", id: 2 }
]
};
}
render() {
const { articles } = this.state;
return <ul>{articles.map(el => <li key={el.id}>{el.title}</li>)}</ul>;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,出于好奇,我做了一个类似的事情,但是在非反应环境中(例如在浏览器的控制台日志中).首先我初始化了constant这样的:
const articles = [
{ title: "React Redux Tutorial for Beginners", id: 1 },
{ title: "Redux e …Run Code Online (Sandbox Code Playgroud) javascript ×7
reactjs ×3
php ×2
arrays ×1
es6-modules ×1
git ×1
github ×1
github-pages ×1
google-maps ×1
mysql ×1
python ×1