【问题标题】:Vue.js suddenly not workingVue.js 突然不工作了
【发布时间】:2018-04-04 15:14:21
【问题描述】:

我正在使用 Vue.js,但由于某种原因它刚刚停止工作。我似乎找不到我的错误 - 看了好几个小时。

这是我的 HTML 文件 - 只是依赖项和一个在 users 中显示数据的表格。

<!DOCTYPE html>
<html>
    <head>
        <title>Hello, world!</title>
    </head>
    <body>
        <script
            src="https://code.jquery.com/jquery-3.3.1.min.js"
            type="text/javascript">
        </script>
        <link 
            href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
            rel="stylesheet" 
        >
        <script
            src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
            type="text/javascript">
        </script>
        <script
            src="https://cdn.jsdelivr.net/npm/vue"
            type="text/javascript">
        </script>

        <script src="/js/app.js" type="text/javascript"></script>

        <div class="container">
            <div id="app">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Personal Number</th>
                            <th>Volcano Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="user in users">
                            <td><% user.name %></td>
                            <td><% user.personal_number %></td>
                            <td><% user.volcano_amount %></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </body>
</html>

这是 app.js 文件 - 它只是初始化 Vue 实例,创建 users 数组,并运行更新 users 数组的 AJAX 请求。

var app = new Vue({
    el: '#app',
    data: {
        users: []
    },
    mounted: function() {
        var vue_object = this;
        $.getJSON('users', function(data) {
            vue_object.users = data;
        });
    },
    delimiters: ['<%', '%>']
});

结果非常令人失望 - 一切都是纯 HTML。该表有 2 行 - 标题,以及带有 &lt;% user.xxx %&gt; 的行,好像 Vue 不存在一样。

我猜这意味着某些东西没有运行,尽管 app.js 本身确实运行了(我尝试添加 console.log 并且它确实打印了)

希望你有好的想法:)

【问题讨论】:

  • 如果您发现错误,请检查您的控制台日志,同时删除 var app = 部分,看看会发生什么
  • 控制台没有错误:(没用。
  • 你能在你的#app div 之后做{{ users }} 吗?查看挂载的钩子是否正确调用了 jquery 部分
  • 您可以尝试将您的app.js 包含在#app 下方吗?

标签: javascript vue.js


【解决方案1】:

将您的 app.js 文件包含在 body 标记的底部。 当你在做:

var app = new Vue({
el: '#app',

带有#app标签的元素不存在

【讨论】:

  • 感谢您的解释!只是指出该修复程序首先由@sklingler93 提出
猜你喜欢
  • 2014-08-05
  • 2018-01-19
  • 2019-11-09
  • 2017-11-07
  • 2019-03-16
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
相关资源
最近更新 更多