我正在使用Django和React制作一个项目。我从Django中获取Twitter API的数据,我想在前端展示这些数据。
我将数据作为Javascript变量从django views.py传递到index.html。然后,我想在Components文件夹中的Landing.js中显示它。我使用了React路由,Landing和Main页面是我目前正在路由的页面。我还没有弄清楚如何从单个html文件将数据传递给React中的Javascript组件。谢谢。
<!-- index.html -->
<section class="section">
<div class="container">
<div id="app" class="columns"><!-- React --></div>
</div>
</section>
<script type="text/javascript">
window.myVar = "{{context.near.text}}";
//data can be passed from the Django.views.py
</script>
{% load static %}
<script src="{% static 'frontend/main.js' %}"></script>
//Landing.js js components
import React, { Component } from 'react';
class Landing extends Component {
componentDidMount() {
console.log(window.myVar); //undefined
console.log(this.myVVar); //undefined
}
render(){
return (
<div>
</div>
)
}
}
export default Landing;