我是Angular的全新角色,刚开始让我的脑袋围绕它如何工作。请互联网,温柔......Angular2下的续集w/Typescript
我已经完成了Angular2 5分钟快速入门通过英雄之旅,并没有问题的工作。现在我试图连接到我的本地MS SQL服务器,因为我有一个基于SQL的项目,其中Angular2是必需的平台。我一直在争取Sequelize的工作,并且可以真正使用一些帮助,让自己的头撞墙。
什么我迄今所做的基于不同的事情我读过:
- 使用NPM安装sequelize和验证sequelize和lodash,蓝鸟和验证所有依赖下node_modules 存在
- 从GitHub的DefinitelyTyped项目下载bluebird.d.ts,lodash.d.ts,sequelize.d.ts和validator.d.ts,并将它们放在我的类型文件夹中
- 在tsConfig.json中,我更改了compilerOptions - >从es5到es6的目标
- 创建一个sim PLE db.component.ts文件(见下文)
- 更新app.component.ts导入db.component并增加了一个途径是
此时简单的页面来了,但我无法弄清楚如何让Sequelize工作。
我看到脚本错误(基于使用Visual Studio代码为我的编辑):
- 当我打开sequelize.d.ts,有就行了'申报模块“sequelize错误“{',指出”环境模块不能嵌套其他模块“
- 当我打开lodash.d.ts时,在行239和240(下划线)上出现错误,指出”重复的标识符“_'”
我已经尝试了许多不同的方法(猜测),关于如何获取连续序列以连接到我的数据库,并且无法获得任何工作。我知道我需要db.component.ts文件中的一个导入和/或一个require,但最终只会在IDE中出现错误语法或在浏览器中出现错误(Chrome)。
我明白,从长远来看,我在这里测试的route/config/etc不会是“正确”的方式,但我只需要通过基本证明-of概念,我可以做一些与数据库之前,我重新设计它(例如 - 我可以连接到数据库和查询表)
app.component.ts
import { Component } from 'angular2/core';
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';
import { DashboardComponent } from './dashboard.component';
import { HeroService } from './hero.service';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
import { dbComponent } from './db.component';
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<nav>
<a [routerLink]="['Dashboard']">Dashboard</a>
<a [routerLink]="['Heroes']">Heroes</a>
</nav>
<router-outlet></router-outlet>
`,
styleUrls: ['app/app.component.css']
directives: [ROUTER_DIRECTIVES],
providers: [
ROUTER_PROVIDERS,
HeroService
]
})
@RouteConfig([
{
path: '/dashboard',
name: 'Dashboard',
component: DashboardComponent,
useAsDefault: true
},
{
path: '/heroes',
name: 'Heroes',
component: HeroesComponent
},
{
path: '/detail/:id',
name: 'HeroDetail',
component: HeroDetailComponent
},
{
path: '/sql',
name: 'SqlTest',
component: dbComponent
}
])
export class AppComponent {
title = 'Sample App';
}
db.component.ts
/// <reference path="../typings/sequelize.d.ts" />
import { Component, OnInit } from 'angular2/core';
import Sequelize = require('sequelize'); <-- I dont know if this is doing anything
//- This just errors out with require not being found ([email protected] installed)
//var Sequelize = require("sequelize");
//- I know this goes somewhere, but I have no idea where
//var sql = new Sequelize('dbName', 'uName', 'pw', {
// host: "127.0.0.1",
// dialect: 'mssql',
// port: 1433 <-- SqlExpress
//});
@Component({
selector: 'my-sql',
template:`
<h1>SQL Test</h1>
`
})
export class dbComponent implements OnInit {
constructor(
) { }
auth() {
console.log('auth');
//sql.authenticate().then(function(errors) { console.log(errors) });
}
ngOnInit() {
console.log('onInit');
this.auth();
}
}
我的console.log消息即将出现,所以我相信我的核心功能基本上可以工作,但是我在为了获得Sequelize功能而需要做的事情方面感到茫然。
我错过了一块拼图,我在这个智慧结束。预先感谢任何方向......
请明确提供错误 –
@PatrickMotard:我在“脚本错误”中列出的可能是问题的根源,但我不知道该怎么做,或者因为它们只显示在IDE中,如果他们是“真实”的错误。 db.components.ts代码段中提到的'require'错误在浏览器中显示为“require is not defined”。 – UncleRico