Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NativeScript cannot find Angular components

I've just started out with Angular and NativeScript, for the life of me, I cannot figure out why I cannot get my references to components to work.

First of all, I generate the initial file with: tns create Test --ng.

Next I create the following file structure:

App_Resource
components
---app
------app.component.ts
---create
------create.component.ts
------create.component.html
app.module.ts
app.routing.ts
main.ts

This is the app.component.ts

import { Component } from "@angular/core";
import { Router } from "@angular/router";
@Component({
    selector: "my-app",
    template: "<page-router-outlet></page-router-outlet>"
})
export class AppComponent { }

This is the create.component.ts

import { Component } from "@angular/core";
import { Location } from "@angular/common";
import { Router } from "@angular/router";
import * as AppSettings from "application-settings";
@Component({
    selector: "create",
    templateUrl: "./components/create/create.component.html",
})
export class CreateComponent { //... }

This is the app.module.ts

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { AppRoutingModule } from "./app.routing";
import { AppComponent } from "./compontents/app/app.component";
import { CreateComponent } from "./compontents/create/create.component"
@NgModule({
bootstrap: [
    AppComponent
],
imports: [
    NativeScriptModule,
    AppRoutingModule
],
declarations: [
    AppComponent,
    CreateComponent
],
providers: [

],
schemas: [
    NO_ERRORS_SCHEMA
]
})

This is the app.routing.ts

import { NgModule } from "@angular/core";
import { NativeScriptRouterModule } from "nativescript-angular/router";
import { Routes } from "@angular/router";
import { CreateComponent } from "./compontents/create/create.component"
const routes: Routes = [
    { path: "", redirectTo: "/compontents", pathMatch: "full" },
    { path: "create", component: CreateComponent }
];
@NgModule({
    imports: [NativeScriptRouterModule.forRoot(routes)],
    exports: [NativeScriptRouterModule]
})
export class AppRoutingModule { }

And this is main.ts

import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app.module";
platformNativeScriptDynamic().bootstrapModule(AppModule);

Problem

When I do tns run android I get the following error messages:

Error: Could not resolve components/create/create.component.html. 
Looked for: /data/data/org.nativescript.AngularTag/files/app/components/create/create.component.html

From the error, it seems like it looks for the components in the wrong folder, however, I'm not exactly sure why.

like image 690
OverflowStack Avatar asked Mar 19 '26 16:03

OverflowStack


1 Answers

in your components declaration include moduleId: module.id, then drop the full path for your templateUrl(same would go for a stylesheet).

for example, your create component should be.

@Component({
    moduleId: module.id,
    selector: "create",
    templateUrl: "./create.component.html",
})
like image 158
JoshSommer Avatar answered Mar 22 '26 06:03

JoshSommer