简体   繁体   English

Angular2快速入门:我应该如何包括新的npm软件包?

[英]Angular2 quickstart: how should I include a new npm package?

I setup the environment by cloning the angular2 quick start repo: https://github.com/angular/quickstart . 我通过克隆angular2快速入门仓库来设置环境: https : //github.com/angular/quickstart After I added new package into system.config.js file, the app runs but "npm test" failed. 将新包添加到system.config.js文件后,该应用程序运行,但“ npm测试”失败。 I noticed that there is an empty file named systemjs.config.extras.js, should I add some code there? 我注意到有一个名为systemjs.config.extras.js的空文件,是否应该在其中添加一些代码?

Question: how should add a new npm package to angular2 quick start example so that both "npm start" and "npm test" can pass? 问题:应如何在angular2快速入门示例中添加新的npm软件包,以便“ npm start”和“ npm test”都能通过?

After I added new package into system.config.js file, the app runs but "npm test" failed 将新包添加到system.config.js文件后,该应用程序运行,但“ npm测试”失败

I'm assuming you are using Karma. 我假设您正在使用Karma。 What you need to do is add that third-party lib to the files array in the karma.conf.js file. 您需要做的是将该第三方库添加到karma.conf.js文件中的files数组中。

How it works is that karma starts a server, and adds all the files listed in the files array, to the list of files that the server is available to serve. 它的工作原理是,因果报应启动服务器,并将files数组中列出的所有文件添加到该服务器可提供的文件列表中。 So when SystemJS tries to load the file, if it is not in the karma files , then the file doesn't exist on the server. 因此,当SystemJS尝试加载文件时,如果它不在karma files ,则该文件在服务器上不存在。 So you will get a 404 when trying to retrieve that file from SystemJS. 因此,当您尝试从SystemJS检索该文件时,将得到404。

You should try angular-cli which is the recommended way to work with angular2. 您应该尝试angular-cli,这是使用angular2的推荐方法。 To create a new project you have to install angular-cli with the command 要创建一个新项目,您必须使用以下命令安装angular-cli

npm install -g angular-cli

then you can create a new angular2 project with 然后您可以使用创建一个新的angular2项目

ng new your-new-project-name

and finally to add a new global dependency by following this guide ( https://github.com/angular/angular-cli#global-library-installation ). 最后,按照本指南( https://github.com/angular/angular-cli#global-library-installation )添加新的全局依赖项。 I reproduce the content here for bootstrap. 我在这里复制内容以进行引导。

npm install bootstrap@next

Then add the needed script files to apps[0].script s in agular-cli.json file: 然后将所需的脚本文件添加到agular-cli.json文件中的apps[0].script s中:

"scripts": [
  "../node_modules/jquery/dist/jquery.js",
  "../node_modules/tether/dist/js/tether.js",
  "../node_modules/bootstrap/dist/js/bootstrap.js"
],

Finally add the Bootstrap CSS to the apps[0].styles array in agular-cli.json : 最后,将Bootstrap CSS添加到agular-cli.jsonapps[0].styles数组中:

"styles": [
  "../node_modules/bootstrap/dist/css/bootstrap.css",
  "styles.css"
],

Finally you can deploy your application using ng serve from the root of the newly created project. 最后,您可以从新创建的项目的根目录使用ng serve部署应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM