0

I am trying to write a simple UI service that's built upon Eureka Client, Ribbon Client, Spring Boot and Angular. I have a simple front in angular that has the following routing:

const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'course/:id', component: DetailedCourseComponent },
{ path: 'coursefix', component: DetailedCourseComponent }, // I did this one to test the parameter issue.

{ path: '', redirectTo: '/home', pathMatch: 'full' },
];

And this specific course component:

DetailedCourseComponent.html

<p>detailed-course works!</p>

as you can see, I'm not too interested in the id parameter for the moment but I will use it eventually.

Whenever I test running the url for course/1 with angular on localhost through the CLI console (No spring boost, eureka, ribbon, etc) it works correctly showing the following message: "detailed-course works!"

Now whenever I compile the front into the "resources/static" folder with angular CLI and run the website from IntelijIdea it fails to access the url: "course/1" with an empty white screen. Although, if I enter the url /coursefix (Without any parameters) I get the "detailed-course works!" any idea of what can be causing this url parameter issue? I tried googling a lot but found no luck so far.

To clarify: Routing works correctly (When deployed with Spring boost), redirection also works once I enter the URL: http://localhost:9092/ => http://localhost:9092/home/

Just in case, this is my angular.json file

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ui": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "baseHref": "./",
            "outputPath": "../../resources/static",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "ui:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "ui:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "ui:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "ui:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "ui:serve:production"
            }
          }
        }
      }
    }},
  "defaultProject": "ui"
}

2 Answers 2

1

When Angular is built, it creates a problem with URL and routing reference, you can only navigate if you access localhost -> url1 -> url2. If you try to go straight to url2, it doesn't work.

To answer this problem you need to add a LocationHashStrategy (that's how I managed to fix it). This adds a # in the URL, in case you don't like it, it doesn't help (i.e. localhost/#/home)

app-routing.module.ts:

...
RouterModule.forRoot(routes, {useHash: true})
...
Sign up to request clarification or add additional context in comments.

Comments

0

try to add this to the app.module

import { LocationStrategy, PathLocationStrategy } from '@angular/common';

providers: [
    {
        provide: LocationStrategy,
        useClass: PathLocationStrategy
    }
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.