我是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