' nvd3'不是一个已知的元素:1。如果' nvd3'是一个Angular组件,然后验证它是否是此模块的一部分

时间:2018-03-26 11:50:19

标签: angular d3.js nvd3.js ng2-nvd3

我是角度5实际上是全角2概念的新手。 我在visual studio 2017中创建了具有角度5和.net核心的新项目。 我正在尝试使用d3和ng2-nvd3。我在npm中添加了包。我尝试运行页面时遇到错误。

以下是错误。

NodeInvocationException: Template parse errors: Can't bind to 'options' since it isn't a known property of 'nvd3'. ("<div>
 <nvd3 [ERROR ->][options]="options" [data]="data"></nvd3>
 </div>
 "): ng:///AppModuleShared/HomeComponent.html@1:10 Can't bind to 'data' since it isn't a known property of 'nvd3'. ("<div>
 <nvd3 [options]="options" [ERROR ->][data]="data"></nvd3>
 </div>
 "): ng:///AppModuleShared/HomeComponent.html@1:30 'nvd3' is not a known element: 1. If 'nvd3' is an Angular component, then verify that it is part of this module. 2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("<div>
 [ERROR ->]<nvd3 [options]="options" [data]="data"></nvd3>
 </div>
 ")

我的Package.json是

{
  "name": "HaloManagement.DashBoard",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js"
  },
  "devDependencies": {
    "@angular/animations": "5.2.0",
    "@angular/common": "5.2.0",
    "@angular/compiler": "5.2.0",
    "@angular/compiler-cli": "5.2.0",
    "@angular/core": "5.2.0",
    "@angular/forms": "5.2.0",
    "@angular/http": "5.2.0",
    "@angular/platform-browser": "5.2.0",
    "@angular/platform-browser-dynamic": "5.2.0",
    "@angular/platform-server": "5.2.0",
    "@angular/router": "5.2.0",
    "@ngtools/webpack": "1.5.0",
    "@types/chai": "4.0.1",
    "@types/jasmine": "2.5.53",
    "@types/webpack-env": "1.13.0",
    "angular2-router-loader": "0.3.5",
    "angular2-template-loader": "0.6.2",
    "aspnet-prerendering": "^3.0.1",
    "aspnet-webpack": "^2.0.1",
    "awesome-typescript-loader": "3.2.1",
    "bootstrap": "3.3.7",
    "chai": "4.0.2",
    "css": "2.2.1",
    "css-loader": "0.28.4",
    "d3": "^3.5.17",
    "nvd3": "^1.8.6",
    "es6-shim": "0.35.3",
    "event-source-polyfill": "0.0.9",
    "expose-loader": "0.7.3",
    "extract-text-webpack-plugin": "2.1.2",
    "file-loader": "0.11.2",
    "html-loader": "0.4.5",
    "isomorphic-fetch": "2.2.1",
    "jasmine-core": "2.6.4",
    "jquery": "3.2.1",
    "json-loader": "0.5.4",
    "karma": "1.7.0",
    "karma-chai": "0.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-webpack": "2.0.3",
    "ng2-nvd3": "^2.0.0",
    "preboot": "4.5.2",
    "raw-loader": "0.5.1",
    "reflect-metadata": "0.1.10",
    "rxjs": "5.4.2",
    "style-loader": "0.18.2",
    "to-string-loader": "1.1.5",
    "typescript": "2.4.1",
    "url-loader": "0.5.9",
    "webpack": "2.5.1",
    "webpack-hot-middleware": "2.18.2",
    "webpack-merge": "4.1.0",
    "zone.js": "0.8.12"
  },
  "dependencies": {
    "nvd3": "^1.8.6"
  }
}

App.shared.module.ts如下所示。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';

import { AppComponent } from './components/app/app.component';
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { CounterComponent } from './components/counter/counter.component';

import { NvD3Module } from "ng2-nvd3";

// d3 and nvd3 should be included somewhere
import 'd3';
import 'nvd3';

@NgModule({
    declarations: [
        AppComponent,
        NavMenuComponent,
        CounterComponent,
        FetchDataComponent,
        NvD3Module,
        HomeComponent
    ],
    imports: [
        CommonModule,
        HttpModule,
        FormsModule,
        RouterModule.forRoot([
            { path: '', redirectTo: 'home', pathMatch: 'full' },
            { path: 'home', component: HomeComponent },
            { path: 'counter', component: CounterComponent },
            { path: 'fetch-data', component: FetchDataComponent },
            { path: '**', redirectTo: 'home' }
        ])
    ]
})
export class AppModuleShared {
}
</pre>

home.component.ts如下图所示我试图添加图表

import { Component, OnInit } from '@angular/core';
import { NvD3Module } from "ng2-nvd3";

import 'd3';
import 'nvd3';

@Component({
    selector: 'home',
    templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {

    ngOnInit(): void {
    }

    options: any;
    data: any;

    constructor() {
        this.options = {
            chart: {
                type: 'pieChart',
                height: 500,
                x: function (d) {
                    return d.key;
                },
                y: function (d) {
                    return d.y;
                },
                showLabels: true,
                duration: 500,
                labelThreshold: 0.01,
                labelSunbeamLayout: true,
                legend: {
                    margin: {
                        top: 5,
                        right: 35,
                        bottom: 5,
                        left: 0
                    }
                }
            }
        };

        this.data = [
            {
                key: "P60-1",
                y: 500
            },
            {
                key: "P60-2",
                y: 445
            },
            {
                key: "P40",
                y: 225
            },
            {
                key: "P73",
                y: 127
            },
            {
                key: "P71",
                y: 128
            }
        ];

    }
}

App.server.module.ts如下所示

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModuleShared } from './app.shared.module';
import { AppComponent } from './components/app/app.component';

@NgModule({
    bootstrap: [ AppComponent ],
    imports: [
        ServerModule,
        AppModuleShared
    ]
})
export class AppModule {
}

和home.component.html看起来像这样

<div>
    <nvd3 [options]="options" [data]="data"></nvd3>
</div>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要将NvD3Module添加到导入列表而不是声明。

@NgModule({
  imports: [ // import Angular's modules 
    ...
    NvD3Module
    ...
  ],
})