Ways to navigate private property using the App Component in Angular 4

I have successfully implemented code in my app component to retrieve the current URL and display it in app.component.html.

app.component.ts

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  public href: string = "";

  constructor(private router: Router) {}

  ngOnInit() {
  }

}

app.component.html

<div class="page-wrap {{router.url}}">

  <app-header></app-header>

  <div class="content-wrap">

    <app-sidebar></app-sidebar>

    <div class="app-content">
      <flash-messages></flash-messages>
      <router-outlet></router-outlet>
    </div>

  </div>

</div>

However, when attempting to compile the code using "ng serve -prod," an error is displayed:

ERROR in C:/Users/theone/Documents/Node Projects SourceTree/dream-angular/src/$$_gendir/app/app.component.ngfactory.ts (58,68): Property 'router' is private and only accessible within class 'AppComponent'.

Answer №1

The message of error clearly points out the issue at hand. The field is designated as private and therefore cannot be reached from the template. Consider using the following code snippet:

constructor(public router: Router) { }

Answer №2

Consider trying

ng build --env=prod 
It appears that ng build --env=prod and ng build --prod are not interchangeable.

This has been reported as a known bug with angular-cli for users of older versions, or you may need to switch it to public

constructor(public router: Router) { }

You can find more information about this issue here: https://github.com/angular/angular-cli/issues/5621

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on accessing a method from a div element in VueJS

Is there a way to only render a div based on a specific condition and call a method when the condition is true? Here's an example of what I'm trying to achieve: <div v-if="timetable === null" @click="mymethodhere"> ...

Utilize Javascript to acquire the redirected URL

Seeking assistance with making a GET call to an API endpoint that redirects me. I have enabled CORS to access the Location, but struggling to find a way through Ajax or Fetch to retrieve only the Redirect URL. Any help would be highly appreciated. ...

Incorporating a static image file into a Material UI cardMedia component within a Next.js project

I am struggling to insert a static image into Material UI CardMedia component. I have tried the following code: const useStyles = makeStyles((theme) => ({ media: { height: 0, paddingTop: "56.25%", // 16:9 }, })); <CardMed ...

ObservableResolve(): Unleashing the power of RxJS5 for handling asynchronous data streams

Hey there! To dive deeper into RxJS, I thought of creating my own custom Rx operator. So here's a straightforward one that seems to be working smoothly: Rx.Observable.prototype.multiply = function (input) { const source = this; return Rx.O ...

Dynamic dropdown menu selection and table display (PHP)

I've been working on building an Ajax / PHP grid that is based on a dropdown selection. The process starts with a dropdown select box on the page, where upon selection, a variable is passed to a PHP page. This PHP page then executes a select statemen ...

Achieving a function call within a Backbone view in Backbone.js

Is it possible to call the plotPort function from the plotLoc function using this.plotPort() instead of self.plotPort()? It seems to not work for Internet Explorer when using self.plotPort(). As a workaround, I added an event to lLoca upon reset to call ...

The choice between using "npm install" and "npm install -g" for

New to the world of node, and feeling a bit lost when it comes to all this "install" stuff. Could someone clarify for me, what sets apart install from install -g? If something is installed with install -g, can it be accessed from anywhere, or is it restr ...

Can anyone provide guidance on how to create this particular shadow effect using CSS?

Is there a method to use CSS to create a green shadow in that particular shape? ...

Utilize MaxMind GeoIP2 to identify the city accurately

I have been using this simple code to retrieve the city name through the maxmind service (geoip2). It has been working flawlessly and I am grateful to a fellow member of this site who shared this code with me. However, there is an issue when the user&apos ...

JavaScript validation is failing to return false when re-entering the password

My JavaScript validation code is working fine. Javascript: All fields return false when re-entering the password, but JavaScript does not return false if it's not working The Re-Enter password JavaScript code is failing to work... An alert is displ ...

Having Trouble Parsing JSON Object with JQuery?

I'm having trouble extracting data from a valid JSON object using jQuery/JavaScript - it always returns as 'undefined'. var json = (the string below). var obj = $.parseJSON(JSON.stringify(JSON.stringify(json))); alert(obj); // aler ...

What is the appropriate Typescript return type to use for a $http request that only returns a successful response with no content?

I recently developed a Typescript service: class SettingsService implements ISettingsService { public info = {}; public backupInfo = {}; public userConfig = {}; public isLoaded = false; constructor( private $http: ng.IHttpSer ...

Creating a modal dialog using a function in a TypeScript file

Hey there, fellow developers! I have a question that might seem simple. So, in my HTML code I've got a Modal Dialog that pops up using ng2 Bootstrap. It's working fine, but... I want to replace this line of code "<button class="btn btn-prim ...

Dilemma of interdependencies between Socket.io and requirejs

I am facing a challenge with my legacy express project that has two servers. The project includes two separate client files: requirejs.config({ baseUrl: '/js' , paths: { "jquery": "lib/jquery/jquery-2.1.1.min", "socket.io" : "lib/socket/ ...

The issue with the functionality of position absolute in CSS when used with JavaScript

<html> <head> <style> #wrapper{ position: absolute; top : 250px; width: 500px; height: 500px; border: 1px solid red; } .tagging{ position: absolute; border: 1px solid black; width : 20px; height: 30px; } & ...

Transferring JavaScript variables to PHP via AJAX and successfully utilizing them

A new WordPress plugin is currently in the works, involving AJAX to send a JavaScript variable as data. The goal is to retrieve and utilize this variable from the data in my PHP plugin template file. Here's a more detailed explanation: I have a file ...

Send data to a webpage and navigate to another webpage using PHP

I am attempting to utilize POST variables in combination with AJAX to send them to a page named results.php: $.ajax({ type: "POST", url: "results.php", data:"score="+score+"&round="+round, success: function(data) { window.locatio ...

After refreshing the page, the console log within the React useEffect function ceases to function

Incorporating the 'useEffect' function to fetch data and store it in the 'ratings' array has proven successful. However, there seems to be an issue when attempting to iterate through the elements of the 'ratings' array for log ...

A guide to successfully sending the 'onClick' function to a child component within React

In my coding project, I have developed a versatile Modal component that can be customized with different headers, bodies, and footers, as well as various arguments for the Reactstrap components. While I am using Reactstrap to create the Modal, the issue is ...

Express Router Setup Error: The Router.use() method is expecting a middleware function, but instead received an undefined value

First and foremost, I want to clarify that I am well aware of the plethora of questions posed on this platform with a similar title. I have already attempted the suggestions provided there, but unfortunately, they either did not work for me or were too sp ...