【问题标题】:vue front-end flask back-end integrationvue前端flask后端集成
【发布时间】:2023-03-15 03:36:02
【问题描述】:

下面是我的 vue 前端构建(npm rum build on vue-cli 3)。

下面是我用于烧瓶后端的run.py 文件。

from flask import Flask, render_template


class CustomFlask(Flask):
    jinja_options = Flask.jinja_options.copy()
    jinja_options.update(dict(
        variable_start_string='%%',
        variable_end_string='%%',
    ))


app = CustomFlask(__name__,
                  static_folder="./dist",
                  template_folder="./dist"
                  )


@app.route('/')
def index():
    return render_template("index.html")


if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000)

如您所见,由于我的 dist 结构,我已将默认烧瓶静态模板目录更改为 ./dist。但是当我尝试测试我的应用时,我收到了以下消息。

Chrome 控制台

vendor.97db904d.js Failed to load resource: the server responded with a status of 404 (NOT FOUND)
app.9aaff056.js Failed to load resource: the server responded with a status of 404 (NOT FOUND)
app.197e53a9.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
vendor.97db904d.js Failed to load resource: the server responded with a status of 404 (NOT FOUND)
app.9aaff056.js Failed to load resource: the server responded with a status of 404 (NOT FOUND)
app.197e53a9.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)

烧瓶

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [11/Apr/2018 19:44:21] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Apr/2018 19:44:21] "GET /js/vendor.97db904d.js HTTP/1.1" 404 -
127.0.0.1 - - [11/Apr/2018 19:44:21] "GET /js/app.9aaff056.js HTTP/1.1" 404 -
127.0.0.1 - - [11/Apr/2018 19:44:21] "GET /css/app.197e53a9.css HTTP/1.1" 404 -
127.0.0.1 - - [11/Apr/2018 19:44:21] "GET /js/vendor.97db904d.js HTTP/1.1" 404 -
127.0.0.1 - - [11/Apr/2018 19:44:21] "GET /js/manifest.ce28c628.js.map HTTP/1.1" 404 -
127.0.0.1 - - [11/Apr/2018 19:44:21] "GET /js/app.9aaff056.js HTTP/1.1" 404 -

如何正确更改烧瓶上的静态/模板目录?或者我应该在 vue-cli3(webpack) 上更改我的构建配置?我对此没有更多的线索。如果可以的话,请给我一个提示。提前谢谢你。

【问题讨论】:

    标签: flask webpack vue.js vue-cli


    【解决方案1】:

    我最近在一个应用中使用了this example,效果很好。

    特别是关于配置重定向的段落。

    【讨论】:

      【解决方案2】:

      如果您更改vue-cli 构建以将dist 文件夹的内容放入可以工作的Flask static 文件夹中。 但是,您需要更改初始化 Flask app 和提供 index.html 文件的方式。

      # vue-flask.py   
      
      from flask import Flask
      
      app = Flask(__name__, static_url_path='')
      
      
      @app.route('/')
      def index():
      
          # changed to send_static_file
          return app.send_static_file('index.html')
      
      
      if __name__ == '__main__':
          app.run(host='127.0.0.1', port=5000)
      

      这是项目结构:

      我在 reactFlask 集成的情况下使用了相同的方法,但最终将其拆分为两个微服务,如 here 所述。

      【讨论】:

      • 它就像一个魅力! static_url_path=''app.send_static_file('index.html') 是关键点。我也会检查和研究你的链接。谢谢。
      猜你喜欢
      • 2019-12-10
      • 1970-01-01
      • 2015-02-19
      • 2022-10-24
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2022-09-28
      相关资源
      最近更新 更多