我们可以在单独的项目中开发一个角度4特征模块,并与根模块建立链接以作为延迟加载吗?
如果是这样,那么loadChildren属性的路由URL是什么
答案 0 :(得分:1)
Follow this guide and check out official Angular modules like HttpModule
.
This is possible but I haven't found any official documentation or tutorial about it. What you basically have to do is to create a library that can be included in your project. Like the official RouterModule
or HttpModule
.
You basically have to create a module, export all the public stuff like directives, services and components and then finally build the whole feature module.
Building the module isn't that simple. In the HttpModule
they use ngc for compiling the typescript. ngc is the official Angular compiler. Afterwards, they package and minify their code with rollup-js and minify-js.
So the dependencies you need (for the most basic library) are @angular/core
, @angular/compiler
, @angular/compiler-cli
, rollup
, rxjs
, typescript
and zone.js
.
You also have to modify your tsconfig.json
.
These are the major changes (the second link I provided at the end lists more performance boosting changes):
"angularCompilerOptions": { "strictMetadataEmit": true}
"declaration": true
, this makes the compiler create the definitions file (you know the some-file.d.ts
stuff)"module": "es2015"
Now you need to configure the rollup.config.js
like this:
entry
should be the path to your transpiled index.ts
dest
should be bundles/modulename.umd.js
, this is the Angular standard name for modules (.umd.js
is the standard format Angular modules).moduleName
is going to be a JavaScript object so no special chars (also no dashes)@angular/core
, so globals
should be an object like this: { "@angular/core": "ng.core"}
Take a look at Angular's rollup.config.js
for the HttpModule?
: here.
Then setup your package.json
to publish your module. Imporant is that you:
"version"
(remember to use semantic versioning)"peerDependencies"
"main"
to the path of your .umd.js
file"module
" to your transpiled index.js
fileNow you can build and then add your feature module as a dependency to your other projects and use your awsome stuff there.
Two good post for further information (although the second one isn't simple to understand):
This post explains how to create a (shared) module.
This post goes more into the little details about publishing your module.
答案 1 :(得分:0)
Here is a very good talk on YouTube that will help you achieve that. It covers the configuration from scratch without tons of dependencies and boilerplate code.