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

There are multiple ways to extract a value from Python code and assign it to a JavaScript variable in a JS file

I am currently working on developing the frontend for a voice bot using JavaScript, while the backend is written in Python. if hi == 0: talk('hello iam kavi') print('hello iam kavi Voice assistant') talk('How are you bu ...

Order ArrayList elements based on specified conditions using SQL query or Java

Within my database, I have a table for employees that includes columns such as emp_id, superior_id, and name. I am attempting to create a tree structure using a JavaScript library. Each employee has a superior_id except for the manager. In order to achie ...

Guide for integrating the shadcn/ui Range Date Picker within a Form

Encountering an issue with using The Range Date Picker within the Form component. Specifically, I am looking to store {from, to} values of the range in an object, however, utilizing an object as a Form field value results in error messages not functioning ...

What is the best way to establish a maximum limit for a counter within an onclick event?

I'm struggling to figure out how to set a maximum limit for a counter in my onclick event. Can someone help me? What is the best way to add a max limit to the counter of an onclick event? Do I need to use an if statement? If yes, how should it be w ...

What is the best way to add a border around an image along with a button using VueJS?

I am struggling to link a button and an image in VueJS to display a border around the picture. While I can successfully display the border on the button, I am unsure how to extend it to the image as well. Vue.component('my-button', 'my-img& ...

Adding complex JSON format to an HTML table involves formatting the data correctly and then using

Utilizing AJAX, I fetched a JSON response and am now looking to map the JSON data into an HTML table structured like this: { "records": [{ "type_id": 000001, "type_desc": "AAAAAA", "type_createby": "Adam" }, { "type ...

"Utilizing the jQuery append method to dynamically insert an SVG element

Currently, I'm in the process of constructing a graph by utilizing svg elements and then dynamically creating rect elements for each bar in the graph through a loop. I have a query regarding how I can effectively pass the value of the "moveBar" varia ...

The combination of Backbone.js and underscore.js is causing disorganized HTML code

When it comes to creating HTML elements, I typically use a combination of backbone.js and underscore.js. Here is an example snippet from my usual workflow: _.template($('#html-container').html(), this.model.toJSON()); I then append this code wh ...

Solution for fixing the error: MongooseError [OverwriteModelError]: It is not possible to overwrite the `User` model after it has been compiled in

I am new to working with the MERN stack and currently attempting to create an exercise tracker app following a tutorial on YouTube. However, I am encountering the Mongoose: OverwriteModelError when running the server and cannot seem to identify where I am ...

Capture the variable within the AJAX success callback in order to prevent the form from being submitted

I am facing an issue with this code where I need to make a decision on whether to submit the form based on the result of an ajax call. When I hardcode it with return false; it prevents the form from redirecting to the PayPal site. That's why I want i ...

Leveraging jQuery alongside HTML5 Video

I have a website that offers live streaming services. I wrote some code specifically for iPads which successfully plays the stream: window.location = 'http://<?php echo DEVSTREAMWEB; ?>/<?php echo $session_id;?>/'+camerahash+'/p ...

Output of ngResource compilation

Is there a way to retrieve a single result from my array $scope.trailers? I am encountering an issue where accessing the first index using $scope.trailers[0] returns undefined. The API call is made using ngResource. function getTrailers(pageNo){ ...

Tips for choosing specific characters from a string using JavaScript

When dealing with URL strings like "example.com/Apps/Visitor/visitscanner" , "example.com/Apps/Seatings/visitscanner I am interested in extracting the directory name following the "http://example.com/Apps/" For the above examples, if the URL string is " ...

Creating CSS styles for fluid width divs with equal height and background images

Looking to create a layout with two side-by-side divs of equal width - one containing an image and the other dynamic text that can vary in height from 400px to 550px based on input. The goal is for the image to align flush at the top and bottom with the te ...

Why is the lower sub-component positioned above the rest of the page?

I have set up a sandbox environment to demonstrate an issue that I am facing. The main problem is that when I click on "Option 1" in the main menu, a new component appears where a bottom sub-component (named BottomControls.js) is displayed at the top of t ...

Unleashing the Power of CodeIgniter with Ajax

I have a form with hidden sections that are revealed when the correct data is submitted. My goal is to achieve this using jQuery and ajax. I want the next element on the form to be shown only if the last piece of entered data has been successfully saved in ...

Why won't Node.js let me redirect to my error page?

I've been putting together my newsletter project with the Mailchimp API, everything seems to be working fine except for when I try to redirect to a failure page if the status code is not 200. The browser shows an error message saying 'localhost r ...

JavaScript - Capture the Values of Input Fields Upon Enter Key Press

Here's the input I have: <input class="form-control" id="unique-ar-array" type="text" name="unique-ar-array" value="" placeholder="Enter a keyword and press return to add items to the array"> And this is my JavaScript code: var uniqueRowsArr ...

What about a lightbox with enhanced jQuery features?

As a newcomer to jQuery, I've never experimented with lightboxes before. However, I was tasked with creating something fun for April Fools' Day at work. Naively, I accepted the challenge thinking it would be simple, but now I find myself struggli ...

Unable to generate onsen-ui popover

My expertise lies in utilizing the Monaca platform for developing mobile applications using Onsen UI and AngularJS. I am looking to incorporate a popover feature from Onsen into my app, and have tried the following code snippet: angular.module('app& ...