json路径映射的方法

json路径映射的方法

本文介绍了Angular 7找不到找到tsconfig.json路径映射的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始了一个angular 7项目,并且试图在angular.json上配置路径映射"以更改导入方式:

I've started an angular 7 project and I'm trying to configure the "path mapping" on angular.json to change my import way from this:

import { environment } from '../../../environments/environment';

对此:

import { environment } from '@environments/environment';

我已经在根级别tsconfig.json文件中完成了此配置:

I've done this config on the root level tsconfig.json file:

"compilerOptions": {
        "baseUrl": "src", // This must be specified if "paths" is.
         ...
        "paths": {
            "@app/*": ["app/*"],
            "@config/*": ["app/_config/*"],
            "@environments/*": ["environments/*"],
            "@shared/*": ["app/_shared/*"],
            "@helpers/*": ["helpers/*"]
        },
...

但我也在cli上收到此错误

But I also get this error on the cli

ERROR in src/app/errors/not-foud/not-found.component.ts(2,29): error TS2307: Cannot find module '@environments/environment'

有什么我想念的吗?

推荐答案

尝试一遍后,我发现问题出在我的vscode设置之一:"设置为相对" 而不是非相对" 的打字稿>首选项:导入模块说明符" .改变这一点,我就能解决问题.

After trying and trying again I find out that the problem was with one of my vscode setting that was called:"Typescript > Preferences: Import Module Specifier" that was set to "relative", instead of "non-relative". Changing this, I was able to solve the problem.

这篇关于Angular 7找不到找到tsconfig.json路径映射的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:33