Hello,Flask & Vue
Webpack
webpack 是一个模块打包工具。什么意思呢?它将一堆文件中的每个文件都作为一个模块,找出他们的依赖关系,将它们打包为可部署的静态资源。
install
mkdir webpack-demo && cd webpack-demonpm init -ynpm install webpack webpack-cli --save-dev
Test
#构建如下目录webpack-demo|- package.json|- /dist|- index.html|- /src|- index.js#install loadsh> npm install --save lodash> vim src/index.jsimport _ from 'lodash';function component() {var element = document.createElement('div');// Lodash, now imported by this scriptelement.innerHTML = _.join(['Hello', 'webpack'], ' ');return element;}document.body.appendChild(component());> vim dist/index.html<!doctype html><html><head><title>起步</title>- <script src="https://unpkg.com/lodash@4.16.6"></script></head><body>- <script src="./src/index.js"></script>+ <script src="main.js"></script></body></html>
config
webpack.config.js
webpack-demo|- package.json+ |- webpack.config.js|- /dist|- index.html|- /src|- index.jsvar webpack = require('webpack')var path = require('path')var ExtractTextPlugin = require('extract-text-webpack-plugin')const {CleanWebpackPlugin} = require('clean-webpack-plugin')module.exports = {entry: {app: './src/main.js'},output: {path: path.resolve(__dirname, './dist'),filename: '[name].[hash:8].js'},module: {rules:[{test: /\.css$/,use: ExtractTextPlugin.extract({use: 'css-loader'})},{test: /\.js$/,exclude: /node_modules/,use: 'babel-loader'}]},plugins: [new ExtractTextPlugin('[name].[hash:8].css'),new CleanWebpackPlugin(['./dist'])]}if (process.env.NODE_ENV == 'production') {module.exports.plugins = (module.exports.plugins || []).concat([new webpack.optimize.UglifyJsPlugin()])}
Flask & flask-sqlalchemy
install
pip install flask flask-sqlachemy
app.py
#coding: utf-8from flask import Flask, jsonifyapp = Flask(__name__)@app.route('/api/hello', methods=['GET'])def hello():return jsonify(ok=True, data='hello, world')if __name__ == '__main__':app.run()
run app
python app.py
listing for 5000
Vue
install
npm install --golbal vue-cli
init
vue init webpack-somple hello-appcd hello-appnpm installnpm run dev
App.vue
<template><div id="app"><h1>{{ msg }}</h1></div></template><script>export default {name: 'app',data () {return {msg: 'Hello, world!'}}}</script><style></style>
listing for 8080
axios
install
npm install --save axios
add The script in App.vue
<template><div id="app"><h1>{{ msg }}</h1></div></template><script>var axios = require('axios');export default {name: 'app',data () {return {msg: ''}},mounted() {axios.get('/api/hello').then(response => console.log(response.data))}}</script><style></style>
add The prolicy in webpack.config.js
devServer: {historyApiFallback: true,noInfo: true,proxy: {"/api/*": {target: 'http://localhost:5000', host: 'localhost'}}}// api/*(8080) 的请求全部转发到我们的后端服务器## App.vuemounted() {axios.get('/api/hello').then(response => this.msg = response.data.data)}// 将转发请求获取到的值赋值给App.vue里的msg
