Final Release Candidate of Angular 2: Loading Route Information in Real-Time from Service

I'm having trouble loading route data from a service and automatically creating routes in Angular 2 RC Final. I've been trying to assign route data using the Router.Module.forRoot() function, but I can't figure out how to assign fetched data to it. The code snippet below doesn't work for me and shows an error saying "Property 'forRoot' does not exist on type 'RouterModule'."

Although my approach may be incorrect, the code gives an idea of what I want to achieve. The paths for my app are as follows: mysite.com/london/ mysite.com/new-york/, etc. Is there a way to accomplish this?

The version of the router I am utilizing is: "@angular/router": "~3.0.1"

app.module.ts:

import { NgModule, OnInit } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { RouterModule } from '@angular/router';
import { PathService } from './path.service';
import { Path } from './path';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        RouterModule
    ],
    declarations: [
        AppComponent
      ],
    providers: [
        PathService
    ],
    bootstrap: [AppComponent]
})
export class AppModule implements OnInit {
    private ROUTES: Array<Object>;
    private paths: Path[];

    constructor(
        @Inject(RouterModule) private _routerModule: RouterModule,
        @Inject(PathService) private _pathService: PathService) {}

    ngOnInit() {
        this.getPathData();
    }

    getPathData() {
        this._pathService.getPaths().subscribe((paths: Path[]) => this.paths = paths, (err: any) => { }, () => this.generateRoutes());
    }

    generateRoutes() {
        this.ROUTES = 'do something with these.paths and set the route data here';

        this._routerModule.forRoot(this.ROUTES);
    }
}

JSON:

[
    {
        id: "1",
        name: "London",
        path: "london"
    },
    {
        id: "2",
        name: "New York",
        path: "new-york"
    },
    {
        id: "3",
        name: "Paris",
        path: "paris"
    },
    {
        id: "4",
        name: "Tokyo",
        path: "tokyo"
    }
]

Answer №1

It appears that what you're looking for is quite cutting edge in technology. The concept of lazy loading routes may be the closest solution to your request. Here is a simple demo showcasing how routes are loaded on demand when first visited.

Although I'm not certain if this will fully assist you, it's worth giving it a try :)

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

Difficulty in activating or deactivating form controls in Angular 2 upon receiving an HTTP response

When using formcontrol in Angular, I encountered an issue where I tried to disable the form control based on a variable assigned from an HTTP response. Angular2 gave me a warning message. The warning mentioned that it's not recommended to use the dis ...

Guide to dynamically using array.map based on a condition in React

I am encountering an issue with a modal screen that contains two dropdowns and a text input field. The problem arises when the second dropdown is set to “is empty”, as the text input field should then disappear, leaving just the two dropdown inputs on ...

Ways to obtain the present value in Angular

Hello, I am looking for assistance in displaying Toast notifications on my View whenever the number of notifications increases (i.e., when new data is added to the database). I have attempted to use this function, but I am unsure how to retrieve the curre ...

Calculate the date difference in Nuxt by leveraging the @nuxtjs/moment module

I'm a Nuxt newbie facing an issue with calculating the difference between two dates (user input date and current date). The code I am using works fine in most cases, but when the input date is '2020-03-31' or '2020-01-30', the cons ...

Experiencing excessive delay in loading data into the DOM following a successful response from a service in AngularJS

I am facing a challenge in loading a large set of data into an HTML table. To begin, you need to click on a button: <a ng-click="get_product()">load data</a> This button triggers a function called get_product: $scope.get_product = func ...

Guide to uploading files in Vue.js v3

I'm trying to implement file upload functionality using Vue.js version 3. Although I have successfully imported ref, I am unsure how to utilize it for retrieving file data? FileUploadTest.vue <template> <h1>File Upload</h1> <div ...

Step-by-step guide on setting up cosmosDB databases and containers in azure functions with the node sdk

In my current setup, I have database initialization code that runs on every function request, impacting performance negatively. How can I verify the existence of a container in Cosmos DB using the node SDK? It's recommended to establish static conne ...

What is the best way to transfer a string to a different Vue component?

Within my project, I have a masterData.js file that serves as a repository for my master data. This file retrieves data from my mongo db and distributes it to various components of the project. To facilitate this process, I have implemented a function with ...

inserting a dynamic variable into a JSON string

My goal is to create a javascript object, var systemName = {"system" : varA}; However, I want the object to be structured like `{"system" :"varA"} where varA contains the variable value but is enclosed in double quotes. I attempted {"system" : "'+ ...

Utilizing Express.js: A Guide to Fetching File Downloads with a POST Method

Although GET requests are successful, I am facing challenges when using POST to achieve the same results. Below are the different code snippets I have attempted: 1. app.post("/download", function (req, res) { res.download("./path"); }); 2. app.post ...

`Troubleshooting Issue with Loading Static Files in Django Python`

Below is the code snippet I am attempting to execute: {% load staticfiles %} <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; st ...

Jquery divides large array into multiple new callbacks during parsing

Encountered a small issue where a JSON response contains a byte array with 67615 entries. Occasionally, it inserts `[....,154,156,);jQuery1910039778258679286416_1363006432850(181,104,...] every ~7300 characters When attempting to parse it using the usua ...

Vue.js causing issues with jQuery data table

Utilizing jQuery data-table within Vue.js in a laravel project has presented an issue for me. Although the data loads successfully into the data-table, there seems to be a problem with retrieving the data after it has been loaded. Specifically, the first r ...

Cypress automation script fails to trigger Knockout computed subscription

Within my setup, I have implemented two textboxes and a span to display the result. Date: <input data-bind="value: dateValue"/> Number: <input data-bind="value: dateValue"/> Result : <span data-bind="text: calculatedValue">Result Should ...

Having trouble retrieving returned data after refetching queries using Apollo and GraphQL

I am able to track my refetch collecting data in the network tab, but I am facing difficulty in retrieving and using that data. In the code snippet below where I am handling the refetch, I am expecting the data to be included in {(mutation, result, ...res ...

Necessary within a JavaScript Class

As a newcomer to using classes in JavaScript, I've been exploring the best practices and wondering about how 'requires' work when used within a class. For example, let's say I want to craft an IoT Connection class for connecting to the ...

Passing a JSONArray to a webview using addJavascriptInterface can provide valuable data

Within my Android app assets, I have stored an HTML page that I want to display using a WebView and add parameters to the webpage. To achieve this, I populated all the values in a JSONArray and utilized 'addJavascriptInterface' to incorporate th ...

Creating a personalized input slider: A step-by-step guide

I have a question about how to design a custom input slider with the label inside the field itself. The desired output should look like the screenshot below: https://i.sstatic.net/gE6EJ.png I have successfully created the input field part, but I am stru ...

Why is the size of my array shrinking with every iteration of the for-loop in JavaScript?

I am struggling to change the classname of three elements that share the same classname. Unfortunately, as I loop through my array, it seems to decrease in size with each iteration, preventing me from successfully changing all three elements. Any advice or ...

Formik button starts off with enabled state at the beginning

My current setup involves using Formik validation to disable a button if the validation schema is not met, specifically for a phone number input where typing alphabets results in the button being disabled. However, I encountered an issue where initially, ...