2017-03-14 79 views
0

我試圖從網站運行一個Angular 2應用程序,其中基本url應該是http://www.example.com/app而不是http://www.example.com。我已經嘗試了幾個指南,通過設置基本元素的href(例如:Angular 2 router no base href set)來改變這種情況,但這些都不起作用。我仍然只能訪問根url上的頁面(因此http://www.example.com/something而不是http://www.example.com/app/something)。這是index.html文件目前的樣子:更改Angular2主要網址

<!doctype html> 
<html> 
    <head> 
    <base href="/app"> 
    <title>Test</title> 

    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="icon" type="image/x-icon" href="favicon.ico"> 

    <!-- Bootstrap stylesheet --> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> 

    <!-- Bootstrap (and jQuery) scripts --> 
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> 

    <!-- Font Awesome icons! --> 
    <script src="https://use.fontawesome.com/9fbdc2c47e.js"></script> 
    </head> 
    <body> 
    <app-root> 
     <div class="centered-container"> 
     <!-- Show animated loading icon --> 
     <div id="loading-notification"> 
      <i class="fa fa-refresh fa-spin fa-5x fa-fw"></i> 
     </div> 
     </div> 
    </app-root> 
    </body> 
</html> 

我做了什麼錯了?我在路由器路徑怎麼看起來像一個例子:

RouterModule.forRoot([ 
     { 
     path: "language", 
     component: LanguageComponent 
     }, 
     { 
     path: "404", 
     component: NotFoundComponent 
     }, 
     { 
     path: "**", 
     component: NotFoundComponent 
     } 
    ]), 

UPDATE:

我終於定了!在構建生產應用程序時,應使用以下命令:ng build --target=production --base-href /app/ --aot true。這一切都固定:)

+0

使應該列在app.module.ts裏面的引導標記 – skid

回答

0

你有沒有試過這種

<base href="/app/">

,並在組件

@Component({ moduleId: "app", selector:"my-component", ... })

+0

下的組件應該給它一個:)謝謝 –

+0

嗯,它不工作。當我去http://www.example.com/app時,它給了我一個404,當我去http://www.example.com時,一切正常。請注意,我希望一切(如此應用程序中的所有路線)以/ app –

+0

開頭我還注意到,當項目編譯時,基礎中的href再次變爲「/」。 –

1

你可以在你的路由

path: '', redirectTo: '/app', pathMatch: 'full' 
path: 'app', component:'componentName' 
以下變化

其中無線最終會重定向你到/應用程序每次當一些錯誤的網址時

+0

這不是我正在尋找的東西。我希望所有組件路徑以/ app /開頭,而不是使用服務器的根目錄/ –

+0

您是否使用了systemjs其他項目webpack配置 – skid