有什么办法可以在同一项目中运行React + Django而无需再次运行npm run build? [英] is there any way to run React + Django in same project without running npm run build again and again?

查看:47
本文介绍了有什么办法可以在同一项目中运行React + Django而无需再次运行npm run build?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Django有足够的了解,并且最近学到了反应,我喜欢将两者结合起来.

I have enough knowledge about Django and recently learned react and i like to combine both.

我已经看过一些视频教程,并且成功地将它们集成在一起.

I have gone through a couple of video tutorials and i am successful in integrating them.

但是问题是我应该在两个不同的项目中构建一个REST API和一个react应用程序,并将它们结合起来,或者我每次在前端进行任何更改时都需要运行 npm run build 结束.

but the problem is i should either build a rest API and a react app in two different projects and them combine them or i need to run npm run build every time i make any changes on my front end.

因此,如果有人有其他选择(在Django服务器上连续运行React而不一次又一次不运行 build ),请告诉我.

so if anybody have an alternative for this(Running React on Django server continuously without running build again and again) please tell me.

推荐答案

在package.json文件中,将有以下脚本标记,

In your package.json file there will be script tag as below,

 "scripts": {
    "dev": "webpack  --mode development ./src/index.js --output ./static/frontend/main.js",
    "build": "webpack --mode production ./src/index.js --output ./static/frontend/main.js"
  },

使用以下开发脚本添加-watch 标记,

add --watch tag with dev script as below,

 "scripts": {
    "dev": "webpack --watch --mode development ./src/index.js --output ./static/frontend/main.js",
    "build": "webpack --mode production ./src/index.js --output ./static/frontend/main.js"
  },

然后执行 npm run dev

因此JS将不会中断运行,即使您在React中更改文件,它也会自动更新并运行.

So the JS will run without breaking.Even if you change the files inside React, it will update and run automatically.

文件结构可能会因您的项目而异,但逻辑相同

The file structure may vary according to your project but logic is same

这篇关于有什么办法可以在同一项目中运行React + Django而无需再次运行npm run build?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆