Our components often refer to external template and style files.
We identify those files with a URL in the templateUrl and styleUrls properties of the @Component metadata
as seen here:
By default, we must specify the full path back to the application root.
We call this an absolute path because it is absolute with respect to the application root.
使用绝对路径有两个问题:
There are two problems with an absolute path:
我们不得不记住到应用程序根目录的完整路径。
We have to remember the full path back to the application root.
当我们在应用的文件结构中移动这个组件时,将不得不更新这个URL
We have to update the URL when we move the component around in the application files structure.
如果能用相对于组件类文件的路径来指定模板和样式表的位置,那么编写和维护组件就会变得容易得多。
It would be much easier to write and maintain our application components if we could specify template and style locations
relative to their component class file.
We can if we build our application as commonjs modules and load those modules
with a suitable package loader such as systemjs or webpack.
Learn why below.
The Angular CLI uses these technologies and defaults to the
component-relative path approach described here.
CLI users can skip this chapter or read on to understand
how it works.
组件相对路径
Component-Relative Paths
目标是把模板和样式表的URL指定为相对于组件类的路径,因此得名组件相对路径。
Our goal is to specify template and style URLs relative to their component class files,
hence the term component-relative path.
成功的关键是遵循一个约定:把相对组件的文件放进众所周知的位置。
The key to success is following a convention that puts related component files in well-known locations.
We recommend keeping component template and component-specific style files as siblings of their
companion component class files.
Here we see the three files for SomeComponent sitting next to each other in the app folder.
We'll have more files and folders — and greater folder depth — as our application grows.
We'll be fine as long as the component files travel together as the inseparable siblings they are.
Having adopted this file structure convention, we can specify locations of the template and style files
relative to the component class file simply by setting the moduleId property of the @Component metadata like this
moduleId:module.id,
从templateUrl和styleUrls中把基准路径app/去掉了。结果是这样的:
We strip the src/app/ base path from the templateUrl and styleUrls and replace it with ./.
The result looks like this:
A component-relative path is obviously superior to an absolute path.
Why did Angular default to the absolute path?
Why do we have to set the moduleId? Why can't Angular set it?
首先,如果只使用相对路径而省略掉moduleId,我们来看看会发生什么。
First, let's look at what happens if we use a relative path and omit the moduleId.
EXCEPTION: Failed to load some.component.html
EXCEPTION: Failed to load some.component.html
Angular找不到这个文件,所以它抛出一个错误。
Angular can't find the file so it throws an error.
为什么Angular不能相对于组件类文件的路径来自动计算模板和样式表的URL呢?
Why can't Angular calculate the template and style URLs from the component file's location?
Because the location of the component can't be determined without the developer's help.
Angular apps can be loaded in many ways: from individual files, from SystemJS packages, or
from CommonJS packages, to name a few.
We might generate modules in any of several formats.
We might not be writing modular code at all!
由于存在这么多打包和模块加载策略,所以Angular不可能知道在运行期这些文件的正确位置。
With this diversity of packaging and module load strategies,
it's not possible for Angular to know with certainty where these files reside at runtime.
The only location Angular can be sure of is the URL of the index.html home page, the application root.
So by default it resolves template and style paths relative to the URL of index.html.
That's why we previously wrote our file URLs with an app/ base path prefix.
But if we follow the recommended guidelines and we write modules in commonjs format
and we use a module loader that plays nice,
then we — the developers of the application —
know that the semi-global module.id variable is available and contains
the absolute URL of the component class module file.
这种认知让我们得以通过设置moduleId来告诉Angular组件类文件在哪里:
That knowledge enables us to tell Angular where the component file is
by setting the moduleId:
moduleId:module.id,
Webpack: 加载模板和样式表
Webpack: load templates and styles
Webpack开发者可以采用moduleId的另一个替代方案。
Webpack developers have an alternative to moduleId.
They can load templates and styles at runtime by adding ./ at the beginning of the template and styles / styleUrls
properties that reference *component-relative URLS.