首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular electron创建连接不是一个函数

问Angular electron创建连接不是一个函数
EN

Stack Overflow用户
提问于 2018-05-30 11:20:06
回答 1查看 962关注 0票数 2

我已经安装了node js mysql和mysql类型通过

代码语言:javascript
复制
"@types/mysql": "^2.15.4",
"mysql": "^2.15.0"

我将电子与angular2结合使用,所以在我的组件中,我想通过

代码语言:javascript
复制
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

但是现在我得到了一个错误

代码语言:javascript
复制
TypeError: o.createConnection is not a function

这是到目前为止,我只学习电子和mysql的唯一组件。我哪里错了?

请注意,当我调用

代码语言:javascript
复制
this.connection.connect

我理解这可能是由于浏览器中没有Net库造成的,但在我的情况下,我运行electron .来启动我的应用程序,所以我没有使用浏览器

或者,我如何在不使用服务器端框架的情况下将应用程序直接连接到mysql

EN

回答 1

Stack Overflow用户

发布于 2018-06-05 21:30:13

您必须从Electron API使用remote来要求mysql,而不是使用import。

  1. 在index.html

上声明远程全局变量

​

代码语言:javascript
复制
<html>
<head>
...
</head>
<body>

<script>
var remote = require('electron').remote;
</script>
</body>
</html>

​

在app.component.ts上使用远程

​

代码语言:javascript
复制
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并学习如何使用它

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50595889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档