未捕获的ReferenceError:在Index.html中设置vue设置时未定义Vue

时间:2017-07-29 11:35:01

标签: vue.js vuejs2

我最近了解vue

我有这个文件main.js

import Vue from 'vue/dist/vue.js'
import Buefy from 'buefy'
import 'buefy/lib/buefy.css'
Vue.use(Buefy)

var App = new Vue({
    el: '#app',
    data: {
          message : "It's working"
    }
})

这是我的HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Vue Example</title>
  </head>
  <body>
    <h3 id="app">{{ message }}</h3>
    <script src="dist/build.js"></script>

    <script>

    </script>
  </body>
</html>

它正在运作。但是,现在我正在尝试用我的脚本做点什么。我更改main.js(我正在使用webpack

import Vue from 'vue/dist/vue.js'
import Buefy from 'buefy'
import 'buefy/lib/buefy.css'
Vue.use(Buefy)

然后我的index.html到了这个

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Vue Example</title>
  </head>
  <body>
    <h3 id="app">{{ message }}</h3>
    <script src="dist/build.js"></script>

    <script>
var App = new Vue({
    el: '#app',
    data: {
        message:"It's not working"
    }
})
    </script>
  </body>
</html>

我收到此错误

  

未捕获的ReferenceError:未定义Vue

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:30)

如果您想直接在Vue制作index.html的新实例,则应在index.html中添加该库:

<script src="https://unpkg.com/vue@2.4.2"></script>

或者您需要在Vue中分配windowmain.js对象,如下所示:

<强> main.js:

 import Vue from 'vue';
 window.Vue = Vue;

然后在index.html中您可以访问Vue(),因为它是window对象的全局属性。