I want to use Vue.js and Flask together: Vue.js for a dynamic front-end, and Flask for the back-end. How can I do that?
相关问题
- Ignore Typescript errors in Webpack-dev-server
- Axios OPTIONS instead of POST Request. Express Res
- Plain (non-HTML) error pages in REST api
- WTForms form with custom validate method is never
- flask application with watchdog observer
相关文章
- 请问为什么后台获取不到表单数据
- 运行"WEBPACK_ENV=online webpack -p",如何将.scss e6文件转化
- TypeError: 'BaseQuery' object is not calla
- React and typescript with webpack typing issue
- Best way to dynamically change theme of my Vue.js
- Log to node console or debug during webpack build
- vue-router how to persist navbar?
- Configuring electron-webpack renderer to work with
I recently had this problem (combining Vue.js and Flask).
There are at least two ways to combine them, depending on whether you're creating 1) a simple Vue.js app or 2) a more-complicated Vue.js app that needs to use a module bundler like Webpack to combine Single-File Components or npm packages.
Simple Vue.js app:
This is actually fairly easy and very powerful on its own:
.html
file. Otherwise just open whatever.html
template file you want the app to be in.If you're OK with having your Vue.js JavaScript code in the same file as your HTML, below is a simple "Hello World" example template you can use to get a sense of what needs to be in the Flask template file:
static
folder, name it after this app you want to create..html
template file include a script tag to include Vue.js.<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>
<script src="%% url_for('static', filename='js/YOUR_APP_NAME.js') %%"></script>
div
in the.html
template file that has anid
ofapp
.<div id="app"></div>
If you are using Jinja2 to render your templates, you will need to add a few lines of code to tell Jinja2 to not use the
{{ }}
syntax to render variables, because we need those double-curly-brace symbols for Vue.js. Below is the code you need to add to yourapp.py
to do this:.html
and.js
files.Easy!
More-complicated Vue.js app using Webpack:
npm install -g @vue/cli
vue create my-project
server
folder and aclient
folder, where theserver
folder contains the Flask server code, and theclient
folder contains the Vue.js project code.app.html
file is created in your Flaskserver/templates
folder, and the static JavaScript and CSS needed byapp.html
is created in aserver/static/app/
folder, isolated from the static assets used by the non-Vue portions of you Flask app.npm run build
from within the folder containing your Vue.js project, which will generate an.html
file and several static files (JavaScript and CSS).The exact changes I made to my Webpack config (via my git commit):
client/build/webpack.dev.conf.js
:Here (above) I'm changing the name of the Vue.js 'launch' file to app.html so that it doesn't conflict with my Flask app's 'index.html'.
client/config/index.js
:Here (above) I'm setting where the app.html file and static assets should be created.
Because we're directing Webpack to generate the static assets within Flask's "static" folder (
/static/app/
)...html
file will automatically be set up correctly by Webpack.src=/static/app/js/app.f5b53b475d0a8ec9499e.js
static/
folder, which Flask assumes has a/static/etc.
URL.app.html
file.client/build/webpack.prod.conf.js
:Here (above) I'm just renaming the 'launch' page, same as in
webpack.dev.conf.js
.routes.py
:Here (above) is my render function. I'm using Flask's Blueprints feature (
<blueprint_name>.route
) but you don't have to.