【发布时间】:2018-08-30 21:40:39
【问题描述】:
在 MEAN Stack 应用程序上工作。 index.html 中的 app-root 未呈现。 index.html 正在加载,但不是模板中定义的 html。创建 REST API 并成功从 mongodb 获取数据。在stackoverflow上搜索了类似的线程,但不是同一个问题。请帮忙。
服务器.js
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var port = 3000;
var app = express();
var api = require('./server/routes/api');
app.engine('html', require('ejs').renderFile);
app.use(express.static(path.join(__dirname , '../dist')));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/api', api);
app.use('*',function (req, res) {
res.sendFile(path.join(__dirname , '../dist/VideoPlayer/index.html'));
});
app.listen(port, function () {
console.log("Server running on localhost:" + port);
})
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>VideoPlayer</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template:`
<h1>My First Angular 2 multiline template</h1>
<p>Second line</p>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
}
app-routing-module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from "./home/home.component";
import { VideoCenterComponent } from "./video-center/video-center.component";
const routes: Routes = [
{path: '', redirectTo:'/home', pathMatch:'full'},
{path: 'home', component: HomeComponent},
{path: 'videos', component: VideoCenterComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { VideoCenterComponent } from './video-center/video-center.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
VideoCenterComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
【问题讨论】:
标签: mean-stack