Vuejs2 使用EventBus通过单个文件组件传递Vue js搜索过滤器功能
我有以下组成部分:Vuejs2 使用EventBus通过单个文件组件传递Vue js搜索过滤器功能,vuejs2,vue-component,v-for,computed-properties,Vuejs2,Vue Component,V For,Computed Properties,我有以下组成部分: <template> <div> <h1>Blog</h1> <TheSidebar> <SearchBlogs /> </TheSidebar> <BlogList/> </div> </template> /components/SearchBlogs.vue搜索要在blog.title和blog.d
<template>
<div>
<h1>Blog</h1>
<TheSidebar>
<SearchBlogs />
</TheSidebar>
<BlogList/>
</div>
</template>
/components/SearchBlogs.vue
搜索要在blog.title
和blog.description
上过滤的组件
/components/BlogList.vue
这里我列出了所有的博客项目
SearchBlogs.vue
<template>
<div>
<input type="text" v-model="search" @change="emitSearchValue" placeholder="search blog">
</div>
</template>
<script>
import { EventBus } from '../event-bus.js'
export default {
name: 'SearchBlogs',
data: () => {
return {
search: ''
}
},
methods: {
emitSearchValue() {
EventBus.$emit('search-value', 'this.search')
}
}
}
</script>
<template>
<div>
<div v-for="blog in filteredBlogs" :key="blog">
<BlogListItem :blog="blog" />
</div>
</div>
</template>
<script>
import BlogListItem from './BlogListItem'
import { EventBus } from '../event-bus.js'
export default {
name: 'BlogList',
components: {
BlogListItem,
},
data: () => {
return {
blogs: [],
searchvalue: ''
}
},
computed: {
filteredBlogs() {
return this.blogs.filter(blog =>
blog.name.toLowerCase().includes(
this.searchvalue.toLowerCase()
)
)
}
},
created() {
fetch('http://localhost:3000/blogs')
.then(response => {
return response.json();
})
.then(data => {
this.blogs = data;
}),
EventBus.$on('search-value', (search) => {
this.searchvalue = value;
})
}
}
</script>
从“../event bus.js”导入{EventBus}
导出默认值{
名称:'SearchBlogs',
数据:()=>{
返回{
搜索:“”
}
},
方法:{
emitSearchValue(){
EventBus.$emit('search-value','this.search')
}
}
}
BlogList.vue
<template>
<div>
<input type="text" v-model="search" @change="emitSearchValue" placeholder="search blog">
</div>
</template>
<script>
import { EventBus } from '../event-bus.js'
export default {
name: 'SearchBlogs',
data: () => {
return {
search: ''
}
},
methods: {
emitSearchValue() {
EventBus.$emit('search-value', 'this.search')
}
}
}
</script>
<template>
<div>
<div v-for="blog in filteredBlogs" :key="blog">
<BlogListItem :blog="blog" />
</div>
</div>
</template>
<script>
import BlogListItem from './BlogListItem'
import { EventBus } from '../event-bus.js'
export default {
name: 'BlogList',
components: {
BlogListItem,
},
data: () => {
return {
blogs: [],
searchvalue: ''
}
},
computed: {
filteredBlogs() {
return this.blogs.filter(blog =>
blog.name.toLowerCase().includes(
this.searchvalue.toLowerCase()
)
)
}
},
created() {
fetch('http://localhost:3000/blogs')
.then(response => {
return response.json();
})
.then(data => {
this.blogs = data;
}),
EventBus.$on('search-value', (search) => {
this.searchvalue = value;
})
}
}
</script>
从“./BlogListItem”导入BlogListItem
从“../event bus.js”导入{EventBus}
导出默认值{
名称:“博客列表”,
组成部分:{
BlogListItem,
},
数据:()=>{
返回{
博客:[],
搜索值:“”
}
},
计算:{
filteredBlogs(){
返回this.blogs.filter(blog=>
blog.name.toLowerCase()包括(
this.searchvalue.toLowerCase()
)
)
}
},
创建(){
取('http://localhost:3000/blogs')
。然后(响应=>{
返回response.json();
})
。然后(数据=>{
this.blogs=数据;
}),
EventBus.$on('search-value',(search)=>{
this.searchvalue=值;
})
}
}
在另一个页面组件博客中我注册了两个组件:
<template>
<div>
<h1>Blog</h1>
<TheSidebar>
<SearchBlogs />
</TheSidebar>
<BlogList/>
</div>
</template>
博客
有人能看到这里少了什么吗?我希望,一旦用户在搜索输入中键入内容(来自
SearchBlogs.vue
组件),它就会开始过滤和更新列表。有几个问题,但基本上计算的属性Filteredata
将如下所示:
computed: {
filteredData() {
return this.experiences.filter(
el => el.category.indexOf(this.search) > -1
);
}
}
此外,在传回其值时,在“this.search”的周围使用引号,使其成为字符串
固定沙箱
有几个问题,但基本上,计算的属性
过滤器数据将如下所示:
computed: {
filteredData() {
return this.experiences.filter(
el => el.category.indexOf(this.search) > -1
);
}
}
此外,在传回其值时,在“this.search”的周围使用引号,使其成为字符串
固定沙箱
查看我的解决方案
下面是一个解释:
您不需要使用EventBus。您可以通过v-model
、使用propvalue
并从输入中发出更新的value
与搜索组件通信
然后,您的主(列表)组件负责所有逻辑
它保持搜索状态
它保留项目和筛选项目
由于您的搜索组件非常清晰,没有数据,这意味着它几乎没有责任
如果我可以添加一些内容来帮助您理解,请提问看看我的解决方案
下面是一个解释:
您不需要使用EventBus。您可以通过v-model
、使用propvalue
并从输入中发出更新的value
与搜索组件通信
然后,您的主(列表)组件负责所有逻辑
它保持搜索状态
它保留项目和筛选项目
由于您的搜索组件非常清晰,没有数据,这意味着它几乎没有责任
如果我能添加一些东西来帮助你理解,请提问。嘿,伙计,添加代码沙盒,我会为你编辑它,并提供解决方案。@Adam Orlov这里是链接,希望你能找到答案,谢谢!您想使用EventBus吗?没有必要。可以用它来做,但是没有意义。我已经更新了我的答案。嘿,伙计,添加代码沙盒,我会用解决方案为你编辑它。@Adam Orlov这里是链接,希望你能找到答案,谢谢!您想使用EventBus吗?没有必要。我已经更新了我的答案。也可以使用el=>el.category.startsWith(this.search)
来理解这个概念。这里发生了什么事?这是一个缩短的箭头函数,相当于(el)=>{return el.category.startsWith(this.search)}
基于您的示例,我尝试添加搜索框。ToLowerCase方法很重要。我还为搜索添加了第二个EventBus,这是否可取?这里有一个到imo的链接,在主应用程序中使用事件总线是不好的做法(从全局插件中暴露的事件总线是可以的,例如:$modal.open()等),而不是在组件上使用vues@eventName=“”
将其与this一起发射。$emit('eventName',this.theValue)
也可以使用el.category.startsWith(this.search)
只是为了理解这个概念。这里发生了什么事?这是一个缩短的箭头函数,相当于(el)=>{return el.category.startsWith(this.search)}
基于您的示例,我尝试添加搜索框。ToLowerCase方法很重要。我还为搜索添加了第二个EventBus,这是否可取?这里有一个到imo的链接,在主应用程序中使用事件总线是不好的做法(从全局插件中暴露的事件总线是好的,例如:$modal.open()等),而不是在组件上使用vues@eventName=“”
,并使用this.$emit('eventName',this.theValue)
谢谢您的解释!同样的原理也适用于我的复选框。如果我能做到这一点,我必须在列表组件上结合搜索和复选框组件,它们应该同时工作。一般来说,应该使用什么或建议什么。有没有EventBus?谢谢你的解释,非常清楚。根据您的示例,我尝试添加复选框。你能明白为什么它不起作用吗?这里是链接到。Thanks@meez1.您的筛选器逻辑不正确。2.您的值:prop
已注册为字符串。它必须是数组。在这里⏩ 我有个问题。要构建布局和路线,我有