Angular-cli + primeng require 未定义 [英] Angular-cli + primeng require is not defined
问题描述
我正在使用 angular-cli@1.0.0-beta.14 (webpack) 和 primeng@1.0.0-beta.15.
I'm using angular-cli@1.0.0-beta.14 (webpack) and primeng@1.0.0-beta.15.
在创建一个新的 angular-cli 项目后,我做了一些更改以添加 Primeng:
After create a fresh angular-cli project i made a few changes to add primeng:
1 在 package.json 上:
1 on package.json:
"primeng": "^1.0.0-beta.15"
angular-cli.json 上的 2:
2 on angular-cli.json:
"styles": [ "styles.css", "../node_modules/primeng/resources/themes/omega/theme.css", "../node_modules/font-awesome/css/font-awesome.min.css", "../node_modules/primeng/resources/primeng.min.css" ],
"scripts": [ "../node_modules/primeng/primeng.js" ]
3 在 app.module.ts 上:
3 on app.module.ts:
@NgModule({ declarations: [ AppComponent ],
imports: [ BrowserModule, FormsModule, HttpModule, PanelModule ],
providers: [], bootstrap: [AppComponent] }) export class AppModule { }
问题:
Uncaught ReferenceError: require is not defined Unexpected value'undefined' 由模块 'AppModule' 导入
Uncaught ReferenceError: require is not defined Unexpected value 'undefined' imported by the module 'AppModule'
任何帮助将primeng添加到angular-cli...我们会很棒!:)
Any help to add primeng to angular-cli... will we great! :)
推荐答案
所以几乎你所拥有的一切看起来都不错.
So just about everything you have there looks good.
问题在于您试图将 primeng 作为脚本标签包含在内(这适用于像 jQuery 这样的全局库).但是由于primeng部署模块,你可以简单地将它导入你的模块,然后像这样将它提供给你的NgModule:
The issue lies in that you're attempting to include primeng as a script tag (which is for global libraries like jQuery). But since primeng deploys modules you can simply import it in your module and then supply that to your NgModule like this:
import { ButtonModule } from 'primeng/components/button/button';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
ButtonModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
然后你还想从你的 angular-cli.json 中删除对 primeng.js 的引用
And then you'll also want to remove the reference to primeng.js from your angular-cli.json
这篇关于Angular-cli + primeng require 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!