![](/img/trans.png)
[英]Can i use a javascript inline edit library in my angular 2 project?
[英]Import a javascript library into a custom javascript file which I use in my Angular2 project
我想通过JavaScript的“ synaptic.js”库将机器学习包含在Angular2项目中。 我已经通过命令安装了库
npm install synaptic --save
我想运行以下自定义javascript文件( myJsFile.js ):
function myFunction() {
var A = new Layer(5);
var B = new Layer(2);
A.project(B);
var learningRate = .3;
for (var i = 0; i < 20000; i++)
{
// when A activates [1, 0, 1, 0, 1]
A.activate([1,0,1,0,1]);
// train B to activate [0,0]
B.activate();
B.propagate(learningRate, [0,0]);
}
// test it
A.activate([1,0,1,0,1]);
console.log(B.activate()); // [0.004606949693864496,0.004606763721459169]
}
在我的index.html文件中,包含以下代码:
<script src="js/myJsFile.js"></script>
在我的app.component.ts文件中
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthenticationService } from '../../Services/authentication.service';
declare var myFunction: any;
@Component({
moduleId: module.id,
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
// some code here
// I call f() function inside the app.component.html file in order to run the myFunction() javascript function.
f() {
new myFunction();
}
例如,如果alert()函数位于myJsFile.js文件中,则代码可以正常运行。 但就我而言,我有以下错误:
app.component.html:9错误ReferenceError:未定义图层
这意味着我必须将突触库导入某处。 github( https://github.com/cazala/synaptic )中的用法部分指出了以下内容:
var synaptic = require('synaptic'); // this line is not needed in the browser
var Neuron = synaptic.Neuron,
Layer = synaptic.Layer,
Network = synaptic.Network,
Trainer = synaptic.Trainer,
Architect = synaptic.Architect;
如果将以上代码导入到myJsFile.js中,则会出现以下错误:
myJsFile.js:1未捕获的ReferenceError:require未定义
我应该在哪里导入突触库以及如何导入?
我还找到并安装了synaptic.d.ts文件,但我不知道如何使用它。有人可以帮助我吗?
非常感谢您的宝贵时间!
您所使用的示例可能正在使用Node,要与Angular 2+一起使用,则需要使用import语法。
例如
import { Neuron, Layer, Network, Trainer, Architect} from 'synaptic';
然后您应该可以引用它们
另外,我建议将myJsFile.js
文件myJsFile.js
ts文件并以相同的方式导入。 如果所有内容都以js而不是html导入,那么它就不那么复杂了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.