2016-09-30 36 views
0

我無法讓我的角應用在本地運行。我收到一個錯誤,我的interview.js和angular.js沒有找到。另外,當我從dev控制檯打開時,angular.js文件中未定義角度。我無法讓我的Angular應用在本地運行

這裏是我的目錄結構:

mock 
public 
    index.html 
src 
    services 
    interview.js 
vendor 
    angular.js 

這裏是我的index.html

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="x-ua-compatible" content="ie=edge"> 
    <title>Interview Practice</title> 
    <meta name="description" content=""> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <script src="../vendor/angular.js" type="text/javascript"></script> 
</head> 
<body ng-app="interviewapp"> 
    hello world! 

    <script src="../src/interview.js" type="text/javascript"></script> 
</body> 
</html> 

這裏是我的interview.js

angular.module("interviewapp", []); 
console.log("does this work"); 

我使用的HTTP服務器啓動應用程序。

Starting up http-server, serving ./public 
Available on: 
    http://10.66.87.184:8080 
    http://192.168.56.1:8080 
    http://127.0.0.1:8080 

去到本地主機,或任何所列的HTTPS的,這是我得到的錯誤:

http://10.66.87.184:8080/vendor/angular.js Failed to load resource: the server responded with a status of 404 (Not Found) 
http://10.66.87.184:8080/src/interview.js Failed to load resource: the server responded with a status of 404 (Not Found) 

我該如何解決這個問題,使我的應用程序運行?

+1

將'src'和'vendor'目錄移動到'public'目錄並從路徑的開始處移除'..'到文件例如將'src =「../ src/interview.js」'更改爲'src =「/ src/interview。js「' –

+0

我鏈接了一個cdn for angular,並且做了你所說的src文件夾。你能告訴我爲什麼這樣工作嗎? – McFiddlyWiddly

+1

爲你添加了一個答案和解釋 –

回答

0

改變你的項目結構看起來像這樣:

mock 
public 
    index.html 
    src 
    vendor 

也改變這一點:

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

這樣:

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

這:

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

這個

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

所以所有的JavaScript的應該是在你的公共目錄。也不要使用包含標籤的相對路徑。因爲它是根文件夾,因此任何公開的內容都應該可訪問。

2

正如評論指出:

移動srcvendor目錄到public目錄,並刪除..從你的路開始到文件如將src="../src/interview.js"更改爲src="/src/interview.js"

這適用,因爲您的服務器使用public作爲根文件夾,因此它無法看到您嘗試訪問的其他文件夾。通過將它們移動到public文件夾,它可以訪問它們。

0

您的服務器正在提供public文件夾。因此,通過您的主機名可以訪問該文件夾中的任何內容。但是您保留srcvendorpublic之外,因此它們不通過您的服務器提供。爲了訪問他們把它們放在public文件夾中,並在js引用之前刪除..

相關問題