简体   繁体   中英

How to import Material Module or NgxBootstrap in Angular Universal?

Currently I am trying to build a project using Angular Universal. I'm using Angular 4. If I import Material Module - the project can be built, but when serving through ts-node an error appears -

dist\ngfactory\node_modules\@angular\material\typings\index.ngfactory.ts:9
import * as import0 from '@angular/core';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Module._extensions..js (module.js:580:10)

Or If I try to import NgXBootstrap then this error appears -

node_modules\ngx-bootstrap\dropdown\bs-dropdown.module.js:1
(function (exports, require, module, __filename, __dirname) { import { NgModule } from '@angular/core';
                                                          ^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)

I'm following this project structure - https://github.com/designcourse/angular-seo

I am pasting the code here -

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

//Material Module
import {MaterialModule} from '@angular/material';

//Ngx Bootstrap
import {BsDropdownModule} from 'ngx-bootstrap';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { PostsComponent } from './posts/posts.component';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    PostsComponent
  ],
  imports: [
    BrowserModule.withServerTransition({appId: 'universal-app'}),
    FormsModule,
    HttpModule,
    AppRoutingModule,
    MaterialModule,
    BsDropdownModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]

})
export class AppModule { }

app.server.module.ts

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';

@NgModule({
imports: [
    ServerModule,
    AppModule
],
bootstrap: [AppComponent]
})
export class AppServerModule { }

server.ts

import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { platformServer, renderModuleFactory } from '@angular/platform-server'
import { enableProdMode } from '@angular/core'
import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory'
import * as express from 'express';
import { readFileSync } from 'fs';
import { join } from 'path';

const PORT = 4000;

enableProdMode();

const app = express();

let template = readFileSync(join(__dirname, '..', 'dist', 'index.html')).toString();

app.engine('html', (_, options, callback) => {
  const opts = { document: template, url: options.req.url };

  renderModuleFactory(AppServerModuleNgFactory, opts)
    .then(html => callback(null, html));
});

app.set('view engine', 'html');
app.set('views', 'src')

app.get('*.*', express.static(join(__dirname, '..', 'dist')));

app.get('*', (req, res) => {
  res.render('index', { req });
});

app.listen(PORT, () => {
  console.log(`listening on http://localhost:${PORT}!`);
});

I guess this is a problem for all third party libraries in Angular Universal. So, How do I properly import these libraries ?

I would suggest you use this seed project ng-seed/universal instead of the one you are currently following.

Now if you structure your app following ng-seed/universal then for using Material Module and BsDropdownModule you would simply add these lines in client/app/app.module.ts

import {MaterialModule} from '@angular/material';
import {BsDropdownModule} from 'ngx-bootstrap';

Make sure you update your package.json accordingly. Then run :

npm install

# dev build (Universal)
npm run build:universal-dev
# prod build (Universal)
npm run build:universal-prod

# start the server (Angular Universal)
npm run serve

All should be working alright now.

Currently I have an app using MdButtonModule, MdSidenavModule, and MdToolbarModule. These definitely work, and the trick was to import hammerjs in src/main.ts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM