javascript - 在挂载函数中使用 vue.js 道具值
问题描述
我正在使用道具将值从一个 vue.js 组件传递到另一个。我在我的“mounted()”函数中使用了这个 props 值。我在下面设置的警报正确显示了从道具接收到的“this.Id”变量的填充值。但是,之后在 axios 帖子中使用相同的变量时未定义。这怎么可能?
mounted () {
this.$nextTick(function () {
alert(this.Id)
axios.post('/api/title', this.Id)
.then((response) => {
...
})
})
}
谢谢你。Ĵ
解决方案
可能是因为上下文。试试这个,看看它是否有效:
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))
}
推荐阅读
- javascript - 不断从字段中获取数据并插入到div中
- python - Python 麻烦保存几个池处理的文件
- omnet++ - 我如何将排队库链接到项目
- r - 在 R 中将栅格转换为空间点数据框时出错
- powershell - 带有 -Force 的 cmdlet Stop-Process 不起作用
- java - Eclipse 中的 Spring MVC 项目设置 - 单独的基础包和 web 和 rest api
- c# - VisualStudio 2015 C# WPF 中 POST 的 405 响应来自我的 WS Rest/java
- java - Java REST API 身份验证,JWT 最佳实践
- ios - 尝试从结构访问 JSON 数据时出现索引超出范围错误
- google-app-maker - 谷歌应用程序制造商:如何保护服务器脚本中的敏感数据?