Showing posts with label Angular2. Show all posts
Showing posts with label Angular2. Show all posts

Sunday, 26 March 2017

This week 6/2017

I did fast research of Vue.js (v. 2.2.2) and I'd like to summary what I got to know about it and how it presents itself in compared to Angular 2 and what I think about it.

1. Performance
Project krausest/js-framework-benchmark tested over 20 JavaScript frameworks, below are results.

src: js-frameworks-benchmark4

As you can see in most cases Angular 2 is slower than Vue.js.


2. Size of attached scripts.
In my case small Angular 2 project with 3 additional modules takes 800kb (prod version and after minification). I wonder to know what will be the size with Vue.js. I found comparison of raw frameworks on Vuejs's web side. Vue.js size is about 23kb but Angular 2 about 50kb. It's really interesting....


3. Learning curve
The creators of Vue framework estimate that it is possible to learn their framework in one day or faster if you know AngularJs. I will see ...
In my opinion to learn Angular 2 in one day is impossible. The same is with AngularJs but I think it was easier then with Angular 2.

4. Testing
On project web page unit testing looks similar to Angular Js or Angular 2 unit.



Resources:
  1. https://github.com/krausest/js-framework-benchmark
  2. http://stefankrause.net/js-frameworks-benchmark4/webdriver-ts/table.html
  3. http://www.valuecoders.com/blog/technology-and-apps/vue-js-comparison-angular-react/
  4. https://vuejs.org/v2/guide/comparison.html#Angular-2

Sunday, 15 January 2017

This week 1/2017

Last two weeks I was preparing maven configuration and I was resolving my business problem by Android 2. I noticed a few important tips during that process. I'd like to gather them below:

1.  In my project I use maven plugin to download NodeJS and npm modules. Then I build all application by execution of Npm tasks. I create a jar with built Angular's 2 files. Then in target project I extract these files into target directory.

  • angular 2 maven project pom fragments

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <!-- 1. Install node and npm locally -->
        <execution>
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
                <nodeVersion>v7.3.0</nodeVersion>
                <npmVersion>3.10.10</npmVersion>
            </configuration>
        </execution>
        <execution>
            <id>npm install</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
                <arguments>install</arguments>
            </configuration>
        </execution>
        <execution>
            <id>npm run prod</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
                <arguments>run prod</arguments>
            </configuration>
        </execution>   
        <execution>
            <id>npm run test-one</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <phase>test</phase>
            <configuration>
                <forkMode>always</forkMode>
                <environmentVariables>
                    <CHROME_BIN>${chrome.path}</CHROME_BIN>
                </environmentVariables>
                <arguments>run test-one</arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

  • target project dependency and extracting plugin configuration

1
2
3
4
5
6
7
<dependency>
    <groupId>example</groupId>
    <artifactId>example_angular2</artifactId>
    <version>1.2.3.0-SNAPSHOT</version>
    <type>jar</type>
    <scope>provided</scope>
</dependency> 
 ....

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>unpack_angular2</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <includeGroupIds>example</includeGroupIds>
                <includeArtifactIds>example_angular2</includeArtifactIds>
                <includes>**/*.*</includes>
                <outputDirectory>${static.content.path}</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
 
2. I use angular-cli to manage whole Angular2 project. In angular-cli.json the apps attribute require a array parameter but it never use other elements of this table than first one. Nobody knows what for is there an array attribute.

3. On single webpage can be only one root module. It is chosen by executing bootstrapModule. 

platformBrowserDynamic().bootstrapModule(MainModule);

With this module should be connected one main component which will contain template gathering all other components selectors.
You can use only one main selectors handled by this main component. In my case it looks like:

  • module:

1
2
3
4
5
6
 @NgModule({
    imports:      [ AnalysisModule, DataHistoryModule],
    declarations: [ MainComponent],
    bootstrap:    [ MainComponent],
})
export class MainModule { }
  • component

1
2
3
4
5
6
7
@Component({
    selector: 'app',
    moduleId: module.id,
    template : '<profile-analysis></profile-analysis><data-history></data-history>',

})
export class MainComponent{};
  • part of main html page

1
2
3
<body>
    <app>Loading...</app>
</body>

4. Every event is directly handled by containing component. To notice parent component you should add to selector's tag an attribute with handling method.

  • child component

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
@Component({
    selector: '[htitle]',
    template: '<div (click)="orderColumn(\'data\', idx,\'change\')">Change</div>'
})
export class DataColumnTitleComponent {
    @Input("htitle")
    private value;

    @Output()
    changeOrder = new EventEmitter();

    orderColumn(colType: string, id: number, subType: string): void {
        this.changeOrder.emit({
            value: { colType: colType, id: id, subType: subType }
        });
    }
};
  • parent component which handles event

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@Component({
    selector: 'profile-analysis',
    moduleId: module.id,
    template: ' <tr [htitle] (changeOrder)="onOrderColumn($event)"></tr>',
})
export class AnalysisComponent {
    onOrderColumn(event): void {
        console.log("parent " + event);
    }
}
 
5. It is possible to show selectors tag content in a child element by using ng-content tag. It could be problem to access to this content in a selector's component. I found only one way to do this by execution a native method. However I don't know if it is good practice and it doesn't limits of advantages of virtual DOM. 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@Component({
    selector: 'th-title',
    template: '<th #contentWrapper><ng-content></ng-content></th>',
})
export class WrapperTitleComponent {
    @ViewChild('contentWrapper') content: ElementRef;

    ngAfterViewInit() {
        console.debug(this.content.nativeElement.innerHTML);
    }
};

Saturday, 17 September 2016

This week 14/2016

A few weeks ago I started doing a Angular 2 tutorial. Angular2 is extremely different to first beta version. I chose coding in TypeScript which is recommended language. It is possible to coding in JavaScript or Dart as well but it is less popular.

In my case I chose TypeScript which is compiled to JavaScript and then interpreted by browser. Writing in TypeScript is nice for developer with Java grounds.

Angular 2 defines 4 types of objects: Component, Service, Module and Pipe. 

Before the first final release was published, the concept how to bootstrap application had changed. As I remember, first version of Angular2 allows to execute project without using main module. Later it was changed.
My experience is that the most difficult thing is to match collaborative versions of libraries. I spend much time on selecting exact version of libraries to run some easy application.

And one more think. To create and build production version of code I recommend to use angular-cli.