This describes the method and tooling used to incorporate Vue.js components into your Django templates. The overall strategy taken is based on this series of articles.
- Ensure that your version of
django-webpack-loader
inrequirements.txt
is~=0.6.0
. - Ensure that your version of
webpack-bundle-tracker
inpackage.json
is^0.4.3
.
-
Create a Vue.js application in your Django project.
vue create vue
-
Install
webpack-bundle-tracker
into your Vue.js application.npm install --save-dev webpack-bundle-tracker@^0.4.3
-
Add the
vue.config.js
configuration file to your Vue.js application -
Add your components (
.vue
) and entry points (i.e.main.js
) files. In this demonstration:vue/src/components/{App, App02}.vue
, andvue/src/components/{main, main_02}.js
were added.
-
Add
django-webpack-loader
to your TOM Toolkit project.requirements.txt
needs updatingINSTALLED_APPS
in yoursettings.py
needswebpack_loader
.
-
Configure
django-webpack-loader
in yoursettings.py
.- add
VUE_FRONTEND_DIR
, which is used by - add
WEBPACK_LOADER
dictionary.
- add
-
Add the Django URLs and templates that will incorporate you Vue.js components.
- in this case
vue_health_check_{01, 02}.html
were added, - add URLs to
tom_demo_base/urls.py
- in this case
-
For deployment, update the
Dockerfile
to- install
nvm
,node
, andnpm
- run
npm install
to install thenode_modules
, and - run
npm run build
to place your JS and CSS files in yourSTATIC_DIRS
.
- install
Key things to note:
-
in
vue.config.js
, yourpages
dictionary defines every component that will be served.- the dictionary key defines the
render_bundle
names that will be used in your template.html
flie - the value
entry
key is the file name of the entrypoint.js
file.
- the dictionary key defines the
-
One the Vue.js side,
webpack-bundle-tracker
writes thewebpack-stats.json
file uponnpm run build
(for production) ornpm run server
(for development). -
On the Django side,
webpack-stats.json
is read bydjango-webpack-loader
-
the Vue component entry points (i.e.
vue/src/components/main.js
)- instantiate a Vue app (
new Vue
orcreateApp()
) for each Vue component, and - associate that Vue component with an html element id.
- instantiate a Vue app (
-
the Django html templates refer to the element id from the entrypoint:
<div id="id-from-entrypoint">
-
the content of the
<div>
refers the Vue component associated with the element.