angular - 奇怪的 NativeScript/Angular2 问题,应用程序在错误的位置查找模板

标签 angular webpack barcode-scanner nativescript

我有一个非常基本的 Nativescript/Angular2 条形码扫描仪应用程序,我刚刚开始在其中实现路由...

我有一个 Home 组件,位于 app/pages/home/home.component.ts并且 home 组件的模板位于同一文件夹 app/home/home.component.html 中当我构建应用程序时,一切似乎都工作正常,直到应用程序部署在 genyMotion 模拟器上。然后我收到以下错误。

看起来它正在寻找 app/home.component.html 下的 home 组件的模板。因为某些原因?知道为什么会发生这种情况吗?我检查了引用 home 组件的所有模块的导入语句都是正确的。知道我该如何弄清楚这里发生了什么吗?

我的错误:

An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Unable to resume activity {org.nativescript.barcodescanner/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: 
Calling js method onCreateView failed

Error: File /data/data/org.nativescript.barcodescanner/files/app/home.component.html does not exist. Resolved from: home.component.html.
File: "/data/data/org.nativescript.barcodescanner/files/app/tns_modules/nativescript-angular/resource-loader.js, line: 22, column: 12

堆栈跟踪:

    Frame: function:'FileSystemResourceLoader.get', file:'/data/data/org.nativescript.barcodescanner/files/app/tns_modules/nativescript-angular/resource-loader.js', line: 22, column: 19
    Frame: function:'DirectiveNormalizer._fetch', file:'/data/data/org.nativescript.barcodescanner/files/app/tns_modules/@angular/compiler/bundles/compiler.umd.js', line: 13661, column: 45
    Frame: function:'DirectiveNormalizer.normalizeTemplateAsync', file:'/data/data/org.nativescript.barcodescanner/files/app/tns_modul
    
    

我的 home.component.ts:

import { Component,} from '@angular/core';

import { RestService } from '../../services/rest.service';
import { LocalStorageService } from '../../services/local-storage.service';

@Component({
  selector: 'home',
  templateUrl: './home.component.html' 
})
export class HomeComponent {

  constructor(private restService: RestService, private localStorageService: LocalStorageService) {

}
    

}

我的app.routing.ts:(由于我正忙于将网络应用程序转换为移动设备,并且仍在添加所有组件的过程中,因此该文件中注释掉了很多内容...)

import { AppComponent } from './app.component';
//import { ProductComponent } from './components/product/product.component';
//import { StockTransactionComponent } from './Components/stock-transaction/stock-transaction.component';
//import { CheckBarcodesComponent } from './Components/check-barcodes/check-barcodes.component';
import { HomeComponent } from "./pages/home/home.component";
//import { SettingsComponent } from './Components/settings/settings.component';

export const routes = [
    //{path: "settings", component: SettingsComponent },
//  {path: "checkBarcodes", component: CheckBarcodesComponent },    
    {path: "", component: HomeComponent}
];

export const navigatableComponents = [
HomeComponent
];

我的app.module.ts:

import { NgModule, ValueProvider } from "@angular/core";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { NativeScriptModule } from "nativescript-angular/platform";
import { NativeScriptRouterModule } from 'nativescript-angular/router'
import { NativeScriptHttpModule } from 'nativescript-angular/http';


import { BarcodeScanner } from "nativescript-barcodescanner";

import { RestService } from './services/rest.service';

import { AppComponent } from "./app.component";
import { HomeComponent } from "./pages/home/home.component";
import { routes, navigatableComponents } from './app.routing';
@NgModule({
    imports : [
    NativeScriptModule,     
    NativeScriptFormsModule ,
    NativeScriptHttpModule,
    NativeScriptRouterModule,
    NativeScriptRouterModule.forRoot(routes)
    ],
    declarations : [
    AppComponent,
    HomeComponent,
    ...navigatableComponents
    ],
    providers : [
    RestService,
    BarcodeScanner],
    bootstrap : [AppComponent]      
})
export class AppModule {}

my app.component.ts:


import { Component } from "@angular/core";



@Component({
    selector: "main",
    template : "<page-router-outlet></page-router-outlet>"
})
export class AppComponent {}

最佳答案

您是否尝试过将 moduleId 添加到 @Component 定义中?我读过它破坏了 Webpack 兼容性,但我不知道这对你是否重要:

@Component({
  moduleId: module.id,
  templateUrl: "home.component.html",
  // ..
})

然后将 home.component.html 放在与此组件相同的文件夹中。

关于angular - 奇怪的 NativeScript/Angular2 问题,应用程序在错误的位置查找模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41433707/

相关文章:

android - 在 fragment 中使用 ZXing

python - 使用 xvkbd 读取条形码。如何禁用Enter键?

javascript - Angular 2+ 如何在打开模态时将焦点集中到模态输入?

javascript - Angular 的最佳实践

javascript - babel loader 仅处理文件

Webpack-Dev-Server 一直在寻找 src 文件夹

angular - Azure 应用服务上的 Angular 应用程序出现应用程序错误

node.js - Angular 2 Firebase 应用程序中的 net::ERR_CONNECTION_CLOSED

javascript - 我可以用 webpack 缩小而不丑化吗

android - 如何判断输入的是扫码器还是软键盘?