Hello,Flask & Vue

Webpack

webpack 是一个模块打包工具。什么意思呢?它将一堆文件中的每个文件都作为一个模块,找出他们的依赖关系,将它们打包为可部署的静态资源。
image.png

install

  1. mkdir webpack-demo && cd webpack-demo
  2. npm init -y
  3. npm install webpack webpack-cli --save-dev

Test

  1. #构建如下目录
  2. webpack-demo
  3. |- package.json
  4. |- /dist
  5. |- index.html
  6. |- /src
  7. |- index.js
  8. #install loadsh
  9. > npm install --save lodash
  10. > vim src/index.js
  11. import _ from 'lodash';
  12. function component() {
  13. var element = document.createElement('div');
  14. // Lodash, now imported by this script
  15. element.innerHTML = _.join(['Hello', 'webpack'], ' ');
  16. return element;
  17. }
  18. document.body.appendChild(component());
  19. > vim dist/index.html
  20. <!doctype html>
  21. <html>
  22. <head>
  23. <title>起步</title>
  24. - <script src="https://unpkg.com/lodash@4.16.6"></script>
  25. </head>
  26. <body>
  27. - <script src="./src/index.js"></script>
  28. + <script src="main.js"></script>
  29. </body>
  30. </html>

config

webpack.config.js
  1. webpack-demo
  2. |- package.json
  3. + |- webpack.config.js
  4. |- /dist
  5. |- index.html
  6. |- /src
  7. |- index.js
  8. var webpack = require('webpack')
  9. var path = require('path')
  10. var ExtractTextPlugin = require('extract-text-webpack-plugin')
  11. const {CleanWebpackPlugin} = require('clean-webpack-plugin')
  12. module.exports = {
  13. entry: {
  14. app: './src/main.js'
  15. },
  16. output: {
  17. path: path.resolve(__dirname, './dist'),
  18. filename: '[name].[hash:8].js'
  19. },
  20. module: {
  21. rules:[
  22. {
  23. test: /\.css$/,
  24. use: ExtractTextPlugin.extract({
  25. use: 'css-loader'
  26. })
  27. },
  28. {
  29. test: /\.js$/,
  30. exclude: /node_modules/,
  31. use: 'babel-loader'
  32. }
  33. ]
  34. },
  35. plugins: [
  36. new ExtractTextPlugin('[name].[hash:8].css'),
  37. new CleanWebpackPlugin(['./dist'])
  38. ]
  39. }
  40. if (process.env.NODE_ENV == 'production') {
  41. module.exports.plugins = (module.exports.plugins || []).concat([
  42. new webpack.optimize.UglifyJsPlugin()
  43. ])
  44. }

Flask & flask-sqlalchemy

install

pip install flask flask-sqlachemy

app.py

  1. #coding: utf-8
  2. from flask import Flask, jsonify
  3. app = Flask(__name__)
  4. @app.route('/api/hello', methods=['GET'])
  5. def hello():
  6. return jsonify(ok=True, data='hello, world')
  7. if __name__ == '__main__':
  8. app.run()

run app

python app.py

listing for 5000

Vue

install

npm install --golbal vue-cli

init

vue init webpack-somple hello-app
cd hello-app
npm install
npm run dev

App.vue

  1. <template>
  2. <div id="app">
  3. <h1>{{ msg }}</h1>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'app',
  9. data () {
  10. return {
  11. msg: 'Hello, world!'
  12. }
  13. }
  14. }
  15. </script>
  16. <style>
  17. </style>

listing for 8080

axios

install

npm install --save axios

add The script in App.vue

  1. <template>
  2. <div id="app">
  3. <h1>{{ msg }}</h1>
  4. </div>
  5. </template>
  6. <script>
  7. var axios = require('axios');
  8. export default {
  9. name: 'app',
  10. data () {
  11. return {
  12. msg: ''
  13. }
  14. },
  15. mounted() {
  16. axios.get('/api/hello')
  17. .then(response => console.log(response.data))
  18. }
  19. }
  20. </script>
  21. <style>
  22. </style>

add The prolicy in webpack.config.js

  1. devServer: {
  2. historyApiFallback: true,
  3. noInfo: true,
  4. proxy: {"/api/*": {target: 'http://localhost:5000', host: 'localhost'}}
  5. }
  6. // api/*(8080) 的请求全部转发到我们的后端服务器
  7. ## App.vue
  8. mounted() {
  9. axios.get('/api/hello')
  10. .then(response => this.msg = response.data.data)
  11. }
  12. // 将转发请求获取到的值赋值给App.vue里的msg

Vue

vue.js

Webpack

webpack 中文网