【问题标题】:Using vue.js props value in mounted Function [closed]在挂载的函数中使用 vue.js 道具值 [关闭]
【发布时间】:2021-08-07 22:40:04
【问题描述】:

我正在使用 props 将值从一个 vue.js 组件传递到另一个。我在我的“mounted()”函数中使用了这个 props 值。我在下面设置的警报正确显示了从道具接收到的“this.Id”变量的填充值。但是,之后在 axios 帖子中使用相同的变量时未定义。这怎么可能?

 mounted () {
 this.$nextTick(function () {
   alert(this.Id)
   axios.post('/api/title', this.Id)
     .then((response) => {
     ...
     })
  })
 }

谢谢。 J

【问题讨论】:

  • 你为什么使用this.$nextTick?这看起来不像你会使用它的地方。
  • $nextTick 的回调应该是一个箭头函数。但我不明白你所描述的行为是如何可能的。你能分享一个复制的链接吗?

标签: javascript node.js vue.js


【解决方案1】:

可能是因为上下文。试试这个,看看它是否有效:

mounted () {
  // arrow function
  this.$nextTick(() => {
    alert(this.Id)
    axios.post('/api/title', this.Id)
     .then((response) => {
       ...
     })
  })

  // or use bind, but I recommend the one above.
  // as it's much cleaner.
  this.$nextTick((function() {
    alert(this.Id)
    axios.post('/api/title', this.Id)
     .then((response) => {
       ...
     })
  }).bind(this))
}

【讨论】:

  • 谢谢@Amats 我在后端做错了什么。
猜你喜欢
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 2023-04-05
  • 2015-09-16
  • 2019-05-31
  • 2020-03-21
相关资源
最近更新 更多