javascript - Routing not working for bundled (webpack) Angular 2 project -
i have simple angular application 2 components (appcomponent , tester) webpacked single app.bundle.js file. problem once app bundled routing no longer works. have tried few different methods seen online working no luck, can help? have seen mention of bundling , routing via modules, rather keep entire angular project single file if possible
<!doctype html> <html> <head> <title>routing webpack test</title> <base href="/"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="slate.css"> </head> <my-app>loading...</my-app> <body> <script type="text/javascript" src="app.bundle.js"></script> </body> </html>
app.component.ts
import { component, input, output } '@angular/core'; import { route } "@angular/router"; @component({ selector: 'my-app', template: `<h1>hello world</h1>` }) export class appcomponent {}
app.module.ts:
import { ngmodule } '@angular/core'; import { browsermodule } '@angular/platform-browser'; import { formsmodule } '@angular/forms'; import { routermodule, routes } '@angular/router'; import { appcomponent } './app/app.component' import { tester } './app/test.component'; const approutes: routes = [ { path: '', component: appcomponent }, { path: 'test', component: tester }, { path: 'myapp', component: appcomponent }, ]; @ngmodule({ imports: [ routermodule.forroot(approutes,{ enabletracing: true }), browsermodule, formsmodule], exports: [routermodule], declarations: [appcomponent, tester ], bootstrap: [appcomponent] }) export class appmodule { }
webpack.config.js:
var webpack = require('webpack'); var htmlwebpack = require('html-webpack-plugin'); var copywebpackplugin = require('copy-webpack-plugin'); var compressionplugin = require('compression-webpack-plugin'); module.exports = { entry: [ __dirname + '/src/main.ts' ], output: { path: __dirname + '/src/dist', filename: 'app.bundle.js' }, module: { loaders: [ { test: /\.ts$/, loaders: ['ts-loader', 'angular2-template-loader', 'angular2-router- loader'] }, ] }, resolve: { extensions: ['.js', '.ts'] }, plugins: [ new htmlwebpack({ template: './src/index.html' }), new copywebpackplugin([ { from: './src/slate.css' } ]), new webpack.defineplugin({ 'process.env': { 'node_env': json.stringify('') } }), new webpack.optimize.aggressivemergingplugin(), new compressionplugin({ asset: "[path].gz[query]", algorithm: "gzip", test: /\.js$|\.css$|\.html$/, threshold: 10240, minratio: 0.8 }), new webpack.contextreplacementplugin( /angular(\\|\/)core(\\|\/)@angular/, './src', {} ), ], };
the template needs placeholder( routeroutlet) router components loaded. think missing
<router-outlet> </router-outlet>
Comments
Post a Comment