我有一個像這樣的簡單路由:
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{
path: 'scenario',
loadChildren: () => import('./pages/scenarios/scenario/scenario.module').then( m => m.ScenarioPageModule)
}
]
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule { }
然后在 app.component.ts
export class AppComponent {
{
this.initializeApp();
}
initializeApp() {
//Get urlParameter
this.getParameter()
if(this.source == "kiosk"){
//go to route
this.navController.navigateRoot('scenario');
}
}
getParameter() {
if (document.URL.indexOf("?") > 0) {
let splitURL = document.URL.split("?");
let splitParams = splitURL[1].split("&");
let i: any;
for (i in splitParams){
let singleURLParam = splitParams[i].split('=');
console.log(singleURLParam[1])
if (singleURLParam[0] == "utm_source"){
this.source = singleURLParam[1];
}
}
}
}
}
如您所見,我讀取了 URL 引數,如果它等于“Kiosk”,它將重定向到場景頁面。因此,如果我在 localhost 上運行專案并嘗試使用此 URL:http://localhost:8100/scenario/?utm_source=kiosk它可以作業,它會重定向到場景并做好一切。
當我在生產中發布時問題就開始了,我嘗試的 URL 是:myapp.company.com/scenario/?utm_source=kiosk它只是回傳 404 - 找不到檔案目錄
任何幫助表示贊賞,謝謝
uj5u.com熱心網友回復:
Angular Web 服務器充當反向代理,其行為如下:
- 如果傳入的請求與資產檔案(CSS、JS、影像...)匹配,則提供它
- 否則,提供
index.html檔案以便在瀏覽器中執行 JavaScript 代碼
當index.html提供服務并執行 JavaScript 時,當前 URL 被視為 Angular 路由并顯示相應的布局/組件。
無論您嘗試在哪里部署,它都應該具有 URL 重寫能力,并且您應該對其進行配置以模仿 Angular 的開發服務器行為。
以下是三個最流行的 Web 服務器的一些有用資源:
- 阿帕奇檔案
- Nginx 檔案
- IIS
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415343.html
標籤:
