Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Route to External Link in Angular 5

This might be a dumb question but I am new to Angular and haven't gotten an answer for this.

I was following a tutorial and have my routes set up like this for my Angular 5 project:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { PageComponent } from './page/page.component';

const appRoutes: Routes = [
  {path: '', redirectTo: '/home', pathMatch: 'full'},
  {path: 'home', component: PageComponent, data: {
    page: 'home'
  }},
  {path: 'about', component: PageComponent, data: {
    page: 'about'
  }},
  {path: 'contact', component: PageComponent, data: {
    page: 'contact'
  }},
  {path: 'facebook', component: PageComponent, data: {
    page: 'facebook'
  }},
  {path: '**', redirectTo: '/home', pathMatch: 'full'}
];

@NgModule({
  imports: [RouterModule.forRoot(appRoutes)],
  exports: [RouterModule]
})
export class AppRoutingModule {
}

Let's say I want to route the "facebook" path to my facebook page. I am not sure how to route to pages outside of the Angular Application.

Any help would be appreciated!

like image 722
Orion Avatar asked Mar 22 '18 19:03

Orion


People also ask

What is auxiliary route in Angular?

Auxiliary Routes are independent, secondary routes that can be added to a page along with a primary route. As an example, we can have a side-menu that lives within the primary route with its own router-outlet : that's an auxiliary route.

How do I redirect from one component to another component in Angular 8?

to navigate to an other page you use this syntax: this. router. navigate(['/']); The example above would to to the home page. Save this answer.

How do I enable Angular routing?

Add the AppRoutingModule link The router is dedicated to routing and imported by the root AppModule . By convention, the module class name is AppRoutingModule and it belongs in the app-routing.module.ts in the src/app directory. Run ng generate to create the application routing module.


2 Answers

@Rafael Has left the correct answer as a comment.

You would just create a link tag pointing to facebook

<a href="www.facebook.com/yourpage">Facebook</a>

The routes that you are setting up are all for local paths. In other words, all of those routes are for what is displayed on www.yoursite.com/route1, and what is displayed on www.yoursite.com/route2.

But notice that they will all have yoursite.com for the domain.

If you want to create a link to a site outside of your domain, then you do not want to mess with the angular routing, but instead create a link.

So what you're really asking is how can you show www.facebook.com's page on www.yourpage.com/facebook, which is not really possible.

Without an iframe you cannot display a different website within your own site, even with a route.

If that is really what you're trying to do, then you might want to look into iframes.

like image 96
JBoothUA Avatar answered Nov 07 '22 04:11

JBoothUA


Just use a regular anchor href.

<a href="http://facebook.com">Add Vehicle</a>

or for javascript:

window.location.href = 'http://facebook.com';
like image 34
Matt Avatar answered Nov 07 '22 06:11

Matt