How to use svg icon set package from node_modules in angular4? - html

I have found this open source icon package which I installed in my project featherIcons, and is listed inside my_modules directory. However Im trying to use it but I just cant seem to make it work.
The issue is, I think I have to be somehow importing the package but not sure how or where.
app-component.html
<div class="navbar__nav-container">
<ul class="navbar__nav-container__nav-items">
<li>Platform</li>
<li>Stix/Taxi</li>
<li>Menu</li>
<li>
<i data-feather="circle"></i>
</li>
</ul>
</div>
app.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '#angular/core';
import { RoutingModule } from './routing.module';
import { SharedModule } from './shared/shared.module';
import { CatService } from './services/cat.service';
import { UserService } from './services/user.service';
import { AuthService } from './services/auth.service';
import { AuthGuardLogin } from './services/auth-guard-login.service';
import { AuthGuardAdmin } from './services/auth-guard-admin.service';
import { AppComponent } from './app.component';
import { CatsComponent } from './cats/cats.component';
import { AboutComponent } from './about/about.component';
import { RegisterComponent } from './register/register.component';
import { LoginComponent } from './login/login.component';
import { LogoutComponent } from './logout/logout.component';
import { AccountComponent } from './account/account.component';
import { AdminComponent } from './admin/admin.component';
import { NotFoundComponent } from './not-found/not-found.component';
#NgModule({
declarations: [
AppComponent,
CatsComponent,
AboutComponent,
RegisterComponent,
LoginComponent,
LogoutComponent,
AccountComponent,
AdminComponent,
NotFoundComponent
],
imports: [
RoutingModule,
SharedModule
],
providers: [
AuthService,
AuthGuardLogin,
AuthGuardAdmin,
CatService,
UserService
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
bootstrap: [AppComponent]
})
export class AppModule { }
Any help would be appreciated

Nevermind, I figured the solution, and I'll post the solution here so that it can help the rest.
So as the library states in the link provided, I had to install it like so
npm install feather-icons --save - the --save is very important because it will be referenced in my package.json. Once installed, you will be able to see it in node_modules directory.
Then in which ever component I want to use the library, all I had to do is import it the following way:
Since I wanted to use an icon from the library in app.component.ts:
app.component.ts
import {Component, OnInit} from '#angular/core';
import { AuthService } from './services/auth.service';
/*
- feather-icons is a directory installed in node_modules.
- I dont have to specify the whole path like '../node_modules/path/to/feather-icons'.
- Also rememeber to call the feather.replace() inside ngOnInit
- because it needs to first make sure the component loads first
*/
import * as feather from 'feather-icons';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor (
public auth: AuthService
) { }
ngOnInit() {
feather.replace();
}
}
Then in my app.component.html:
<div class="navbar__nav-container">
<ul class="navbar__nav-container__nav-items">
<li>Platform</li>
<li>Stix/Taxi</li>
<li>Menu</li>
<li>
<i data-feather="circle"></i>
</li>
</ul>
</div>
This was all to it to be honest.
Hope this helps :)

Solution using angular-feather
The advantage of it being that you cherry pick only the icons that you need, thus reducing bundle size.
Install the package
npm install angular-feather
Import the icons you need
Import the icons a la carte in the angular module of your needs
import { NgModule } from '#angular/core';
import { FeatherModule } from 'angular-feather';
import { Camera, Heart, Github } from 'angular-feather/icons';
// Select some icons (use an object, not an array)
const icons = {
Camera,
Heart,
Github
};
#NgModule({
imports: [
FeatherModule.pick(icons)
],
exports: [
FeatherModule
]
})
export class IconsModule {}
Import IconsModule and use component
<i-feather name="heart"></i-feather>
<i-feather name="camera"></i-feather>
Stackblitz demo

Related

Angular - Can't import a component from a library

I'm learning Angular and I decided to create separate generic elements in addition to the main project (in a library) and then import and use them according to my needs.
My problem is due to the fact that I can't import the generic components now... (I can import the Module from the library, but when I try to use my component's selector, Angular warns that the element is unknown to it and doesn't compile the server ).
Generic Component:
(.html)
<p>f-base-screen works!</p>
(.ts)
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'nx-flow-base-screen',
templateUrl: './f-base-screen.component.html',
styleUrls: ['./f-base-screen.component.scss'],
})
export class FBaseScreenComponent implements OnInit {
constructor() {}
ngOnInit(): void {}
}
Library Module:
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { FBaseScreenComponent } from './f-base-screen/f-base-screen.component';
#NgModule({
imports: [
CommonModule
],
declarations: [
FBaseScreenComponent
],
exports: [
FBaseScreenComponent
],
})
export class FLibrariesModule {}
f-libraries.ts (exports the module and its components):
export * from './f-libraries.module';
export * from './f-base-screen/f-base-screen.component';
My SharedModule (it's later imported into my app.module):
import { CommonModule } from "#angular/common";
import { NgModule } from "#angular/core";
import { FLibrariesModule } from "#nx-flow/f-libraries";
#NgModule({
imports: [CommonModule, FLibrariesModule],
exports: [CommonModule, FLibrariesModule],
providers: []
})
export class SharedModule {}
App.module.ts:
import { NgModule } from '#angular/core';
import { HttpClientModule } from '#angular/common/http';
import { BrowserModule } from '#angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FRestService } from './shared/utils/rest.service';
import { SharedModule } from './shared/shared.module';
#NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, HttpClientModule, SharedModule],
exports: [],
providers: [FRestService],
bootstrap: [AppComponent],
})
export class AppModule {}
My Home.component.module.ts (which imports the SharedModule to use the nx-flow-base-screen component):
import { NgModule } from '#angular/core';
import { WelcomeService } from '../../shared/services/flow-services/welcome.service';
import { SharedModule } from '../../shared/shared.module';
import { FRestService } from '../../shared/utils/rest.service';
import { HomeComponent } from './home.component';
#NgModule({
declarations: [HomeComponent],
imports: [SharedModule],
providers: [WelcomeService, FRestService],
bootstrap: [HomeComponent],
})
export class HomeComponentModule {}
My Home.component.html (using the component, the error that appears in the image is the same as angular throws when I compile):
<div>
<span>Home Page</span>
</div>
<nx-flow-base-screen></nx-flow-base-screen>
I already researched and followed tutorials, none of them worked so far. Are there any changes I still need to make to be able to use my component, from the library, within my main project?
If any more files are needed to reach a conclusion, I'll post a print of it.
Error:
apps/flow/src/app/views/home/home.component.html:4:3 - error
NG8001: 'nx-flow-base-screen' is not a known element:
If 'nx-flow-base-screen' is an Angular component, then verify that it is part of this module.
If 'nx-flow-base-screen' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component
to suppress this message.

Cannot export service angular library

Hi i am trying to export a service from my library but i am always getting an error.
this is my module where i import the service:
import { BrowserModule } from '#angular/platform-browser';
import { NgModule, APP_INITIALIZER } from '#angular/core';
import { AppConfigService } from 'ecarelib/lib/services/app-config.service';
#NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
],
bootstrap: [AppComponent],
providers: [AppConfigService]
})
export class AppModule { }
Public-api:
export * from './lib/services/app-config.service';
ERROR in ./src/app/app.module.ts
Module not found: Error: Can't resolve 'ecarelib/lib/services/app-config.service' in 'C:\Users\ArneVandenEynden\vitalsigns\Ecare.VitalSigns.Client\src\app'
I think this is because the import path for 'AppConfigService' provided in AppModule is incorrect. You should give the relative path there.
For example:
If the AppConfigService's path is src>ecarelib>lib>services>app-config.service and AppModule's path is src>app>app.module, then the import path that we should provide is the relative path as shown below:
import { AppConfigService } from '../ecarelib/lib/services/app-config.service';
Instead of :
import { AppConfigService } from 'ecarelib/lib/services/app-config.service';
Hope this would resolve the issue.

Why does my Angular app display the path of my html-file and not its contents?

I am starting with Angular. I am creating a very simple App, that uses two components. A default app component and a second component that is used in the html-file of the app component.
However, when running the app (see files below for the relevant files), I get the following output:
App component
./server.component.html
Instead of what is actually in the html-file, in my case:
App component
The server component
Anyone knows what I am doing wrong?
Here is my Module: (app.module.ts)
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { AppComponent } from './app.component';
import { ServerComponent } from './server/server.component';
#NgModule({
declarations: [
AppComponent,
ServerComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
The html-file of my main component (app.component.html)
<p>App Coponent</p>
<hr>
<app-server></app-server>
The server component (server/server.component.ts)
import { Component } from '#angular/core';
#Component({
selector: 'app-server',
template: './server.component.html'
})
export class ServerComponent {
}
and, finally, the html-file of the server component (server/server.component.html)
<p>The server component</p>
Change
#Component({
selector: 'app-server',
template: './server.component.html'
})
to
#Component({
selector: 'app-server',
templateUrl: './server.component.html'
})

how to implement ng2-dragula in angular 2

how to implement ng2-dragula in angular 2.
My code is shared below,
****viewer.component.ts****
import { Component, Input, Output, EventEmitter } from '#angular/core';
import { Injectable } from '#angular/core';
import { Headers, Http, Response } from '#angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import { ImageViewerService } from '../services/http.services';
import { Dragula,DragulaService } from 'ng2-dragula/ng2-dragula';
#Component({
moduleId: module.id,
selector: 'viewer',
directives: [Dragula],
viewProviders: [DragulaService],
providers: [ImageViewerService, DragulaService],
templateUrl: 'viewer.component.html',
})
export class ViewerComponent {
#Output() click = new EventEmitter();
http: Http;
url: string;
obj: any;
getData: string;
postData: string;
users: any;
firtsImageToBind: any;
tile: number;
tableVisible: boolean;
status: { isopen: boolean } = { isopen: false };
localImageViewerService: ImageViewerService;
localDragulaService: DragulaService;
constructor(http: Http, imageViewerService: ImageViewerService,
localDragulaService: DragulaService) {
this.users = [1];
this.http = http;
this.localImageViewerService = imageViewerService;
this.localDragulaService = localDragulaService;
this.imageReceivedFromWebAPI();
}
Ok so a quick example showing how to implement dragula into Angular2 project first install via npm:
npm install ng2-dragula dragula --save
Next import into module:
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { AppComponent } from './app.component';
import {DragulaModule} from 'ng2-dragula/ng2-dragula'; // Here
#NgModule({
declarations: [
AppComponent
],
imports: [
DragulaModule, // Here
BrowserModule,
FormsModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Inside app.component.html you do this.
<div>
<div class='wrapper'>
<div class='container' [dragula]='"first-bag"'>
<div>You can move these elements between these two containers</div>
<div>Moving them anywhere else isn't quite possible</div>
<div>There's also the possibility of moving elements around in the same container, changing their position</div>
</div>
<div class='container' [dragula]='"first-bag"'>
<div>This is the default use case. You only need to specify the containers you want to use</div>
<div>More interactive use cases lie ahead</div>
<div>Make sure to check out the <a href='https://github.com/bevacqua/dragula#readme'>documentation on GitHub!</a></div>
</div>
</div>
</div>
run the project, and you'll be able to drag the text up and down i.e changing its order.
Example specified above is from dragula github page.

Can't I use a component defined in a file other than app.component.ts in HTML directly?

I am facing difficulty in using a component defined in a file named navigation.component.ts directly on HTML Page.
The same component works fine if I use it under template of a component defined on app.component.ts.
Contents of app.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { AppComponent } from './app.component';
import { NavigationComponent} from './shared/navigation.component';
#NgModule({
imports: [BrowserModule],
declarations: [AppComponent, NavigationComponent],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Contents of navigation.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'navigation',
templateUrl: '/views/shared/navigation.html'
})
export class NavigationComponent {
userName: string = 'Anonymous';
}
Contents of app.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'main-app',
template: '<navigation></navigation><h1>{{pageTitle}}</h1>'
})
export class AppComponent {
pageTitle: string = 'Portal 2.0';
}
Contents of index.html
<body>
<main-app></main-app>
</body>
The above works and renders menus on top but when I try to use <navigation> directly (given below) it doesn't render it, doesn't show any errors either.
<body>
<navigation></navigation>
</body>
Am I doing something wrong?
And the bigger question is how I go debugging issues like this?
Yes you can use web components. Add all the components that you want to load to entrycomponents.
Using createCustomElement you can create elements and use their selector anywhere.
import { BrowserModule } from '#angular/platform-browser';
import { NgModule, Injector } from '#angular/core';
import { createCustomElement } from '#angular/elements';
import { AppComponent } from './app.component';
#NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
entryComponents: [AppComponent]
})
export class AppModule {
constructor(private injector: Injector) {
console.log('Elements is loaded: Activation');
this.registerComponent('metro-activation-loader', AppComponent);
}
public ngDoBootstrap(): void {
console.log('Elements is loaded: Activation ngDoBootstrap');
}
// tslint:disable-next-line:no-any
private registerComponent(name: string, component: any): void {
const injector = this.injector;
const customElement = createCustomElement(component, { injector });
customElements.define(name, customElement);
}
}