一起使用Vue.js和Flask

时间:2018-12-28 02:55:29

标签: python vue.js flask

我的代码运行得很好,然后我想使用flask,所以我将其复制到flask应用程序目录中。 html代码如下:

<html>

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
        crossorigin="anonymous">
</head>

<body>
    <div id="app" class="container">
        <div class="row">
            <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav">
                        <li class="nav-item" v-for="tab in tabs" v-bind:class="tab.active">
                            <a href="#" class="nav-link">{{ tab.name }}</a>
                        </li>
                    </ul>
                </div>
            </nav>
        </div>
        <div class="row">
            <div class="col">
                <hr class="navbarDivide">
            </div>
        </div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="/static/js/challenges.js"></script>
</body>

</html>

当我删除Vue的内容时,它工作得很好(当我删除v-for,v-bind和{{tab.name}}时。但是,当我添加它时,出现500错误我正在使用flask执行以下所有操作:Flask代码如下:

from flask import Flask, render_template

app = Flask(__name__)
application = app

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

if __name__ == '__main__':
    app.run(debug=True)

javascript代码仅具有Vue.js(带有数据的对象)所需的内容。 javascript代码如下:

var app = new Vue({
    el: '#app',
    data: {
        tabs: [
            { name: "Home", active: "" },
            { name: "Challenges", active: "active" },
            { name: "Scoreboard", active: "" },
            { name: "About", active: "" }
        ]
    }
});

更新

经过更多调试之后,我认为我可能已经解决了问题。当我注释掉所有内容并将console.log("it works");添加到我的javascript代码时,什么都没有发生。这可能意味着javascript代码未连接到html,但是我该如何解决呢?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我是手动输入javascript的网址

<script src="/static/js/challenges.js"></script>

我要解决的问题是使用url_for

<script src="{{ url_for('static', filename='js/challenges.js') }}"></script>

可能很多人以前都使用过烧瓶,但是我对此并不陌生。