3

我想用對話從PrimeNG模塊,但我總是得到錯誤:Angular2:PrimeNG - XHR錯誤(404未找到)

Unhandled Promise rejection: (SystemJS) Error: XHR error (404 Not Found) loading http://localhost:4200/node_modules/primeng/primeng.js 

我使用的設置配置從http://www.primefaces.org/primeng/#/setup 但一切,我已經嘗試過,不會工作。 我正在使用Angular2 RC4。

系統config.ts

/** Map relative paths to URLs. */ 
const map: any = { 
    'firebase': 'vendor/firebase/firebase.js', 
    'angularfire2': 'vendor/angularfire2', 
    'primeng': 'node_modules/primeng' 
}; 

/** User packages configuration. */ 
const packages: any = { 
    angularfire2: { 
    defaultExtension: 'js', 
    main: 'angularfire2.js' 
    }, 
    'primeng': { defaultExtension: 'js' } 
    }; 

//////////////////////////////////////////////////////////////////////////////////////////////// 
/*********************************************************************************************** 
* Everything underneath this line is managed by the CLI. 
**********************************************************************************************/ 
const barrels: string[] = [ 
    // Angular specific barrels. 
    '@angular/core', 
    '@angular/common', 
    '@angular/compiler', 
    '@angular/forms', 
    '@angular/http', 
    '@angular/router', 
    '@angular/platform-browser', 
    '@angular/platform-browser-dynamic', 

    // Thirdparty barrels. 
    'rxjs', 

    // App specific barrels. 
    'app', 
    'app/shared', 
    /** @cli-barrel */ 
]; 

const cliSystemConfigPackages: any = {}; 
barrels.forEach((barrelName: string) => { 
    cliSystemConfigPackages[barrelName] = { main: 'index' }; 
}); 

/** Type declaration for ambient System. */ 
declare var System: any; 

// Apply the CLI SystemJS configuration. 
System.config({ 
    map: { 
    '@angular': 'vendor/@angular', 
    'rxjs': 'vendor/rxjs', 
    'main': 'main.js' 
    }, 
    packages: cliSystemConfigPackages 
}); 

// Apply the user's configuration. 
System.config({ map, packages }); 

角-CLI-build.js

/* global require, module */ 

var Angular2App = require('angular-cli/lib/broccoli/angular2-app'); 

module.exports = function(defaults) { 
    return new Angular2App(defaults, { 
    vendorNpmFiles: [ 
     'systemjs/dist/system-polyfills.js', 
     'systemjs/dist/system.src.js', 
     'zone.js/dist/**/*.+(js|js.map)', 
     'es6-shim/es6-shim.js', 
     'reflect-metadata/**/*.+(ts|js|js.map)', 
     'rxjs/**/*.+(js|js.map)', 
     '@angular/**/*.+(js|js.map)', 
     'angularfire2/**/*.js', 
     'firebase/*.js', 
     'primeng/*.js' 
    ] 
    }); 
}; 

位置component.ts

import {Component} from "@angular/core"; 
import {LocationGridComponent} from "./location-grid/location-grid.component"; 
import {LocationDetailComponent} from "./location-detail/location-detail.component"; 
import {Dialog} from "primeng/primeng"; 


@Component({ 
    selector: 'app-location', 
    templateUrl: 'app/location/location.component.html', 
    styleUrls: ['app/orienteering/orienteering.component.css', 'app/location/location.component.css'], 
    directives: [LocationGridComponent, LocationDetailComponent, Dialog] 
}) 

export class LocationComponent { 

    display: boolean = false; 

    showDialog() { 
    this.display = true; 
    } 

} 

的package.json

{ 
    "name": "digitaler-lerngarten", 
    "version": "0.0.0", 
    "license": "MIT", 
    "angular-cli": {}, 
    "scripts": { 
    "start": "ng serve", 
    "postinstall": "typings install", 
    "lint": "tslint \"src/**/*.ts\"", 
    "test": "ng test", 
    "pree2e": "webdriver-manager update", 
    "e2e": "protractor" 
    }, 
    "private": true, 
    "dependencies": { 
    "@angular/common": "^2.0.0-rc.4", 
    "@angular/compiler": "^2.0.0-rc.4", 
    "@angular/core": "^2.0.0-rc.4", 
    "@angular/forms": "^0.2.0", 
    "@angular/http": "^2.0.0-rc.4", 
    "@angular/platform-browser": "^2.0.0-rc.4", 
    "@angular/platform-browser-dynamic": "^2.0.0-rc.4", 
    "@angular/router": "^3.0.0-beta.2", 
    "angular-cli": "^1.0.0-beta.9", 
    "codelyzer": "0.0.25", 
    "es6-shim": "^0.35.1", 
    "karma": "^1.1.1", 
    "karma-chrome-launcher": "^1.0.1", 
    "karma-jasmine": "^1.0.2", 
    "latest-version": "^2.0.0", 
    "primeng": "^1.0.0-beta.9", 
    "primeui": "^4.1.12", 
    "reflect-metadata": "^0.1.3", 
    "rxjs": "5.0.0-beta.6", 
    "systemjs": "^0.19.31", 
    "ts-node": "^0.9.3", 
    "tslint": "^3.13.0", 
    "typings": "^1.3.1", 
    "zone.js": "^0.6.12" 
    }, 
    "devDependencies": { 
    "angular-cli": "^1.0.0-beta.9", 
    "codelyzer": "^0.0.25", 
    "ember-cli-inject-live-reload": "^1.4.0", 
    "jasmine-core": "^2.4.1", 
    "jasmine-spec-reporter": "^2.5.0", 
    "karma": "^1.1.1", 
    "karma-chrome-launcher": "^1.0.1", 
    "karma-jasmine": "^1.0.2", 
    "protractor": "^3.3.0", 
    "ts-node": "^0.9.3", 
    "tslint": "^3.11.0", 
    "typescript": "^1.8.10", 
    "typings": "^1.3.1" 
    } 
} 
+0

首先嚐試降級到'angular-rc3',因爲'primeng'與rc4不兼容。原因是角度的rc版本有突破性的變化...這就是rc對他們的定義:/。請添加組件的'html'。 – Sergio

+1

我已降級到RC3,現在它可以工作。謝謝! – Alpha0

回答

1

我已經降級爲Angular2 RC3,而現在它的工作原理。 我必須將system-config.ts中的'node_modules/primeng'更改爲'vendor/primeng'。 然後我在angular-cli-build.js中添加primeng-files,然後用「ng build」重建項目。

系統config.ts

// SystemJS configuration file, see links for more information 
// https://github.com/systemjs/systemjs 
// https://github.com/systemjs/systemjs/blob/master/docs/config-api.md 

/*********************************************************************************************** 
* User Configuration. 
**********************************************************************************************/ 
/** Map relative paths to URLs. */ 
const map: any = { 
    'firebase': 'vendor/firebase/firebase.js', 
    'angularfire2': 'vendor/angularfire2', 
    'primeng': 'vendor/primeng' 
}; 

/** User packages configuration. */ 
const packages: any = { 
    angularfire2: { 
     defaultExtension: 'js', 
     main: 'angularfire2.js' 
    }, 
    primeng: { defaultExtension: 'js' } 
}; 

//////////////////////////////////////////////////////////////////////////////////////////////// 
/*********************************************************************************************** 
* Everything underneath this line is managed by the CLI. 
**********************************************************************************************/ 
const barrels: string[] = [ 
    // Angular specific barrels. 
    '@angular/core', 
    '@angular/common', 
    '@angular/compiler', 
    '@angular/forms', 
    '@angular/http', 
    '@angular/router', 
    '@angular/platform-browser', 
    '@angular/platform-browser-dynamic', 

    // Thirdparty barrels. 
    'rxjs', 

    // App specific barrels. 
    'app', 
    'app/shared', 
    /** @cli-barrel */ 
]; 

const cliSystemConfigPackages: any = {}; 
barrels.forEach((barrelName: string) => { 
    cliSystemConfigPackages[barrelName] = { main: 'index' }; 
}); 

/** Type declaration for ambient System. */ 
declare var System: any; 

// Apply the CLI SystemJS configuration. 
System.config({ 
    map: { 
    '@angular': 'vendor/@angular', 
    'rxjs': 'vendor/rxjs', 
    'main': 'main.js' 
    }, 
    packages: cliSystemConfigPackages 
}); 

// Apply the user's configuration. 
System.config({ map, packages }); 

角-CLI-build.js

// Angular-CLI build configuration 
// This file lists all the node_modules files that will be used in a build 
// Also see https://github.com/angular/angular-cli/wiki/3rd-party-libs 

/* global require, module */ 

var Angular2App = require('angular-cli/lib/broccoli/angular2-app'); 

module.exports = function(defaults) { 
    return new Angular2App(defaults, { 
    vendorNpmFiles: [ 
     'systemjs/dist/system-polyfills.js', 
     'systemjs/dist/system.src.js', 
     'zone.js/dist/**/*.+(js|js.map)', 
     'es6-shim/es6-shim.js', 
     'reflect-metadata/**/*.+(ts|js|js.map)', 
     'rxjs/**/*.+(js|js.map)', 
     '@angular/**/*.+(js|js.map)', 
     'angularfire2/**/*.js', 
     'firebase/*.js', 
     'primeng/**/*.js', 
     'primeui/**/*.css' 
    ] 
    }); 
}; 

的package.json

{ 
    "name": "digitaler-lerngaraten", 
    "version": "0.0.0", 
    "license": "MIT", 
    "angular-cli": {}, 
    "scripts": { 
    "start": "ng serve", 
    "postinstall": "typings install", 
    "lint": "tslint \"src/**/*.ts\"", 
    "test": "ng test", 
    "pree2e": "webdriver-manager update", 
    "e2e": "protractor" 
    }, 
    "private": true, 
    "dependencies": { 
    "@angular/common": "2.0.0-rc.3", 
    "@angular/compiler": "2.0.0-rc.3", 
    "@angular/core": "2.0.0-rc.3", 
    "@angular/forms": "0.2.0", 
    "@angular/http": "2.0.0-rc.3", 
    "@angular/platform-browser": "2.0.0-rc.3", 
    "@angular/platform-browser-dynamic": "2.0.0-rc.3", 
    "@angular/router": "3.0.0-alpha.8", 
    "angularfire2": "^2.0.0-beta.2", 
    "es6-shim": "0.35.1", 
    "firebase": "^3.2.0", 
    "primeng": "^1.0.0-beta.9", 
    "primeui": "^4.1.12", 
    "reflect-metadata": "0.1.3", 
    "rxjs": "5.0.0-beta.6", 
    "systemjs": "0.19.26", 
    "zone.js": "0.6.12" 
    }, 
    "devDependencies": { 
    "angular-cli": "1.0.0-beta.9", 
    "codelyzer": "0.0.20", 
    "ember-cli-inject-live-reload": "1.4.0", 
    "jasmine-core": "2.4.1", 
    "jasmine-spec-reporter": "2.5.0", 
    "karma": "0.13.22", 
    "karma-chrome-launcher": "0.2.3", 
    "karma-jasmine": "0.3.8", 
    "protractor": "3.3.0", 
    "ts-node": "0.5.5", 
    "tslint": "3.11.0", 
    "typescript": "1.8.10", 
    "typings": "0.8.1" 
    } 
} 
0

Basically follow the examples on how to install 3rd party libraries

  1. 通過NPM
  2. 可選安裝!安裝類型庫
  3. 添加將angular-cli-build.js文件傳遞給vendorNpmFiles數組(非常重要)。它告訴在哪裏找到文件,這些文件將轉到供應商文件夾內的dist文件夾。
  4. 配置SystemJS映射知道去哪裏找(其中所有的文件將在DIST文件夾)。
1

嘗試一次。

System.config({  
     packages: {   
      app: { 
      format: 'register', 
      defaultExtension: 'js' 
      }, 
      'node_modules/primeng': { 
      format: 'cjs', 
      defaultExtension: 'js' 
      } 
     }, 
     map: { 
      "primeng": "node_modules/primeng" 
     } 
     }); 
     System.import('app/js/components/main') 
      .then(null, console.error.bind(console)); 
相關問題