简体   繁体   English

对于使用TypeScript编写的项目,使用变量而不是字符串和相对路径

[英]Use variables instead of strings with relative paths for a project written in TypeScript

Can I use something like namespaces inside a large project for imports? 我可以在大型项目中使用类似命名空间的东西进行导入吗?

For example, instead of 例如,而不是

import {formatNumber} from '../../../utils/formatters'

I could use 我可以用

import {formatNumber} from utils.formatters

I think it could simplify future changes in the structure of the project, when I move some components around and don't have to look for the relative paths to change. 我认为它可以简化项目结构的未来变化,当我移动一些组件而不必寻找改变的相对路径时。

How can I tackle the problem? 我该如何解决这个问题?

when I move some components around and don't have to look for the relative paths to change 当我移动一些组件而不必寻找改变的相对路径时

FWIW with TypeScript it will be a compile error (that you can fix easily with stuff like path completions https://basarat.gitbooks.io/alm/content/features/autocomplete.html#path-completions ) so you are better off than you would be with pure JavaScript. 使用TypeScript的FWIW将是一个编译错误(你可以轻松修复路径完成https://basarat.gitbooks.io/alm/content/features/autocomplete.html#path-completions之类的东西),所以你比你将使用纯JavaScript。

Can I use something like namespaces inside my large project for imports 我可以在我的大型项目中使用类似名称空间的东西进行导入

No. Currently it requires manual management of file ordering which can quickly turn into a pain : https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md 不。目前它需要手动管理文件排序,这很快就会变成一种痛苦: https//github.com/TypeStrong/atom-typescript/blob/master/docs/out.md

More 更多

Having relative file paths is indeed a pain. 拥有相对文件路径确实很痛苦。 But that is how some JavaScript module systems work (especially nodejs). 但这就是一些JavaScript模块系统的工作方式(尤其是nodejs)。

I would suggest to follow technique called "Barrels" (doc link is from angular2 but it is general concept) 我建议遵循称为“桶”的技术(doc链接来自angular2但它是一般概念)

https://angular.io/docs/ts/latest/guide/style-guide.html#!#create-and-import-barrels https://angular.io/docs/ts/latest/guide/style-guide.html#!#create-and-import-barrels

So, on a global folder level create a file with all these links (and keep only reference to each file there) 因此,在全局文件夹级别创建一个包含所有这些链接的文件(并且仅保留对每个文件的引用)

export * from './config';
export * from './entity.service';
export * from './exception.service';
export * from './filter-text';
export * from './init-caps.pipe';
export * from './modal';
export * from './nav';
export * from './spinner';
export * from './toast';

Later, anywhere deep in the folder structure , just keep the reference to this file... 后来, 随时随地文件夹结构中 ,只要保持引用此文件...

You can have more of them, and even use some kind of inner sub-namespace. 你可以拥有更多,甚至使用某种内部子命名空间。 Eg this is an example of the root/global 'ng.ts' 例如,这是root / global'ng.ts'的示例

// Detail
export * from "@angular/core";

import * as common from "@angular/common";
import * as http from "@angular/http";
import * as upgrade from "@angular/upgrade";
import * as platform from "@angular/platform-browser-dynamic";

// with sub namespaces
export { common, http, platform, upgrade };

and then this is valid 然后这是有效的

import * as ng from "../../../ng";

@ng.Component(... // root is 'core'
...
let control = new ng.common.Control(); // 'common' is nested as common

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我已经编写了自己的 npm 模块,当我将其导入新项目时,所有资产路径都相对于项目而不是模块内部? - I've written my own npm module, when I import it into a new project, all the asset paths are relative toward the project instead of inside the module? 如何在 PHP 中使用路径而不是 GET 变量? - How to use paths instead of GET variables in PHP? 如何仅在Rails中使用相对路径,而不使用资产管道? - How to only use relative paths in Rails, instead of using the asset pipeline? 为什么在JavaScript的相对路径中使用正斜杠“ /”而不是反斜杠“ \\”? - Why use forward slash '/' instead of backward slash '\' in relative paths in JavaScript? 将Typescript绝对路径转换为Node.js相对路径? - Converting Typescript absolute paths to nodejs relative paths? 如何在 React 或 Next.js 中使用绝对路径而不是相对路径? - how to use absolute paths instead of relative paths in React or Next.js? IntelliJ打字稿参考路径不是相对的 - IntelliJ Typescript Reference Paths Aren't Relative Angular的相对路径更短-Webpack和Typescript - Shorter relative paths with Angular - Webpack and Typescript 无法将项目转换为 Typescript 路径 - Trouble converting project to Typescript paths 为什么一些专业网页设计师使用绝对路径而不是相对路径(例如CSS,Javascript,图像等)? - Why some professional web designers use absolute paths instead of relative paths (e.g for CSS, Javascript, images, etc.)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM