2016-08-07 14 views
1

我已经生成了默认的快速应用程序,我修改了其中的app.js文件中的以下行。在没有模板引擎的情况下将路由表达为ng2

app.set('views', path.join(__dirname, '/views')); 
app.engine('html', require('jade').renderFile); 
app.set('view engine', 'html'); 

在./views目录,我本来layout.jadeindex.jadeerror.jade。好,那么好。当项目运行时,我会看到索引或错误布局。

现在...我想添加一个最小的angular2安装,以便让ng2管理一个1页的应用程序,其中index.jade目前是。

所以我把一个app.component.ts

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'my-app', 
    template: '<h1>My First Angular 2 App</h1>' 
}) 

export class AppComponent { } 

main.ts文件:

import { bootstrap } from '@angular/platform-browser-dynamic'; 
import { AppComponent } from './app.component'; 
bootstrap(AppComponent); 

然后,我改变index.jadeindex.html并补充说:

<html> 
    <head> 
    <title>Angular 2 QuickStart</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="../public/stylesheets/style.css"> 
    <!-- 1. Load libraries --> 
    <!-- Polyfill(s) for older browsers --> 
    <script src="node_modules/core-js/client/shim.min.js"></script> 
    <script src="node_modules/zone.js/dist/zone.js"></script> 
    <script src="node_modules/reflect-metadata/Reflect.js"></script> 
    <script src="node_modules/systemjs/dist/system.src.js"></script> 
    <!-- 2. Configure SystemJS --> 
    <script src="systemjs.config.js"></script> 
    <script> 
     System.import('app').catch(function(err){ console.error(err); }); 
    </script> 
    </head> 
    <!-- 3. Display the application --> 
    <body> 
    <my-app>Loading...</my-app> 
    </body> 
</html> 

然而, HTML模板引擎和m y新error.htmlindex.html文件,我总是会显示错误页面,而不是我的ng2管理索引。任何想法为什么?

+0

您是否知道这一点?下面的建议说ejs,但我也不想使用模板引擎。使用angular 2选择器来提供index.html文件的正确方法是什么?这样我就可以将前端传递给角度2而不会出现任何奇怪的错误。就像我刷新页面时一样 – wuno

回答

0

现在最好的选择是使用ejs(引擎)并将其配置为接受并呈现html。

代码:

app.set( '视图',path.join(__dirname, '视图'));
app.set('view engine','ejs'); //模板引擎

app.engine('html',require('ejs')。renderFile); //转动引擎使用html

注意:您的所有视图或模板都应该使用.html扩展名

相关问题