【发布时间】:2018-11-21 02:14:25
【问题描述】:
我被困在 Vue 的逻辑用法中,我有一个从 ajax 获取结果的“列表”组件,当我想添加搜索字段时出现问题,我有这样的东西:
搜索.vue
<template>
<div>
<div v-for="(result, index) in results">
<h2>{{ result.name }}</h2>
</div>
</div>
</template>
<script>
export default {
name : 'searchList',
data() {
return { results: [] }
}
created: function(){
this.goSearch();
},
methods : {
goSearch : function(){
this.results = axios.get('/search');
}
}
}
</script>
这就像一个魅力,关键是我想在搜索中添加一个输入,我做了一些研究,我发现获得它的唯一方法是使用另一个组件,但我没有想为输入创建另一个组件,所以我想做类似的事情:
index.html
<input type="text" v-model="goSearch">
<search-list></search-list>
但我面临的问题是 Vue 返回错误:
[Vue warn]: Property or method "goSearch" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
我也尝试过v-bind="goSearch",但也没有用。有什么想法可以做到这一点?
更新: 我创建了一个按钮来调用该函数:
<button @click="goSearch"></button>
函数“goSearch”现在尝试从文本框中获取值,但这也不起作用。
【问题讨论】:
-
@Core972 我读过,但据我所知,prop 仅适用于子组件到父组件,不是吗?
-
您的
created和methods键不在导出的对象中。你没有收到解析错误吗?
标签: javascript vue.js