【发布时间】:2019-02-27 01:36:24
【问题描述】:
我正在使用 Django 和 React 制作一个项目。我从 Django 的 Twitter API 获取数据,我想在前端显示数据。 我将 django views.py 中的数据作为 javascript 变量传递给 index.html。然后我想在 Components 文件夹中的 Landing.js 中展示它。我使用了 React 路由器,我的 Landing 和 Main page 是我目前路由到的页面。我还没有弄清楚如何将数据从单个 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;
this is context I pass from Django views.py
this is a variable I want to pass to "LANDING" componentsThis is "Landing.js" components I want to show the variables
【问题讨论】:
-
这是可能的,但不要这样做——react 不是另一个模板引擎——让你的应用从你的 api 中获取数据
-
我也有同样的问题。你想好怎么做了吗?
标签: javascript django reactjs