随幸 2022-03-10 23:52 采纳率: 100%
浏览 49
已结题

vue在回调函数中赋值到data,然后再从data中获取之前的赋值,能否在created之后的地方获取到?


<script>
import { getOrderList, getUserList } from '@/api/pageInfo.js'
export default {
  data() {
    return {
      orderList: [],
      userList: []
    }
  },
  created() {
    this.reload()
    this.getUserLists()
  },
  methods: {
    reload() {
      const datas = {
        page: this.currentPage,
        rows: this.currentRows
      }
      getOrderList(datas) .then(res => {
           this.orderList = res.data.list
      })
      console.log(this.userList)
      console.log(this.orderList)
    },
    getUserLists() {
      const datas = {
        page: this.currentPage,
        rows: this.currentRows
      }
      getUserList(datas).then(res => {
        this.userList = res.data.list
      })
      console.log(this.userList)
      console.log(this.orderList)
    }
  }
}
</script>
请问有大佬知道,这四个console.log()是不是都能拿到值
  • 写回答

2条回答 默认 最新

  • 关注

    ajax 回调函数默认的请求方式是异步的,你需要去了解下ajax异步的概念

      getUserList(datas).then(res => {
        this.userList = res.data.list
      })
      console.log(this.userList)
      console.log(this.orderList)
    

    是先执行console.log(this.userList) console.log(this.orderList)
    之后异步加载内容,再执行回调函数中的 this.userList = res.data.list

    所以 console.log(this.userList) 输出的不是最新获取的内容, 而是上一次获取的内容

    你要把console.log(this.userList) console.log(this.orderList)放到回调函数中,这样输出的才能是最新获取的内容

    getUserList(datas).then(res => {
        this.userList = res.data.list
        console.log(this.userList)
        console.log(this.orderList)
    })
    

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月19日
  • 已采纳回答 3月11日
  • 创建了问题 3月10日

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据