我已经安装了node js mysql和mysql类型通过
"@types/mysql": "^2.15.4",
"mysql": "^2.15.0"我将电子与angular2结合使用,所以在我的组件中,我想通过
import * as mysql from 'mysql'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
connection: any;
constructor() {
this.connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '5378@Geowan5378',
database: 'company'
});
this.connection.connect((err) => {
if (err) {
console.log('error connecting', err);
}else{
console.log("connection was a success ");
}
});
} // DI但是现在我得到了一个错误
TypeError: o.createConnection is not a function这是到目前为止,我只学习电子和mysql的唯一组件。我哪里错了?
请注意,当我调用
this.connection.connect我理解这可能是由于浏览器中没有Net库造成的,但在我的情况下,我运行electron .来启动我的应用程序,所以我没有使用浏览器
或者,我如何在不使用服务器端框架的情况下将应用程序直接连接到mysql
发布于 2018-06-05 21:30:13
您必须从Electron API使用remote来要求mysql,而不是使用import。
上声明远程全局变量
<html>
<head>
...
</head>
<body>
<script>
var remote = require('electron').remote;
</script>
</body>
</html>
在app.component.ts上使用远程
declare var remote :any; // declare remote as any variable
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
connection: any;
constructor() {
var mysql = remote.require('mysql'); //require mysql with electron remote
this.connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '5378@Geowan5378',
database: 'company'
});
this.connection.connect((err) => {
if (err) {
console.log('error connecting', err);
}else{
console.log("connection was a success ");
}
});
} // DI
希望这对你有所帮助,我建议你改进mysql的服务加载方法,使其更高效地进行查询等。
编辑:或者使用我的模块https://github.com/mochrira/ngx-mysql并学习如何使用它
https://stackoverflow.com/questions/50595889
复制相似问题