How to use a bower component in a angular 2 Project

蓝咒 提交于 2020-01-04 05:38:09

问题


I am beginner to angular 2. Angular 2 project use npm packages and we can use them in angular 2 projects by simply importing as follows

import { FormsModule } from '@angular/forms';

also, we can import and install npm packages using package.json file.

But the problem is I have to import bower component in my angular 2 project. this is the link for installation and guideline for use that bower component (https://www.predix-ui.com/?show=getting_started&type=local)

I copied the bower components into assets folder in my project and tried to import that bower component in my index.html as follows

<head>
  <meta charset="utf-8">
  <title>TestApp</title>
  <base href="/">
  <link rel="import" href="./assets/px-spinner/px-spinner.html" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>

Then I tried to use them in my app.component.html file which is in my src/app as follows.

<h1>
  {{title}}
</h1>
<px-spinner>
  <px-inbox height="100vh" list-items='[{"id":"1","title":"GT Vibration","subtitle":"Block 2","severity":"important","date":"2016-10-05T01:29"}]'>
    // define or bind to your details view here
  </px-inbox>

</px-spinner>

but then it gives the following error.

zone.js:388Unhandled Promise rejection: Template parse errors:
'px-inbox' is not a known element:
1. If 'px-inbox' is an Angular component, then verify that it is part of this module.
2. If 'px-inbox' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("
</h1>
<link rel="import" href="./assets/px-spinner/px-spinner.html" />
[ERROR ->]<px-inbox height="100vh" list-items='[{"id":"1","title":"GT Vibration","subtitle":"Block 2","severity"): AppComponent@4:0 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors:
'px-inbox' is not a known element:
1. If 'px-inbox' is an Angular component, then verify that it is part of this module.
2. If 'px-inbox' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("
</h1>
<link rel="import" href="./assets/px-spinner/px-spinner.html" />
[ERROR ->]<px-inbox height="100vh" list-items='[{"id":"1","title":"GT Vibration","subtitle":"Block 2","severity"): AppComponent@4:0
    at TemplateParser.parse (http://127.0.0.1:4200/main.bundle.js:13787:19)
    at RuntimeCompiler._compileTemplate (http://127.0.0.1:4200/main.bundle.js:32817:51)
    at http://127.0.0.1:4200/main.bundle.js:32737:62
    at Set.forEach (native)
    at RuntimeCompiler._compileComponents (http://127.0.0.1:4200/main.bundle.js:32737:19)
    at createResult (http://127.0.0.1:4200/main.bundle.js:32633:19)
    at ZoneDelegate.invoke (http://127.0.0.1:4200/main.bundle.js:62681:26)
    at Zone.run (http://127.0.0.1:4200/main.bundle.js:62563:43)
    at http://127.0.0.1:4200/main.bundle.js:62951:57
    at ZoneDelegate.invokeTask (http://127.0.0.1:4200/main.bundle.js:62714:35)

Now I am confused about how to use them in my project. Please give me a hint.

Thanks


回答1:


I guess you're looking for something like this:

schemas: [CUSTOM_ELEMENTS_SCHEMA]

within your @NgModule

Plunker Example




回答2:


webcomponents-lite.js polyfill is a prerequisite according to the readme of px-spinner .

Add the following in index.html

<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>


来源:https://stackoverflow.com/questions/41009301/how-to-use-a-bower-component-in-a-angular-2-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!