【问题标题】:AngularJS 2 call outside function in OnInit()AngularJS 2 在 OnInit() 中调用外部函数
【发布时间】:2016-10-29 07:55:44
【问题描述】:

我们在Asp.Net Core 中创建AngularJS 2 应用程序。并且使用的模板不支持AngularJS 2。问题是 Layout.Init() 函数未初始化。我尝试在AngularJS 2 组件的OnInit() 事件中调用Layout.Init() 函数。然后我得到错误:

我的代码:

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'ds-left-sidebar',
    template: require('./ds-left-sidebar.component.html')
})
export class LeftSidebarComponent implements OnInit {    
    ngOnInit() {
        Layout.init();
    }
}

有什么建议吗?感谢您的回答。

【问题讨论】:

  • 这里的布局是什么?
  • @micronyks,布局是javascriptlayout.js中的函数

标签: c# angular asp.net-core


【解决方案1】:

在您的代码中添加如下声明:

import { Component, OnInit } from '@angular/core';
declare Layout:any;

@Component({
    selector: 'ds-left-sidebar',
    template: require('./ds-left-sidebar.component.html')
})
export class LeftSidebarComponent implements OnInit {    
    ngOnInit() {
        Layout.init();
    }
}

声明的Layout 将使用layout.js 中的Layout 对象。

希望对你有帮助

【讨论】:

  • 我写的是declare function Layout:any;,在这种情况下,Layout.Init() 出现错误。 Init() 未定义。
  • 不要使用“function”这个词,只使用“declare Layout:any;”
猜你喜欢
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多