Angular 4 - Bootstrap Modal抛出异常

时间:2017-11-14 15:39:45

标签: angular angular-ui-bootstrap bootstrap-modal

我得到'Uncaught(承诺):错误:没有ComponentLoaderFactory的提供者!'尝试访问导入Bootstrap ModalModule的组件时出错。

app.module.ts:

import { ModalModule } from 'ngx-bootstrap/modal';

@NgModule({
    imports: [BrowserModule, RouterModule, ModalModule.forRoot()],
...

template.component.ts(发生错误的地方):

import {  TemplateRef } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';

@Component({
    selector: 'templateController',
    templateUrl: './templateController.component.html',
    providers: [BsModalService]
})

export class TemplateControllerComponent implements OnInit {

    modalRef: BsModalRef;

    constructor(private modalService: BsModalService) {

这里我提供了ngx-bootstrap / modal的路径,没有它,它无法找到所需的js文件并抛出404。 systemjs.config.js:

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'node_modules/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            'app': '/src/app',

            // angular bundles
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
            'tslib': 'npm:tslib/tslib.js',
            '@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.js',

            // other libraries
            'rxjs': 'npm:rxjs',
            'moment': 'node_modules/moment/moment.js',
            'ngx-bootstrap': 'node_modules/ngx-bootstrap/bundles/ngx-bootstrap.umd.js',
            'ngx-bootstrap/modal': 'node_modules/ngx-bootstrap/bundles/ngx-bootstrap.umd.js'

        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                defaultExtension: 'js',
                meta: {
                    './*.js': {
                        loader: 'src/systemjs-angular-loader.js'
                    }
                }
            },
            rxjs: {
                defaultExtension: 'js'
            }
            //,
            //"ngx-bootstrap/modal": {
            //    "defaultExtension": "js"
            //}

        }
    });
})(this);

1 个答案:

答案 0 :(得分:0)

尝试在模块导入中添加forRoot()(来自ngx-bootstrap的模块)。

如果它不起作用,那么你需要提供入口组件。

在app.module中:

在模块文件中需要@NgModule()中的entryComponent字段,因为您正在动态加载Bootstrap组件。在entryComponents数组中添加引导程序组件,如下所示:

@NgModule({
declaration: [],
imports: [],
  entryComponents: [ModalComponent,..]
})