V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
phpuser
V2EX  ›  程序员

请教前后端分离时关联表的 API 组织方式

  •  
  •   phpuser · Oct 2, 2019 · 2763 views
    This topic created in 2413 days ago, the information mentioned may be changed or developed.
    如:

    后台管理前后端分离时,表 user,联 user_type,city 表,字段为 user_type_id, city_id.

    请问编辑 user 表的时候 API 中是包含 user_type, city 的下拉框列表还是另外调用 user_type,city 的 api 取列表?
    另展示 user 列表时的 API 是直接返回 user_type_id,city_id 在 json 里面,还是把 user_type 和 city 对应的名字返回在输出中?
    如果是 user_type_id 那么如果在列表中展示相应的名称。


    谢谢。
    3 replies    2019-10-04 17:33:04 +08:00
    xuanbg
        1
    xuanbg  
       Oct 2, 2019
    下拉框列表调用 user_type,city 的 api
    前端显示什么,接口就返回什么,前端最好不要自己多个查询拼凑数据,除非是应用的特殊需求。另外,数据的格式化应该由前端处理,如日期格式,小数点对齐等等。
    intermole
        2
    intermole  
       Oct 2, 2019 via iPhone
    user_type 和 city 的下拉列表直接调用对应表的 api 获取,如果这两组信息比较常用,可以在前端初始化的时候就从后台返回存在 vuex 之类的 store 里 n。
    后台直接返回 user_type_id 和 city_id 就好了,前端的下拉列表控件会自动匹配出中文名的
    phpuser
        3
    phpuser  
    OP
       Oct 4, 2019
    感谢两位的耐心回复,多谢。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3155 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 14:44 · PVG 22:44 · LAX 07:44 · JFK 10:44
    ♥ Do have faith in what you're doing.