javascript - angular2 提示No provider for Array!
伊谢尔伦
伊谢尔伦 2017-04-11 12:37:04
[JavaScript讨论组]

我现在在做一个添加course页面,此时我在component.ts ,采用了template来写,
可以实现,但是当我尝试新建一个service.ts,发现提示一堆错误,错误说没有provider,但是我已经写了,貌似是service那里没有返回成功。剩下三幅图分别显示course.html,courese.component.还有service.ts.新手求指导
如图所示

html代码

{{title}}

  • {{course}}

component.ts部分代码

import { Component, OnInit } from '@angular/core';
import {CoursesService} from '../courses.service';
import {AutoGrowDirective} from '../auto-grow.directive';
@Component({
  selector: 'app-courses',
  templateUrl: './courses.component.html',
  styleUrls: ['./courses.component.css'],
  providers:[CoursesService]
})

export class CoursesComponent implements OnInit {
  title="The title of courses page";
  //courses;
  courseList;
  //courseName;
  constructor( CoursesService:CoursesService) { 
     this.courseList=CoursesService.getCourses();
     //this.courseList=CoursesService.savecourse();
    // this.courseName=CoursesService.onKey();
  }

  ngOnInit() {
  }

}

service.ts

import { Injectable } from '@angular/core';

@Injectable()
export class CoursesService {
  courseName:string;
  courseList:string[] = ["Course1","Course2","Course3"];
  constructor(courseList:string[]) { 
      this.courseList = courseList.concat(this.courseList);
  }
   getCourses():string[] {
      return this.courseList;
  }
  savecourse(courseName){
     //alert(this.courseName);
     this.courseList.push(courseName);
     //console.log(courseNameInput);
    
  }
  onKey(e){
    this.courseName += e.key;
    console.log(this.courseName);
  }
}
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(1)
黄舟

因为你的Service是一个带有参数的构造方法,所以DI无法确认值应该多少,为了让DI正常实例化,可以加上 @Optional() 来表示参数可选项。

constructor(@Optional() courseList:string[]) {
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号