使用“路径"在tsconfig.server.json中用于服务器端重新工作不起作用 [英] using "paths" in tsconfig.server.json for server side redering not working
问题描述
为了避免导入文件时出现相对路径,我尝试配置angular以了解路径.
In order to avoid relative path when I import file, I try to configure angular to understand the path.
所以我将其配置为:
//tsconfig.server.json
"compilerOptions":{
"baseUrl": ".",
"paths": {
"@env/*": ["./enviroment/*"]
...
},
}
//and in a component I use like this:
import { X } from '@env/enviroment'
运行ng服务时,一切正常.
when running ng serve, Everything working fine.
但是在服务器端渲染的情况下,它将抛出错误
But in case of server-side rendering, it throws the error as
找不到模块'@ env/environment'.
Cannot find module '@env/environment'.
服务器端渲染不支持路径"吗?
Is "paths" are not supported by server-side rendering?
PS:参考号.从这里 https://github.com/angular/universal/issues/734#issuecomment-306176547
PS: Ref. from here https://github.com/angular/universal/issues/734#issuecomment-306176547
没关系,似乎问题在于尽管客户端渲染了该行:
Never mind, it seems the problem is that although for client side rendering the line:
import { environment } from 'environments/environment';
效果很好,对于服务器端渲染,该行必须为:
works well, for server side rendering the line must be:
import { environment } from '../environments/environment';
是否可以在服务器端渲染中使用路径"?
Is it possible to use "paths" in server-side rendering ?
推荐答案
默认情况下,您的 tsconfig.server.json
扩展了 tsconfig.app.json
,如果使用路径,它们存在于 tsconfig.app.json
中.
As default your tsconfig.server.json
extends tsconfig.app.json
and if you use paths, they exists in tsconfig.app.json
.
您应该做的一件事是从 tsconfig.server.json
中删除 baseUrl
参数.因为在该文件中将其定义为." ,但是在 tsconfig.json
中将其定义为"./" .
One thing what you should do is remove baseUrl
parameter from tsconfig.server.json
. Because in that file is defined as ".", but in tsconfig.json
is defined as "./".
这篇关于使用“路径"在tsconfig.server.json中用于服务器端重新工作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!