简体   繁体   English

Angualr2 AngularCLI App无法在node_modules库中找到HTML文件

[英]Angualr2 AngularCLI App cannot find HTML files in node_modules library

I have a Angular2 Angular-CLI app which is consuming a custom library installed via NPM (node_modules folder). 我有一个Angular2 Angular-CLI应用程序,它使用通过NPM(node_modules文件夹)安装的自定义库。 My Angular2 app can located the custom library components at design time. 我的Angular2应用程序可以在设计时找到自定义库组件。 However, when serving the app via ng serve, the app errors at runtime with a 404 error for HTML, CSS files that exist in the custom library installed via NPM: 但是,当通过ng服务提供应用程序时,应用程序错误在运行时出现404错误的HTML,通过NPM安装的自定义库中存在的CSS文件:

http://localhost:4200/cdf-media-slider.component.html 404 (Not Found) http:// localhost:4200 / cdf-media-slider.component.html 404(未找到)

cdf-video.component is a custom Angular2 library I'm trying to get to work inside my app. cdf-video.component是一个自定义的Angular2库,我正试图在我的应用程序中工作。 How do I get my app to serve components and corresponding HTML, CSS files from a library in node_modules? 如何让我的应用程序从node_modules中的库中提供组件和相应的HTML,CSS文件?

Here are my particulars around Angular-CLI: 以下是Angular-CLI的详细信息:

angular-cli: 1.0.0-beta.18 angular-cli:1.0.0-beta.18
node: 6.6.0 节点:6.6.0
os: win32 x64 os:win32 x64

Here's how the custom component is loading HTML file: 以下是自定义组件加载HTML文件的方式:

import { ... }      from '@angular/core';

@Component({
    selector: 'cdf-media-slider',
    templateUrl: './cdf-media-slider.component.html',
    styleUrls: [ './cdf-media-slider.component.less' ]
})
export class CdfMediaGridComponent implements OnInit, AfterViewInit 
{
    //IMPLEMENTATION CODE HERE...
    ...
}

cdf-media-slider.component.html and cdf-media-slider.component.less reside in the same folder as this component. cdf-media-slider.component.html和cdf-media-slider.component.less与此组件位于同一文件夹中。 This code ultimately resides in node_modules. 此代码最终驻留在node_modules中。 I'm using barrel approach for surfacing code where each folder has an index.ts file that exports it's contents. 我正在使用桶方法来表示代码,其中每个文件夹都有一个index.ts文件,用于导出它的内容。 Each parent folder has an index.ts that exports it's child's index etc. 每个父文件夹都有一个index.ts,用于导出它的子索引等。

In my client app that consumes this custom component, I'm importing the component like this: 在我的客户端应用程序中使用此自定义组件,我正在导入组件,如下所示:

import { CdfMediaGridComponent }  from 'ng2cdf/index';

VisualStudio Code can find this file because I have intellisense and it doesn't complain about it being missing. VisualStudio代码可以找到此文件,因为我有智能感知,并且它不会抱怨它丢失。 At run-time however, the HTML and CSS files are not found (404) and the app is looking for them at the root of the app ( http://localhost:4200/cdf-media-slider.component.html ). 但是,在运行时,找不到HTML和CSS文件(404),应用程序正在应用程序的根目录中查找它们( http:// localhost:4200 / cdf-media-slider.component.html )。

How do you get the appropriate path to the HTML and CSS files? 如何获得HTML和CSS文件的相应路径?

Thanks for your help. 谢谢你的帮助。

WebPack is smart enough to grab those files, so long as they're being included right. WebPack非常聪明,可以抓取这些文件,只要它们被正确包含在内。 If you could provide a link to the npm package it would be easier to tell, but you may just need to add a module import. 如果您可以提供指向npm包的链接,那么更容易分辨,但您可能只需要添加模块导入。

If the library doesn't have a module, you could either create one for it, or simply add the components/directives to the main module. 如果库没有模块,您可以为其创建一个模块,或者只是将组件/指令添加到主模块。

In your app.module.ts @NgModule declaration, add CdfMediaGridComponent and any other directives/components in that library to both the declarations and exports properties. app.module.ts @NgModule声明中,将CdfMediaGridComponent和该库中的任何其他指令/组件添加到declarationsexports属性中。 Alternatively, it would be better to make a completely a separate module for this custom library, and just add that module to app.module 's imports property. 或者,最好为这个自定义库创建一个完全独立的模块,然后将该模块添加到app.moduleimports属性中。 Example: 例:

cdf-media.module.ts CDF-media.module.ts

@NgModule({
  declarations: [
    CdfMediaGridComponent,
    CdfSliderDirective,
    EtcDirective,
    ...
  ],
  providers: [],
  imports: [],
  exports: [
    CdfMediaGridComponent,
    CdfSliderDirective,
    EtcDirective,
    ...
  ]
})
export class CdfMediaModule {
}

app.modules.ts app.modules.ts

@NgModule({
  declarations: [
    MyAppComponent
    ...
  ],
  providers: [],
  imports: [CdfMediaModule],
  exports: [
    MyAppComponent
    ...
  ]
})
export class AppModule {
}

 templateUrl: 'app/cdf-media-slider.component.html', styleUrls: [ 'app/cdf-media-slider.component.css' ] 

Try this. 试试这个。 Css. CSS。 no less. 不会少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM