小编bla*_*rix的帖子

如何创建一组自定义类型打字稿

我有一个界面如下:

interface orderItem {
  symbol: string,
  side: string,
  price: number
  quantity: number
}
Run Code Online (Sandbox Code Playgroud)

我想在我的构造函数中创建一个由“orderItems”组成的数组“orders”。

但是, orders : [orderItem] 似乎并没有做到这一点。有谁知道我该如何继续?

这是我在构造函数中尝试执行的操作:

   this.state = {
      orders: orderItem []

  } 
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs

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

使用异步并在FileReader中等待时出错

我正在尝试使用FileReader读取文件:

async readFile(event: any) {
    var file = event.target.files[0];
    var data:string
    if (file) {   
        var reader:FileReader = new FileReader();
         reader.onload = async function (evt : FileReaderEvent) {
            data = await evt.target.result;
            console.log(evt.target.result);

        };
        console.log(file);
        console.log(data);
        await reader.readAsText(file);
        await this.processFileContent(data);
    }
 }
Run Code Online (Sandbox Code Playgroud)

但是,evt.target.result仍然在我的console.log(file)调用后被打印。

有谁知道我如何获得文件结果并将其传递给我的processFileContent函数?

javascript asynchronous typescript

5
推荐指数
2
解决办法
3501
查看次数

带有axios的GET请求返回未定义

我正在尝试使用以下命令执行获取请求:

router.get('/marketUpdates',((request, response) => {
  console.log("market updates");
  var data: Order[]
  axios.get('http://localhost:8082/marketUpdates')
  .then(function (response) {
    console.log("GET Response")
    console.log(response.data);
    data = response.data;
  })
  .catch(function (error) {
    console.log("Error in fetching market updates");
  });  

  console.log("Data before sending is ")
  console.log(data);
  response.send(data);
}))
Run Code Online (Sandbox Code Playgroud)

但是,我靠近底部的console.log在.then中的console.log之前执行。

数据在发送时是不确定的。有谁知道我能避免这种情况?

javascript asynchronous http typescript

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

如何在反应组件中显示字符串数组

我正在尝试通过执行以下操作在我的反应组件中的单独行上显示一个字符串数组:

       <div>
          {this.props.notifications.join('\n')}
      </div>
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不起作用。this.props.notifications 是我想在 div 中呈现的字符串数组。有谁知道我该如何解决这个问题?

javascript typescript reactjs

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

标签 统计

javascript ×4

typescript ×4

asynchronous ×2

reactjs ×2

http ×1