Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

angular - 有没有更好的方法来定义这些角度/离子模型?(Is there a better way to define these Angular/Ionic models?)

A new Angular/Ionic project I'm working on has many models defined in a single .model.ts file in the following way.

(我正在研究的一个新的Angular / Ionic项目通过以下方式在单个.model.ts文件中定义了许多模型。)

export class Order {
    constructor(
        public OrderId?: string,
        public Items?: string[],
    ) {}
}

export class OrderStatus {
    constructor(
        public OrderId?: string,
        public __str__: string,
    ) {}
}

And is imported in other files using

(并使用其他文件导入)

import { Order, OrderStatus } from 'src/app/.../models/orders/orders.model.ts';

which is a nice one-liner but i'm wondering wether if there's a better way load the models and structure directories/files.

(这是一个很好的单行代码,但我想知道是否还有更好的方式加载模型和结构目录/文件。)

I was thinking of implementing the following pattern:

(我正在考虑实现以下模式:)

'src/app/.../models/orders/order.model.ts';
'src/app/.../models/orders/order-status.model.ts';

Where each file has its corresponding Order and OrderStatus classes defined.

(每个文件都定义了相应的OrderOrderStatus类。)

This seems to me a better way of abstracting each models since now the file name directly correspond to the class defined inside;

(在我看来,这是抽象每个模型的更好方法,因为现在文件名直接对应于内部定义的类;)

a one-to-one relation is stablished between file and model and.

(在文件与模型之间建立了一对一的关系。)

My motivation for reshaping this file structure is for facilitating future escalations of the project.

(重塑此文件结构的动机是为了促进项目的未来升级。)

A drawback I do foresee is having to use two lines of code when importing these two models in the same component/service.

(我预见的一个缺点是,在同一组件/服务中导入这两个模型时,必须使用两行代码。)

Where i would once import the two classes like this

(我曾经将这样的两个类导入的地方)

import { Order, OrderStatus } from 'src/app/.../models/orders/orders.model.ts';

i would now have to import them like this

(我现在必须像这样导入它们)

import { Order } from 'src/app/.../models/orders/order.model.ts';
import { OrderStatus } from 'src/app/.../models/orders/order-status.model.ts';

I already googled about this and couldn't find any documentation about best practices in model defining for Ionic projects, which is why I'm posting here with a logic approach I thought of.

(我已经对此进行了搜索,找不到关于Ionic项目的模型定义最佳实践的任何文档,这就是为什么我在这里以我想到的逻辑方法发表文章的原因。)

Am I going in the right direction?

(我朝着正确的方向前进吗?)

Should I leave the current structure alone and focus on something else?

(我是否应该不理会当前结构,而专注于其他事情?)

Is there a better way to do this?

(有一个更好的方法吗?)

  ask by gerardo mijares translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...