javascript - 在 Vue.js 中传递参数
问题描述
我试图在我的 Vue.js 项目中传递一个参数,但没有运气。目标是当我从列表中选择一个用户时,它应该路由到该特定用户的个人资料作为 localhost:61601/Profile/"lastName"
这是我单击用户名旁边的方法:
editItem(lastName) {
this.$router.push({ name: 'GetInquiry', params: { lastName: lastName } })
this.$http.get('http://localhost:61601/api/' + 'GetInquiry/' + { lastName : lastName })
},
async GetAllInquiries() {
this.loading = true
try {
this.records = await api.GetAllInquiries()
} finally {
this.loading = false
}
},
截至目前,我只是传递姓氏,但最终,它将传递一个唯一的 id。
这是测试个人资料页面,在这里我只是想获取要显示的用户信息以测试页面。因此,如果一切正常,用户 firstName 应该显示:
<template>
<div class="Profile">
<div class="container">
<div class="row">
<template slot="items" slot-scope="records">
<h1> Student Name: {{ records.items.firstName }}</h1>
</template>
</div>
</div>
<script>
import api from '../../store/api.js'
export default {
data() {
return {
records: {},
}
},
async created() {
this.GetInquiriesByUser()
},
methods: {
async GetInquiriesByUser() {
this.loading = true
try {
this.records = await api.GetInquiriesByUser()
} finally {
this.loading = false
}
},
}
}
</script>
这是我的 routes.js
{
path: '/Profile/:lastName',
name: 'GetInquiry',
component: Profile
}
当我在 chrome 上打开开发工具时,我得到 localhost:61601/api/GetInquiry/[object%20Object]
我在后端使用 .netcore api,它得到了预期的结果,只是似乎无法在我的前端得到它。如果有人可以帮助我并指出我正确的方向,那就太棒了。如果有人需要更多详细信息,请告诉我。
解决方案
您在 vue-resource 上传递一个对象而不是值。
只需将姓氏直接传递到路由中,它应该可以正常工作。
this.$http.get(`http://localhost:61601/api/GetInquiry/${lastName}`);
推荐阅读
- java - 将 vararg 参数传递给 Kotlin 中的另一个函数时出现编译时错误
- mysql - 如何编写 MySQL sql 查询?
- datetime - 如何在容器中注入 Docker 容器构建时间戳?
- ios - 如何将CALayer放在视频数据输出的CMSampleBuffer上?
- vue.js - 返回Vue中静态资源(png)的路径
- android - DFU模式下的蓝牙:某些设备的连接总是失败
- node.js - 在回调函数中更改全局变量值
- css - 在 TYPO3 中使用自己的自定义 css
- android - 如果 Oreo 上的屏幕关闭,我的 WebView 不会呈现 HTML 页面
- python - Pytorch 张量 - 如何通过特定张量获取索引