天天看點

angular5報錯集合

以下是個人曾經在angular中遇到各種報錯問題,在此整理以下,僅供參考

報錯一:

The Angular Compiler requires TypeScript >=3.1.1 and < 3.3.0 but 2.9.2 was found instead.

原因:Angular Compiler需要Typescript 在3.1.1至3.3.0之間,但是目前安裝了2.9.2版本

angular5報錯集合

報錯解決:進行更新

安裝TypeScript版本3.1.1至3.3.0之間即可,具體指令為:

$ npm i typescript@">-3.1.1 <3.3.0"
           

報錯二:

Cannot read property ‘getContext’ of null

原因:是因為dom元素還沒加載好,就調用了導緻的。

angular5報錯集合

以上錯誤是我在angular5項目中這麼使用代碼導緻。請看代碼。

在canvas-text.component.html中,給canvas元素id值。

<canvas id="canvas" style="border:1px solid red" [width]="canvasObj.canvasWith" [height]="canvasObj.canvasHeight">浏覽器過低不支援canvas顯示</canvas>
           

在canvas-text.component.ts中,我當時直接使用 document.getElementById(‘canvas’)擷取的。但是由于頁面dom元素未加載完就擷取,自然報錯。

this.canvasId = document.getElementById('canvas');
this.canvasPen = this.canvasId.getContext("2d");
this.canvasEvent(this.canvasId);
           

解決方法:

在canvas-text.component.html中,給canvas元素起一個引用名字#myCanvas,便于查找該元素。

<canvas id="canvas" #myCanvas style="border:1px solid red" [width]="canvasObj.canvasWith" [height]="canvasObj.canvasHeight">浏覽器過低不支援canvas顯示</canvas>
           

在canvas-text.component.ts中,導入Renderer2, ElementRef, ViewChild,其中@viewChild是用來查找引用dom元素的,與document.getElementById(‘canvas’)同樣道理。

import { Component, OnInit, Renderer2, ElementRef, ViewChild } from '@angular/core';
@ViewChild('myCanvas') myCanvas:ElementRef;

drawCanvas(){
this.canvasId=this.myCanvas.nativeElement;
this.canvasPen=this.canvasId.getContext("2d");
this.canvasEvent(this.canvsId);
}

           

報錯三:

No value accessor for form control with unspecified name attribute

原因:這個是因為 [(ngModel)]是雙向綁定,[(ngModel)]屬性是綁定在input上的,若綁定其他元素如div,span等就會出現上面錯誤。

angular5報錯集合

解決辦法就是在span标簽内同時加個ngDefaultControl就行。

angular5報錯集合

報錯四:

angular報錯:Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’

angular5報錯集合

解決:

在 app.module.ts中 導入 FormsModule 。

import { FormsModule } from '@angular/forms'; 
           

在app.module.ts 把 FormsModule 添加到 @NgModule 中繼資料的 imports 數組中。

imports: [
BrowserModule,
FormsModule],
           

報錯五

angular報錯 Data path “.builders[‘app-shell’]” should have required property ‘class’。

原因不明,個人猜測可能是項目代碼中的package.json的devDependencies中是@angular-devkit/build-angular": ^0.800.3,但我本地環境是"@angular-devkit/build-angular": "~0.13.0不一緻導緻的。

這是項目代碼的package.json的devDependencies中

"@angular-devkit/build-angular": "^0.800.3",

           

本地環境是"@angular-devkit/build-angular": “~0.13.0

angular5報錯集合

解決:

“@angular-devkit/build-angular”: “^0.800.3”,修改為”@angular-devkit/build-angular": “~0.13.0”,

報錯:

Invalid configuration of route ‘’. One of the following must be provided: component, redirectTo, children or loadChildren

原因:路由沒有配置好,配置好路由就可以解決。

當然,此處一定注意,當年你在app.module.ts跟子產品中導入多個子產品比如meetModule,userModule等,一定檢測查每個子產品路由是否都配置好。注意此處多個子產品放置順序問題。

因為我是因為meetmodule子產品(沒配好路由)放置在usermodule(已經配好路由)之前導緻的我怎麼配usermodule子產品路由都報錯此錯,明明usermodule沒配錯,但就報這個錯,後面發現是我前一個子產品meetmodule忘記配路由了。

angular5報錯集合

報錯六:

No provider for ChildrenOutletContexts!

angular5報錯集合

解決:

在app.module.ts添加

RouterModule.forRoot(//沒添加導緻的錯誤

routes,//路由配置

{ enableTracing: true }

)

如下:

const routes11: Routes = [
  {
    path: 'home',
     component:HomeComponent
]
@NgModule({
  declarations: [
    AppComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    ClientModule,
   RouterModule.forRoot(//沒添加導緻的錯誤
    routes,//路由配置
    { enableTracing: true } 
  )
  ],
  .............
  })
           

報錯七:

報錯Error: Angular JIT compilation failed: ‘@angular/compiler’ not loaded!

解決:

在Main.ts檔案中導入@angular/compiler

import '@angular/compiler';
           

angular.json檔案中,把“aot”:true 改為false即可。