1

我想为我尝试过 ngx-pagination 的表添加分页,但它似乎不适用于 angular5:只是一个例子

<table>
   <tr *ngFor="let x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
   </tr>
 </table>

搜索了很多,但发现了这个:


ngx-分页

这个包在导入时给了我错误说:

无法解析符号 NgxPaginationModule

我正在使用angular5 package.json

    {
  "name": "eci",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "node app.js",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "angular-font-awesome": "^3.1.2",
    "angular2-json2csv": "^1.1.2",
    "angular5-csv": "^0.2.8",
    "bootstrap": "^4.1.0",
    "cfenv": "^1.0.4",
    "core-js": "^2.4.1",
    "express": "^4.15.0",
    "font-awesome": "^4.7.0",
    "ngx-pagination": "^3.1.1",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "~1.7.2",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  }
}
4

3 回答 3

1

尝试在 app.module.ts 中导入NgxPaginationModule。尝试这个

// app.module.ts
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {NgxPaginationModule} from 'ngx-pagination'; // <-- import the module
import {MyComponent} from './my.component';

@NgModule({
    imports: [BrowserModule, NgxPaginationModule], // <-- include it in your app module
    declarations: [MyComponent],
    bootstrap: [MyComponent]
})
export class MyAppModule {}
于 2018-04-27T11:16:57.717 回答
1

( at first sorry for my bad english) 为此,您可以执行以下操作: ( 1 ) 使用此命令添加库:( npm install ngx-pagination@3.1.1 --save@3.1.1 -> 以防万一您必须使用此版本),然后 ( 2 ) 将此库导入您的 app.module。 ts (完成此操作后,您的 app.module 如下所示):

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CommonModule } from '@angular/common';
import { NgxPaginationModule } from 'ngx-pagination';  /* import NgxPaginationModule here */

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,
        CommonModule,     /* always check has this line this */
        NgxPaginationModule    /* add NgxPaginationModule to your imports */
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

并将其添加到您要在其中使用此模块的组件中:(我将其添加到我的 app.component.ts):page = 1; 而我的 app.component.ts 在这个改变之后是:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  // Your Data
  names: any = [
    {
      Name: 'frank',
      Country: 'US'
    },
    {
      Name: 'joe',
      Country: 'fanland'
    },
    {
      Name: 'mary',
      Country: 'nourthAmerica'
    },
    {
      Name: 'elina',
      Country: 'US'
    },
    {
      Name: 'emmi',
      Country: 'Argentina'
    },
    {
      Name: 'jully',
      Country: 'Belize'
    },
    {
      Name: 'mariana',
      Country: 'Cameroun'
    },
    {
      Name: 'shivana',
      Country: 'Costa Rica'
    },
    {
      Name: 'jullietta',
      Country: 'Danemark'
    }];
  // number
  page = 1;    /* this show page in pagination controller after page load */
  constructor() {}
}

现在怎么用?像管道一样使用这个模块,并在你的模板中给它一个分页控制标签(我在 app.component.html 中使用它)

<table>
  <tr *ngFor="let x of names | paginate: { itemsPerPage: 3, currentPage: page }">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>
<pagination-controls (pageChange)="page = $event"></pagination-controls>

我希望这可以帮到你。

于 2018-12-17T14:47:49.650 回答
0

我按照以下步骤解决了这个问题(Windows 操作系统用户):

  1. 使用 VS Code 作为管理员模式打开您的 Angular 项目。
  2. 在终端中运行npm install并等待它完成。
  3. 关闭VS 代码。
  4. 使用 VS Code 作为管理员模式重新打开您的 Angular 项目。

这个问题应该得到解决。

于 2021-07-21T02:43:06.513 回答