vuejs2 - Vue - 使用 v-model 和 vuex
问题描述
我正在构建一个搜索页面,并且在使用 v-model 和 vuex 存储更新我的计算属性时遇到了困难。我正在使用这个基于 Vue 构建的wordpress 主题
在我的数据中,我有一个主要的帖子请求,它使用 3 个参数从 wordpress rest api 获取数据,我希望能够使用 v-model 设置这些参数并相应地获取帖子。
postsRequest: {
type: 'quote',
params: {
per_page: 50,
search: null,
authors: null,
tags: null
},
showLoading: true
},
然后我正在获取作者和标签列表,以便能够获取我需要在 postsRequest 参数中使用的 id。
authorsRequest: {
type: 'authors',
params: {
cq_per_page: 500,
},
},
topicsRequest: {
type: 'tags',
params: {
per_page: 100,
},
},
以下是我的计算属性和方法
computed: {
authors () {
return this.$store.getters.requestedItems(this.authorsRequest)
},
topics () {
return this.$store.getters.requestedItems(this.topicsRequest)
},
posts() {
return this.$store.getters.requestedItems(this.postsRequest)
}
},
methods: {
getAuthors() {
return this.$store.dispatch('getItems', this.authorsRequest)
},
getTopics() {
return this.$store.dispatch('getItems', this.topicsRequest)
},
getPosts() {
return this.$store.dispatch('getItems', this.postsRequest)
},
},
created() {
this.getAuthors()
this.getTopics()
this.getPosts()
},
我尝试在所有三个 v-model 属性上设置手表。查看 vue 开发人员工具,手表确实更改了 v-model 更改的 postsRequest.params,但帖子保持不变。
watch: {
"searchAuthor": function() {
this.postsRequest.params.search = null
this.postsRequest.params.authors = this.searchAuthor
this.postsRequest.params.tags = null
this.getPosts()
},
"searchTopic": function() {
this.postsRequest.params.search = null
this.postsRequest.params.authors = null
this.postsRequest.params.tags = this.searchTopic
this.getPosts()
},
"searchTerm": function() {
this.postsRequest.params.search = this.searchTerm
this.postsRequest.params.authors = null
this.postsRequest.params.tags = null
this.getPosts()
}
},
模板
<div class="advanced-search">
<input type="text"
name="searchTerm"
placeholder="Search title..."
tabindex="1"
v-model="searchTerm">
<select v-model="searchAuthor">
<option value="">All</option>
<option
v-for="author in authors"
:value="author.id"
:key="author.id">{{ author.name }}</option>
</select>
<select v-model="searchTopic">
<option value="">All</option>
<option
v-for="topic in topics"
:value="topic.id"
:key="topic.id">{{ topic.name }}</option>
</select>
</div>
<section v-if="posts">
<post-item
v-for="post in posts"
:key="post.id"
:post="post"
:postmeta=false
/>
</section>
我正在学习,非常感谢任何帮助。
编辑(完整的组件代码)
<template>
<main class="site-content">
<div class="container">
<div class="advanced-search">
<input type="text"
name="searchTerm"
placeholder="Search title..."
tabindex="1"
v-model="searchTerm">
<select v-model="searchAuthor">
<option value="">All</option>
<option
v-for="author in authors"
:value="author.id"
:key="author.id">{{ author.name }}</option>
</select>
<select v-model="searchTopic">
<option value="">All</option>
<option
v-for="topic in topics"
:value="topic.id"
:key="topic.id">{{ topic.name }}</option>
</select>
</div>
<section v-if="posts">
<post-item
v-for="post in posts"
:key="post.id"
:post="post"
:postmeta=false
/>
</section>
</div>
</main>
</template>
<script>
import PostItem from '@/components/template-parts/PostItem'
export default {
name: 'Search',
components: {
PostItem,
},
props: {
term: {
type: String,
required: true
}
},
data() {
return {
postsRequest: {
type: 'quote',
params: {
per_page: 50,
search: null,
authors: null,
tags: null
},
showLoading: true
},
authorsRequest: {
type: 'authors',
params: {
cq_per_page: 500,
},
},
topicsRequest: {
type: 'tags',
params: {
per_page: 100,
},
},
searchTerm: '',
searchAuthor: '',
searchTopic: ''
}
},
watch: {
"searchAuthor": function() {
this.postsRequest.params.search = null
this.postsRequest.params.authors = this.searchAuthor
this.postsRequest.params.tags = null
this.getPosts()
},
"searchTopic": function() {
this.postsRequest.params.search = null
this.postsRequest.params.authors = null
this.postsRequest.params.tags = this.searchTopic
this.getPosts()
},
"searchTerm": function() {
this.postsRequest.params.search = this.searchTerm
this.postsRequest.params.authors = null
this.postsRequest.params.tags = null
this.getPosts()
}
},
computed: {
authors () {
return this.$store.getters.requestedItems(this.authorsRequest)
},
topics () {
return this.$store.getters.requestedItems(this.topicsRequest)
},
posts() {
return this.$store.getters.requestedItems(this.postsRequest)
}
},
methods: {
getAuthors() {
return this.$store.dispatch('getItems', this.authorsRequest)
},
getTopics() {
return this.$store.dispatch('getItems', this.topicsRequest)
},
getPosts() {
return this.$store.dispatch('getItems', this.postsRequest)
},
},
created() {
this.getAuthors()
this.getTopics()
this.getPosts()
},
}
</script>
解决方案
推荐阅读
- c# - 使用 OLEDB 提供程序连接到 Access 数据库崩溃(自 Office 365 Build 12430.20184 起)
- python - keep_prob: 0.8 是什么意思?
- curl - 通过 curl 向 API 发送音频数据
- python - 如果答案是随机的,我如何追踪对原始答案的响应?
- google-cloud-platform - 在 GKE 中使用 N2 机器类型 - (Google Kubernetes Engine)
- c - 我尝试在 C 中为链表编写代码
- python - Pandas 数据框计算过去 3 年同月的平均值并附加在同一数据框中
- css - Sass:选择器的占位符行为
- jenkins - .npm 目录消耗 149+ GB 的磁盘空间
- asp.net - 从不可编辑的 asp.net 后面的代码创建表