【发布时间】:2018-09-30 23:00:27
【问题描述】:
我的父组件从 api 获取一个字符串数组的数据,然后将其传递给子组件。在子组件中,我将来自父组件的数据显示为下拉列表,当我从下拉列表中选择特定项目时,我希望它设置特定变量。 我使用了 $emit 和 $event,如文档中所示,但它不起作用。请查看我的代码并告诉我哪里出错了。
父组件App.vue
<template>
<div id="app">
<nlp-vision-catalog v-bind:cataloglist="catalogs" v-on:listenClick="setcatalogselected($event)" ></nlp-vision-catalog>
</div>
</template>
<script>
import NlpVisionCatalog from './components/NlpVisionCatalog'
import axios from 'axios'
export default {
name: 'App',
components: {
NlpVisionCatalog
},
data (){
return {
catalogs :[],
catalog_selected : ""
}
},
methods:{
fetchcatalogs(){
axios.get("http://localhost:5000/clients")
.then((resp)=>{this.catalogs.push.apply(this.catalogs,
resp.data.response.results.client_name);
}).catch((err)=>{
console.log(err);
})
},
setcatalogselected(catalog){
this.catalog_selected = catalog;
)}
},
created(){
this.fetchcatalogs()
}
}
</script>
<style></style>
我的子组件是 NlpVisionCatalog.vue
enter code here
<template>
<div>
<h3>Select Catalog</h3>
<select>
<option v-for="item in cataloglist">
<p v-on:click="emitbackthecatalog(item)"> {{ item }} </p>
</option>
</select>
</div>
</template>
<script>
export default{
name : 'NlpVisionCatalog',
props: ['cataloglist'],
data (){
return {
comp: ""
}
},
methods:{
emitbackthecatalog(catalog_name){
this.$emit('listenClick',catalog_name);
}
}
};
</script>
<style>
</style>
我到底哪里出错了? ps- http://localhost:5000/clients 是在我的系统上运行的 api。
【问题讨论】:
标签: javascript vue.js vuejs2 vue-component