我刚刚开始使用Angular和NativeScript,对于我的生活,我无法弄清楚为什么我无法获得对组件的引用。
首先,我生成初始文件: tns create Test -ng 。
接下来,我创建以下文件结构:
App_Resource
components
---app
------app.component.ts
---create
------create.component.ts
------create.component.html
app.module.ts
app.routing.ts
main.ts
这是 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 { }
这是 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 { //... }
这是 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
]
})
这是 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 { }
这是 main.ts
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app.module";
platformNativeScriptDynamic().bootstrapModule(AppModule);
问题
当我运行android 时,我收到以下错误消息:
Error: Could not resolve components/create/create.component.html.
Looked for: /data/data/org.nativescript.AngularTag/files/app/components/create/create.component.html
从错误中看,它似乎在错误的文件夹中查找组件,但是,我不确定为什么。
答案 0 :(得分:8)
包括moduleId: module.id
,然后删除templateUrl
的完整路径(同样适用于样式表)。
@Component({
moduleId: module.id,
selector: "create",
templateUrl: "./create.component.html",
})