2016-02-16 40 views
8

我已經爲我的angular2組件開發了一個解析器,它將它們呈現在我的HTML文件中。 到目前爲止,我能夠使它們在HTML作爲如何使用Angular 2服務器端渲染

import {Component} from 'angular2/core'; 
import {NgSwitch, NgSwitchWhen, NgSwitchDefault,NgFor} from 'angular2/common'; 
import {Http, Response} from 'angular2/http'; 
import {Observable} from 'rxjs/Rx'; 

@Component({ 
    selector: 'app',  
    template: ` 
      <div> 
       <div> 
        <img src={{record}} class="pull-left image-responsive" style="width:50%;"> 
        <img src={{record}} class="pull-right image-responsive" style="width:30%;"> 
       </div> 
       <div> 
        <img src={{record}} class="pull-right image-responsive" style="width:100%;"> 
       </div> 
       <br/><br/> 
       <div> 
        <table class="table-responsive" style="width:100%;border-collapse:separate;border:solid #D8D8D8 2px;border-radius:5px;"> 
         <tr style="background-color: grey;"> 
          <th style="text-align:center;">{{record}}</th> 
          <th style="border-left:2px solid #f5821f;text-align:center;">{{record}}</th> 
         </tr> 
         <tr style="text-align:center;"> 
          <td >&nbsp;</td> 
          <td style="border-left:2px solid #f5821f;">&nbsp;</td> 
         </tr> 
        </table> 
       </div> 
       <br/> 
      </div> 
      ` 
}) 
export class App{ 
    public record; 
    constructor() {  
     this.record="angular2"; 
    } 
} 

然後我的靜態HTML被正確生成,但如果我用我的部件是這樣的:

import {Component} from 'angular2/core'; 
import {NgSwitch, NgSwitchWhen, NgSwitchDefault,NgFor} from 'angular2/common'; 
import {Http, Response} from 'angular2/http'; 
import {Observable} from 'rxjs/Rx'; 
import {Component1} from './component_1/component_1.component'; 
import {Component2} from './component_2/component_2.component'; 

@Component({ 
    selector: 'app', 
    directives: [Component1,Component2], 
    template: ` 
     <div class="container"> 
      <div class="row"> 
       <component_1>loading...</component_1> 
      </div> 
      <div class="row"> 
       <component_2>loading...</component_2> 
      </div>    
     </div> 
    ` 
}) 
export class App{ 
    public record; 
    constructor() {  
     this.record="angular2"; 
    } 
} 

然後我的組件加載在正確的HTML,但我的靜態HTML不正確生成。 見下面的圖片: 組件加載到瀏覽器 Components

但靜態的HTML沒有生成靜態HTML Console for Static HTML

Server.ts正確 控制檯窗口

import * as path from 'path'; 
 
import * as express from 'express'; 
 
import * as universal from 'angular2-universal-preview'; 
 
// Angular 2 
 
import {App} from './app/app.component' 
 
let app = express(); 
 
let root = path.join(path.resolve(__dirname, '..')); 
 

 
// Express View 
 
app.engine('.ng2.html', universal.ng2engine); 
 
app.set('views', __dirname); 
 
app.set('view engine', 'ng2.html'); 
 

 
// Serve static files 
 
app.use(express.static(root)); 
 
var i = 0; 
 
// Routes 
 

 
app.use('/', (req, res) => { 
 
    res.render('index_new', {App}, function(err,html){ 
 
     console.log("------------------My HTML ---------\n"+(i++)); 
 
     console.log(html); 
 
     console.log("------------------My HTML ----------"); 
 
     res.send(html); 
 
    }); 
 
}); 
 

 
// Server 
 
app.listen(3000,() => { 
 
    console.log('Listen on http://localhost:3000'); 
 
});

index_new.ng2.html

<!doctype html> 
 
<html lang="en"> 
 

 
<head> 
 
    <link rel="icon" href="data:;base64,iVBORw0KGgo="> 
 
    <link href="\node_modules\bootstrap\dist\css\bootstrap.min.css" rel="stylesheet" /> 
 
    <link href="\node_modules\bootstrap\dist\css\style.css" rel="stylesheet" /> 
 
    <script src="/node_modules/es6-shim/es6-shim.js"></script> 
 
    <script src="/node_modules/es6-promise/dist/es6-promise.js"></script> 
 
    <script src="/node_modules/reflect-metadata/Reflect.js"></script> 
 
    <script src="/node_modules/zone.js/dist/zone-microtask.js"></script> 
 
    <script src="/node_modules/zone.js/dist/long-stack-trace-zone.js"></script> 
 
    <script src="/dist/client/bundle.js"></script>  
 
</head> 
 
<body> 
 
    <app>Loading....</app>  
 
</body> 
 
</html>

我試過@alexpods的更新的Git倉庫中心提前 感謝。

回答

4

我有universal-starter-kit 同樣的問題,嘗試https://github.com/pmachowski/angular2-starter-kit

優點:

  • 服務器端渲染實際工作
  • 網絡工作者
  • 生產準備
  • 良好的結構
  • 正在積極開發
  • 現代堆棧(的WebPack,因果報應,量角器)
  • 單元&端對端測試

+0

試過這個...但我的節點模塊沒有安裝@Peter –

+1

@BhushanGadekar我可以看到你在Windows上。我在Mac上,並且node_modules沒有問題。也許如果你發佈錯誤信息我可以幫忙。 –

+0

'類型ERR!消息無法解析「github:typings/typed-es6-promise」\ n typings ERR!無法連接到「https://raw.githubusercontent.com/typings/typed-es6-promise/master/typings.json」造成的\ n 類型ERR!由getaddrinfo引起的ENOTFOUND proxy.example.com' –

1

對此example app,我最近部署(https://github.com/ng-seed/universal

看看優點:

  • 服務器端渲染
  • SCSS編譯
  • 懶/異步線路
  • 開發/生產(AOT)編譯
  • SPA /通用模式
  • TsLint/Codelyzer
  • 截至到最新的依賴關係(角4,平臺服務器等)
  • 現代堆棧(webpack,karma,量角器)
相關問題