vue.js获取从选择到Axios帖子的ID并显示另一个选择
可能这是一个非常愚蠢的问题,我是vue.js和javascript的新手,所以如果问题无法正确解释或答案很简单,请原谅我 我想从一个选择中获取 id ,然后将ID放入另一个API中,并在下一个选择中显示模型,列表在我执行刚性时可以很好地工作。 但是我不知道如何将ID传递/粘贴到异步安装的Axios函数“ API/SD/Model/Get”,ID)“
这是我的模板
<main class="home-page">
<select v-model="selectedMark">
<option v-for="model in items" :key="model.id">{{ model.name.value }}</option>
</select><br><br>
<select v-model="selectedModel">
<option v-for="make in info" :key="make.id" >{{ make.name.value }}</option>
</select><br><br>
<button type="submit" class="conectButton">Połącz</button>
<div class="titleCarMain"><p class="titleCar">{{selectedMark}}</p><p class="titleComponent">{{selectedModel}}</p></div>
</main>
这是脚本
import axios from 'axios';
export default {
name: "App",
data() {
return {
info: null,
items: [],
selectedMark: null,
selectedModel: null,
};
},
async mounted() {
axios
.get("http://local/api/sd/make/get")
.then(response => { this.items = response.data.data.items });
const id = { id: this.selectedMark } //dont work
await axios
.post("http://local/api/sd/model/get", id) //dont work
.then(response => { this.info = response.data.data.items });
},
};
有什么好灵魂可以帮助这个新手? (如果也能解释解决方案的原因,请理解,将真是太神奇了!!)
谢谢!
Probably this is a very stupid question, i'm new in vue.js and javascript, so please forgive me if the question is not properly explained or the answer is simple... I have a problem
I wanted to get the id from one select, and put that id into the other api and display the models in the next select, the listings work beautifully as I do the rigid.
But I don't know how to pass/paste the id into the async mounted axios function "api/sd/model/get", id)"
This is my template
<main class="home-page">
<select v-model="selectedMark">
<option v-for="model in items" :key="model.id">{{ model.name.value }}</option>
</select><br><br>
<select v-model="selectedModel">
<option v-for="make in info" :key="make.id" >{{ make.name.value }}</option>
</select><br><br>
<button type="submit" class="conectButton">Połącz</button>
<div class="titleCarMain"><p class="titleCar">{{selectedMark}}</p><p class="titleComponent">{{selectedModel}}</p></div>
</main>
And this is script
import axios from 'axios';
export default {
name: "App",
data() {
return {
info: null,
items: [],
selectedMark: null,
selectedModel: null,
};
},
async mounted() {
axios
.get("http://local/api/sd/make/get")
.then(response => { this.items = response.data.data.items });
const id = { id: this.selectedMark } //dont work
await axios
.post("http://local/api/sd/model/get", id) //dont work
.then(response => { this.info = response.data.data.items });
},
};
Any good soul who can help this newbie? (if can explain the why of the solution as well, for understand, will be amazing!!)
Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它现在不起作用,因为当组件安装 Selectedmark 等于null时,您在开始时定义它。如果您想从“ http:// local/api/sd/model/ge”中获取数据,则已安装挂钩中的端点,您必须建立 selectedmark 以某种方式 - 例如第一个项目的ID。
但是,如果您每次选择其他项目时都需要获取新数据,则必须创建一个将在更改事件上触发的函数。
请记住要添加:值属性属于选项标签
示例使用JSON占位符免费REST API(只是为了向您展示其工作原理)
It does not work right now, because when the component is mounted selectedMark is equal to null as you define it at the beginning. If you want to fetch data from "http://local/api/sd/model/ge" endpoint in the mounted hook you have to establish selectedMark somehow - e.g. the id of the first item.
But if you want to fetch new data every time you select a different item you have to create a function that will be triggered on the change event.
Remember to add :value attribute to option tags
Working example BUT with the use of JSON placeholder FREE REST API (just to be able to show you how it works)
您可以使用
@change
事件,并使用方法
挂钩如下。You can use
@change
event and useMethods
hook as below.