Home > Web Front-end > JS Tutorial > How to use counters in JS

How to use counters in JS

php中世界最好的语言
Release: 2018-03-19 16:27:01
Original
3096 people have browsed it

This time I will bring you the use of counters in JS. What are the precautions when using counters in JS. The following is a practical case, let’s take a look.

The learning cost of angular2+ should be the highest among the three major frameworks. Tutorials and cases are scarce and the process is more complicated. Here I use two cases of counter and online acquisition of user data and rendering into a list to help you quickly Get angular2+.

Before you start, I hope you can master rxjs and typescript, otherwise you may find it difficult to understand some of the writing methods.

Before you start, you need to install @ngrx/store and @ngrx/effects

yarn add @ngrx/store @ngrx/effects

The ngrx/effects and ngrx/store versions used in this tutorial are both 5.2.0.

Let’s briefly talk about the development process:

Start -> Write data model-> Write action -> Write reducers and Configure to the corresponding module -> Write services -> Write effects and configure them to the corresponding module -> Create components -> Component binding data model -> Rendering

Let’s complete the counter case first. Since there are no asynchronous tasks in this case, services and effects can be omitted.

The steps from creating the project to starting the initial page will not be discussed here. Note that style should use scss. Also do not use cnpm to install the package. Use yarn or npm instead, so that errors are less likely to occur later.

ng new your-project --style scss
Copy after login

The first step: write the data model (app/models/num.ts)

export class Num {
  count: number;
 
  constructor(num: number) {    this.count = num;
  }
}
Copy after login

The second step: write the action (app /actions/num.ts)

import {Action} from '@ngrx/store';
 
export enum NumActionType {
  Add = 'ADD'}
 
export class ADD implements Action {
  readonly type = NumActionType.Add;  //固定写法,必须叫type}
Copy after login

Step 3: Write redurcers (app/redurces/modelNum.ts)

import {Num} from '../models/num';
import {Action} from '@ngrx/store';
import {NumActionType} from '../actions/num';
 
 
export const modelNum = (state: Num = new Num(0), action: Action) => {       
  switch (action.type) {    case NumActionType.Add:
      state.count++;      return state;    default:      return state;
  }
};
Copy after login

Don’t forget to configureredurcer(app/app.module.ts)

  imports: [
    BrowserModule,
    RouterModule.forRoot(routes),
    StoreModule.forRoot({ modelNum}),      //配置redurcer
  ],
Copy after login

Part 4: Create components

ng g component model-num

Step 5: Component binding data Model (complete the sixth step together)

Componenthtml file:

<p>
  <input (click)="add()" value="+" type="button">
  <p>{{num.count}}</p>
  <input value="-" type="button">
  <br/>
  <a routerLink="/list">to list demo</a></p>
Copy after login

Componentts file:

import {Component, OnInit} from '@angular/core';
import {Num} from '../models/num';
import {Store} from '@ngrx/store';
import {NumActionType} from '../actions/num';
Copy after login
@Component({
  selector: &#39;app-model-demo&#39;,
  templateUrl: &#39;./model-demo.component.html&#39;,
  styleUrls: [&#39;./model-demo.component.scss&#39;]
})
export class ModelDemoComponent implements OnInit {
  constructor(private _store: Store<any>) {
    this._store.select(&#39;modelNum&#39;).subscribe(mNum => {    //涉及到rxjs。    
      this.num = mNum;
      console.log(mNum);
    });
  }
  public num: Num;
  public add() {
    console.log(&#39;add&#39;);
    this._store.dispatch({          //调用dispatch触发添加redurces
      type: NumActionType.Add
    });
  }
  ngOnInit() {
  }
}
Copy after login

Believe it or not After reading the case in this article, you have mastered the method. For more exciting information, please pay attention to the php Chinese websiteOtherrelated articles!

Recommended reading:

JavaScript Optimizing DOM

Vue’s Computed Properties

The above is the detailed content of How to use counters in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template