vuejs2 - Axios 获取单一配置文件
问题描述
Q) 使用 Axios 和 Vue 传递 id 并获取“Get”请求的正确方法是什么。
我有 Vue 组件,它具有数据对象和 pId -key 的值。
我已经检查过 pId 是否有价值。
个人资料 ID:{{ pId }}
给出值 1。
data() {
return {
pId: ''
}
},
methods: {
loadProfile(){
this.status = 'Loading ....';
axios.get("/profile/${pId} ")
.then(function(response){
this.profile = response.data.profile;
})
.catch(e => {
this.errors.push(e)
})
},
init(){
console.log('Profile mounted');
EventBus.$on('creds' , key =>{
[this.pId, this.uId] = key;
})
}
mounted(){
this.init()
},
created: function() {
this.loadProfile();
}
- 当我像这样传递 pId 时:
axios.get("/profile/${pId} "
- 网址为:http: //192.168.10.101 :8000/profile/ $%7BpId%7D
这意味着 pId 是字符串而不是值。
我试过这个
axios.get("/profile " + this.pId)
没有个人资料 ID,
也尝试将 id 作为参数传递,但这不是正确的方法。
如果我硬编码个人资料 ID,我将从 Laravel 获得个人资料,
- 所以路线在 Laravel 方面是好的。
谢谢米卡。
解决方案
您必须使用``作为模板字符串。
所以正确的方法是:axios.get(`/profile/${this.pId}`)
推荐阅读
- hibernate - 在spring data jpa中使用@PrimaryKeyJoinColumn注解
- flutter - Flutter Web 字体缺失
- python - 使用 $unwind 聚合 Mongodb 数组值需要很长时间
- javascript - 如何格式化 Y 轴以仅显示自然数?
- mongodb - 无法登录或卸载 mongodb
- authentication - Tomcat 9 上的 LDAP 简单身份验证
- reactjs - 如何让反应等待从 Fetch 获取数据?
- excel - 如果单元格包含特定文本,则移动到新工作簿
- gulp - Gulp sourcemaps 仅在触发 gulp 时
- android - FIrebase-OTP 发送成功,但未在反应原生 android 中验证代码