小编noo*_*mer的帖子

如何在angular2中调用另一个组件函数

我有两个组件如下,我想从另一个组件调用一个函数.这两个组件都包含在第三个父组件using指令中.

第1部分:

@component(
    selector:'com1'
)
export class com1{
    function1(){...}
}
Run Code Online (Sandbox Code Playgroud)

第2部分:

@component(
    selector:'com2'
)
export class com2{
    function2(){...
        // i want to call function 1 from com1 here
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用@input@output,但我不明白究竟是如何使用它,以及如何调用该函数,任何人都可以帮忙吗?

angular2-components angular

130
推荐指数
6
解决办法
23万
查看次数

服务实例不适用于angular2中的子组件

我已经创建了一些应该在整个应用程序中共享的服务但由于某种原因,子组件抛出错误 Error: DI Exception at NoProviderError.BaseException [as constructor] 我提供了引导文件中的所有服务

bootstrap(AppComponent, [
    JwtHelper,
    HTTP_PROVIDERS, authervice, ROUTER_PROVIDERS, 
    categoryService,
    Configuration
]);
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议吗?如果您需要更多解释或代码,请告诉我.

类别组件

import ...
import {categoryService}       from './categoryService';
@Component({
    selector: 'category-list',
    template: `...`,
    directives: [],
    styles: ['.error {color:red;}'],
    providers: []

})
export class categoryComponent implements OnInit {
    constructor(private _categoryService: categoryService) { }
    ...
}
Run Code Online (Sandbox Code Playgroud)

如果我在上面的提供程序中添加categoryservice,否则会抛出错误

完全错误

EXCEPTION:错误:未捕获(在承诺中):EXCEPTION:错误:0:0原始异常:没有categoryService的提供者!ORIGINAL堆栈跟踪:错误:在NoProviderError.BaseException DI异常[作为构造](HTTP://本地主机:58056/libs/@angular/core/src/facade/exceptions.js:17:23)在NoProviderError.AbstractProviderError [作为构造](http:// localhost:58056/libs/@angular/core/src/di/reflective_exceptions.js:39:16)新的NoProviderError(http:// localhost:58056/libs/@ angular/core/src/di/reflective_exceptions.js:75:16)在ReflectiveInjector _._ throwOrNull(http:// localhost:58056 / libs / @ angular / core / src / …

angular

4
推荐指数
1
解决办法
3841
查看次数

标签 统计

angular ×2

angular2-components ×1