2017-07-29 21 views
2
放VUE設置

我最近學習是沒有定義的Vue約vue未捕獲的ReferenceError:當index.html中

我有這個文件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> 

和我得到這個錯誤

Uncaught ReferenceError: Vue is not defined

我怎樣才能解決這個問題?

+0

我覺得'var App = new Vue()'應該在'main.js' –

回答

6

如果你想的Vue一個新實例直接index.html您應該包括在你的index.html庫:

<script src="https://unpkg.com/[email protected]"></script> 

,或者您需要分配Vuewindow對象main.js像這樣:

main.js:

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

然後在index.html您有權訪問Vue(),因爲它是window對象的全局屬性。

相關問題