导入节点模块时出现XHR错误404 - Angular-2

时间:2017-07-01 15:55:50

标签: angular typescript angular2-template typescript-typings

我的目录结构如下:

dir
   |--app.component.ts

  node_modules
  |--angular2-bootstrap-switch

我按照https://www.npmjs.com/package/angular2-bootstrap-switch添加angular2-boostrap开关,按照说明,我不得不这样做,但我不确定如何。

的index.html:

<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>


<!-- 2. Configure SystemJS -->
<script>
  System.config({
    packages: {        
      app: {
        format: 'register',
        defaultExtension: 'js'
      }

    }
  });


     System.import('app/boot')
            .then(null, console.error.bind(console));
    </script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

Inside boot.ts

 import {bootstrap}    from 'angular2/platform/browser'
import {AppComponent} from './app.component'

bootstrap(AppComponent);

IN tsconfig.json

 "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]

现在我必须在app.component.ts中导入../node_modules/angular2_bootstrap_switch/components,例如:

import {SwitchComponent} from angular2_bootstrap_switch/components

我不确定如何。请帮忙。

1 个答案:

答案 0 :(得分:1)

使用它非常简单。

  1. 安装node_module

    npm i angular2-bootstrap-switch
    
  2. 在您的系统中,Js将配置添加为

    'angular2-bootstrap-switch':'npm:angular2-bootstrap-switch/lib/switch.component.js'
    
  3. 在您的应用模块中,将开关组件导入为

    import {SwitchComponent} from 'angular2-bootstrap-switch'
    
  4. 将其添加到声明如下,

    declarations: [ SwitchComponent ],
    
  5. 在HTML中使用

    <switch [status]="value" [onText]="onText" [offText]="offText" 
            [onColor]="onColor" [offColor]="offColor" [size]="size">
    </switch>
    
  6. LIVE DEMO

相关问题