|
| 1 | +## Goals |
| 2 | +- [ ] Install NPM |
| 3 | +- [ ] Install VueCLI |
| 4 | +- [ ] Add Vue to application |
| 5 | + |
| 6 | +## Install NPM |
| 7 | +Go to `https://nodejs.org/en/download/`, download the latest or the LTS version of Node. Install node with the recommended settings. Check if Node and NPM is installed by checking in your terminal: |
| 8 | + |
| 9 | +``` |
| 10 | +> npm --version |
| 11 | +6.14.14 |
| 12 | +> node --version |
| 13 | +v14.17.4 |
| 14 | +``` |
| 15 | + |
| 16 | +## Install VueCLI |
| 17 | +Once the NodeJS has been installed in your computer, you may now install the VueCLI. In your terminal, type this command: |
| 18 | +``` |
| 19 | +sudo npm i -g @vue/cli |
| 20 | +``` |
| 21 | +Then test if its working by typing |
| 22 | +``` |
| 23 | +vue create hello-vue |
| 24 | +Vue CLI v4.5.13 |
| 25 | +? Please pick a preset: Manually select features |
| 26 | +? Check the features needed for your project: Choose Vue version, Babel, Router, Linter |
| 27 | +? Choose a version of Vue.js that you want to start the project with 2.x |
| 28 | +? Use history mode for router? (Requires proper server setup for index fallback in production) Yes |
| 29 | +? Pick a linter / formatter config: Prettier |
| 30 | +? Pick additional lint features: Lint on save |
| 31 | +? Where do you prefer placing config for Babel, ESLint, etc.? In package.json |
| 32 | +? Save this as a preset for future projects? (y/N) N |
| 33 | +``` |
| 34 | + |
| 35 | +Wait for a few minutes, then once completed, will show a prompt like this: |
| 36 | + |
| 37 | +``` |
| 38 | +📄 Generating README.md... |
| 39 | +
|
| 40 | +🎉 Successfully created project hello-vue2. |
| 41 | +👉 Get started with the following commands: |
| 42 | +
|
| 43 | + $ cd hello-vue |
| 44 | + $ npm run serve |
| 45 | +``` |
| 46 | + |
| 47 | +Go to `hello-vue` and type `npm run serve`. It will remind you to access to `http://localhost:<port>/`. Go to that url and check if Vue is showing. This indicates that Vue is properly installed to your computer. |
| 48 | + |
| 49 | + |
| 50 | +## Add Vue to application |
| 51 | +We are going to create a simple Vue project in our Django project. Ensure that the project name is `frontend`, like so: |
| 52 | +``` |
| 53 | +vue create frontend |
| 54 | +``` |
| 55 | + |
| 56 | +Set the config like the ones below: |
| 57 | +``` |
| 58 | +vue create hello-vue |
| 59 | +Vue CLI v4.5.13 |
| 60 | +? Please pick a preset: Manually select features |
| 61 | +? Check the features needed for your project: Choose Vue version, Babel, Router, Linter |
| 62 | +? Choose a version of Vue.js that you want to start the project with 2.x |
| 63 | +? Use history mode for router? (Requires proper server setup for index fallback in production) Yes |
| 64 | +? Pick a linter / formatter config: Prettier |
| 65 | +? Pick additional lint features: Lint on save |
| 66 | +? Where do you prefer placing config for Babel, ESLint, etc.? In package.json |
| 67 | +? Save this as a preset for future projects? (y/N) N |
| 68 | +``` |
| 69 | + |
| 70 | +Then, we're going to install a package `webpack-bundle-tracker` to keep track of the code in our frontend application. |
| 71 | +Go to `frontend` folder then install the webpack tracker: |
| 72 | +``` |
| 73 | +cd frontend |
| 74 | +
|
| 75 | +npm install --save-dev webpack-bundle-tracker@0.4.3 |
| 76 | +``` |
| 77 | + |
| 78 | +Next, create a file inside the folder `frontend`. Name it `vue.config.js`. |
| 79 | +``` |
| 80 | +const BundleTracker = require("webpack-bundle-tracker"); |
| 81 | +
|
| 82 | +module.exports = { |
| 83 | + // on Windows you might want to set publicPath: "http://127.0.0.1:8080/" |
| 84 | + publicPath: "http://127.0.0.1:8081/", |
| 85 | + outputDir: './dist/', |
| 86 | +
|
| 87 | + chainWebpack: config => { |
| 88 | +
|
| 89 | + config |
| 90 | + .plugin('BundleTracker') |
| 91 | + .use(BundleTracker, [{filename: './webpack-stats.json'}]) |
| 92 | +
|
| 93 | + config.output |
| 94 | + .filename('bundle.js') |
| 95 | +
|
| 96 | + config.optimization |
| 97 | + .splitChunks(false) |
| 98 | +
|
| 99 | + config.resolve.alias |
| 100 | + .set('__STATIC__', 'static') |
| 101 | +
|
| 102 | + config.devServer |
| 103 | + // the first 3 lines of the following code have been added to the configuration |
| 104 | + .public('http://127.0.0.1:8080') |
| 105 | + .host('127.0.0.1') |
| 106 | + .port(8080) |
| 107 | + .hotOnly(true) |
| 108 | + .watchOptions({poll: 1000}) |
| 109 | + .https(false) |
| 110 | + .disableHostCheck(true) |
| 111 | + .headers({"Access-Control-Allow-Origin": ["\*"]}) |
| 112 | +
|
| 113 | + }, |
| 114 | +
|
| 115 | + // uncomment before executing 'npm run build' |
| 116 | + // css: { |
| 117 | + // extract: { |
| 118 | + // filename: 'bundle.css', |
| 119 | + // chunkFilename: 'bundle.css', |
| 120 | + // }, |
| 121 | + // } |
| 122 | +
|
| 123 | +}; |
| 124 | +
|
| 125 | +``` |
| 126 | + |
| 127 | +Next, we are going to install a webpack loader in Django environment. This will help us read the changes given by the bundle tracker. |
| 128 | +``` |
| 129 | +pip install django-webpack-loader==0.7.0 |
| 130 | +``` |
| 131 | + |
| 132 | +Update the requirements.txt file. |
| 133 | +``` |
| 134 | +pip freeze > requirements.txt |
| 135 | +``` |
| 136 | + |
| 137 | +Then add the webpack_loader library to `settings.py`. |
| 138 | +``` |
| 139 | +INSTALLED_APPS = [ |
| 140 | + 'webpack_loader', |
| 141 | +] |
| 142 | +
|
| 143 | +// on the bottom of settings.py file |
| 144 | +WEBPACK_LOADER = { |
| 145 | + 'DEFAULT': { |
| 146 | + 'BUNDLE_DIR_NAME': 'dist/', |
| 147 | + 'STATS_FILE': BASE_DIR / 'frontend' / 'webpack-stats.json' |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +Add this code inside the file `templates/index.html`, on the first line of the code: |
| 153 | +``` |
| 154 | +{% load render_bundle from webpack_loader %} |
| 155 | +``` |
| 156 | +Also, add this inside the body of the code, after the div id app: |
| 157 | +``` |
| 158 | +{% render_bundle 'app' %} |
| 159 | +``` |
| 160 | + |
| 161 | +The index file should look this this: |
| 162 | +``` |
| 163 | +{% load render_bundle from webpack_loader %} |
| 164 | +<!DOCTYPE html> |
| 165 | +<html lang="en"> |
| 166 | +<head> |
| 167 | + <meta charset="UTF-8"> |
| 168 | + <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| 169 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 170 | + <title>Forum App</title> |
| 171 | +</head> |
| 172 | +<body> |
| 173 | +
|
| 174 | + <h1> Vue JS</h1> |
| 175 | +
|
| 176 | + <div id="app"></div> |
| 177 | +
|
| 178 | + {% render_bundle 'app' %} |
| 179 | + |
| 180 | +</body> |
| 181 | +</html> |
| 182 | +``` |
| 183 | + |
| 184 | +Run server of both django and vue separately. |
| 185 | +``` |
| 186 | +python manage.py runserver |
| 187 | +
|
| 188 | +npm run serve |
| 189 | +``` |
| 190 | + |
| 191 | + |
| 192 | +(Optional) You may also explore the vue project by typing this command. |
| 193 | +``` |
| 194 | +vue ui |
| 195 | +``` |
0 commit comments