随笔 - 3, 文章 - 0, 评论 - 0, 阅读 - 13

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

php-vue-博客系统开发bug

Posted on   卖姜茶  阅读(2)  评论(0编辑  收藏  举报

[1]前后端交互数据的形式:
后端返回的数组为对象:
{{"c_id":1,"c_name":"\u524d\u7aef\u5f00\u53d1","c_pid":0,"c_create_time":null,"c_update_time":null,"children":[{"c_id":11,"c_name":"Html/Css","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":12,"c_name":"JavaScript","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":13,"c_name":"jQuery","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":14,"c_name":"Angular","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":15,"c_name":"React","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":16,"c_name":"Vue","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"}],"showSubCategories":"false"}]}
转换成
后端返回的数组为数组:[有的表面上是数组,但是实际上是json,这种要json.parse解析,数据经console.log可以返回的是被解析后,并列出数组的key和value]
[{"c_id":1,"c_name":"\u524d\u7aef\u5f00\u53d1","c_pid":0,"c_create_time":null,"c_update_time":null,"children":[{"c_id":11,"c_name":"Html/Css","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":12,"c_name":"JavaScript","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":13,"c_name":"jQuery","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":14,"c_name":"Angular","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":15,"c_name":"React","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"},{"c_id":16,"c_name":"Vue","c_pid":1,"c_create_time":null,"c_update_time":null,"children":[],"showSubCategories":"false"}],"showSubCategories":"false"}]]
转换方法:
后端返回是:①return json_encode($result);,将 PHP 中的 $result 变量转换为 JSON 格式的字符串并返回,可以参考文档添加参数设置选项 ②return json($result)
前端转换:①前端接收到json数据,要进行categories.value = JSON.parse(response.data);将 response.data 中的 JSON 格式的字符串解析为 JavaScript 对象,可以在 JavaScript 中以对象的形式来操作和使用原本以字符串形式存在的 JSON 数据
const data = Object.values(response.data);从 response.data 对象中获取所有的值,并将这些值存储在一个新的数组 data 中,如果 response.data 是 {key1: value1, key2: value2} ,那么 Object.values(response.data) 将会返回 [value1, value2] ,然后将其赋值给 data 变量
但这种在前端遍历前要先进行处理:

const response = await api.get('api/category/getCategories');
  const data = Object.values(response.data);
  const formattedCategories = data.map(category => ({
    c_name: category.c_name,
    c_id: category.c_id,
    children: category.children.map(subCategory => ({
      c_name: subCategory.c_name,
      c_id: subCategory.c_id
    }))
  }));
categories.value = formattedCategories;

[2]图片地址错误:@/../都不对时,尝试从src开头
[3]vue3前端的数据定义
①如果设定'const user = ref([id:])',暂未找到成功设定数据方法
②如果设定'const user = ref({})',设定user.value.id = 数据 ;
[4]在

相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示