- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个相当简单的 Vue 实例,它从 REST 端点检索对象并将它们显示在页面上。几乎一切都正常工作(例如过滤),除非我通过向服务请求新的结果“页面”来将新对象添加到结果集中。此操作与其他操作的区别在于,我尝试将服务中的新结果添加到现有结果集中。由于该服务仅返回所请求的结果“页面”,因此我无法完全替换该数组,而是必须将其添加到 Vue 实例中的现有结果集中。这是有问题的代码:
// these come from an external server-side process rendered in a separate place so are defined outside of the Vue context
let searchTags = ["tag1", "tag2", "etc"];
let productsBaseSearchTag = "a/b/c";
Vue.mixin({
methods: {
doSearchStuff: function(response, data) {
this.searchResponse = response;
this.resultCount = response.total; // this doesn't seem to be working consistently either so I'm doing it with jQuery
jQuery(".search-page-sort-results .num").text(this.resultCount);
if(this.currentPage > 1) {
this.searchResults = this.searchResults.concat(data.hits);
} else {
this.searchResults = data.hits;
}
this.facetCount = data.facets.length;
}
}
});
new Vue({
el: '#v-search-page-results',
data: {
searchResponse: {},
searchResults: [],
facetHeaders: searchTags,
returnedFacetHeaders: undefined,
currentPage: 1,
hitsPerPage: 12,
searchTerm: "",
statusMessage: "",
sortProperty: "",
displayedFilters: {},
predicateList: [],
facetCount: 0,
resultCount: 0,
showLoading: true
},
created: function() {
this.predicateList = searchTags;
this.getSearchResults(false);
},
computed: {
pdpPaths: function() {
return this.searchResults.map(function(item) {
let catalogPath = item.path;
return decodeURIComponent(pdpPath) + catalogPath.replace(decodeURIComponent(catalogRoot), "").replace(/\//g, ".");
});
},
summaries: function() {
return this.searchResults.map(function(item) {
let summary = item.properties.summary;
if (summary.length >= 120) {
summary = summary.substring(0, 120);
summary = summary.substring(0, summary.lastIndexOf(" ")) + "...";
}
return summary;
});
},
assetAbsoluteURLs: function() {
return this.searchResults.map(function(item) {
let escapedUrl = item.path.replace(/\s/g, "%20");
return location.origin + escapedUrl;
});
},
canClearFilters: function() {
return this.predicateList !== searchTags;
},
moreResults: function() {
if(this.searchResponse) {
let resultCount = this.resultCount;
let totalLoadedResults = this.hitsPerPage * this.currentPage;
if(totalLoadedResults < resultCount) {
return true;
}
}
return false;
}
},
methods: {
loadMoreResults: function() {
this.currentPage += 1;
this.getSearchResults();
},
clearSearchTerm: function() {
this.searchTerm = "";
this.submitSearchTerm();
},
getSearchFilters: function() {
if(this.predicateList.length > 0) {
return this.predicateList;
} else {
this.predicateList = searchTags;
return this.predicateList;
}
},
getSearchResults: function(xhrAsync=true) {
let query = this.buildQuery();
var jqXhr = $.ajax({
url: query,
async: xhrAsync, // search breaks if we don't set this to false on the initial page load search
context: this
});
if(!this.returnedFacetHeaders) {
jqXhr.done(function(response) {
let data = response;
this.doSearchStuff(response, data);
this.returnedFacetHeaders = data.facetHeaders;
if(queryParams.q) {
this.searchTerm = decodeURIComponent(queryParams.q);
}
if(queryParams.tags) {
this.predicateList = queryParams.tags;
}
}).done(function () {
this.getSearchResults();
});
} else {
jqXhr.done(function(response) {
let data = response;
this.doSearchStuff(response, data);
});
}
},
submitSearchTerm: function() {
this.resetSearch();
},
resetSearch: function() {
this.currentPage = 1;
this.getSearchResults();
},
buildQuery: function() {
let offset = (this.currentPage - 1) * this.hitsPerPage;
query = "..."; // not relevant
return query;
}
}
});
代码中发生的事情比这多得多,但这是与数组相关的部分。它不会在此 block 之外更新。这是使用 v-for 迭代数组的相应标记:
<div id="v-search-page-results" class="search-page-container _clearfix">
<div class="search-page-wrapper">
<div class="search-page-facets-wrapper">
<div class="search-page-sort-results">
<span v-cloak class="num">{{ resultCount }}</span> results
</div>
</div>
<div class="search-page-container">
<!-- facet stuff here -->
<div class="search-page-results-container">
<div class="search-page-results-wrapper">
<div v-for="(result, index) in searchResults" class="search-page-results-item" v-bind:key="result.id">
<div class="search-page-image-container">
<img v-cloak :alt="result.title" :src="result.properties.thumbnailPath" class="search-page-image">
</div>
<div class="search-page-results-content">
<a v-cloak :href="pdpPaths[index] + '.html'" class="title" title="">{{ result.properties.productTitle }}</a>
<div v-cloak class="description" v-html="summaries[index]"></div>
</div>
</div>
</div>
<div class="search-button-top-nav">
<div v-show="moreResults" class="button-wrapper load-more-container load-more-wrapper">
<div class="button-wrapper-2">
<div class="button-container">
<a @click="loadMoreResults" class="btn -primary load-more">Load More</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
最后,这是我们尝试迭代的数据示例:
"hits": [
{
"id": 1,
"title": "product1",
"path": "/path/to/product1",
"properties": {
"thumbnailPath": "/products/images/product1.jpg",
"productTitle": "Product 1",
"summary": "<p>This is product 1.</p>\r\n"
}
},
{
"id": 2,
"title": "product2",
"path": "/path/to/product2",
"properties": {
"thumbnailPath": "/products/images/product2.jpg",
"productTitle": "Product 2",
"summary": "<p>This is product 2.</p>\r\n"
}
}
]
我尝试过使用 Vue.set,我尝试过迭代响应对象并一次将一个对象推送到数组,我尝试过使用展开运算符等。在代码中,数组正在被添加成功,但是Vue不会显示新的结果。如果我用新对象覆盖当前数组中的现有对象,它会正确显示,所以并不是说 DOM 根本没有更新,它只是更新为仅显示前 X 个结果,永远不会比那更多的。看起来应该可行,但事实并非如此。
最佳答案
您不应该将 jQuery 与 Vue 混合使用 - 您最终会因直接操作 DOM 而失去 react 性,或者经历一些其他不良影响。
new Vue({
el: '#app',
data: {
searchResults: [],
},
methods: {
async getNewPost(next) {
this.searchResults.push(await this.getSearchResult(next))
},
getSearchResult(next) {
return fetch(`https://jsonplaceholder.typicode.com/posts/${next}`)
.then(response => response.json())
.then(json => {
return json
})
}
},
async mounted() {
this.searchResults = [await this.getSearchResult(this.searchResults.length + 1)]
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button @click="getNewPost(searchResults.length + 1)">Get new post</button>
<div v-for="(result, index) in searchResults" class="search-page-results-item" v-bind:key="result.id">
<div class="search-page-results-content">
<h3>{{index + 1}}. {{result.title}}</h3>
<div v-cloak class="description">{{result.body}}</div>
</div>
</div>
</div>
上面的代码片段从模型 JSON API 获取数据,按下按钮即可获取下一项。
您可以看到,在 getNewPost()
方法中,我没有直接推送项目到 searchResults
数据属性中,而是创建一个临时项目变量,然后更改整个数组。
关于javascript - 从ajax请求动态连接Vue中的数组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58459468/
我知道这个问题可能已经被问过,但我检查了所有这些,我认为我的情况有所不同(请友善)。所以我有两个数据集,第一个是测试数据集,第二个是我保存在数据框中的预测(预测值,这就是没有数据列的原因)。我想合并两
在 .loc 方法的帮助下,我根据同一数据框中另一列中的值来识别 Panda 数据框中某一列中的值。 下面给出了代码片段供您引用: var1 = output_df['Player'].loc[out
当我在 Windows 中使用 WinSCP 通过 Ubuntu 连接到 VMware 时,它提示: The server rejected SFTP connection, but it lis
我正在开发一个使用 xml web 服务的 android 应用程序。在 wi-fi 网络中连接时工作正常,但在 3G 网络中连接时失败(未找到 http 404)。 这不仅仅发生在设备中。为了进行测
我有一个XIB包含我的控件的文件,加载到 Interface Builder(Snow Leopard 上的 Xcode 4.0.2)中。 文件的所有者被设置为 someClassController
我在本地计算机上管理 MySQL 数据库,并通过运行以下程序通过 C 连接到它: #include #include #include int main(int argc, char** arg
我不知道为什么每次有人访问我网站上的页面时,都会打开一个与数据库的新连接。最终我到达了大约 300 并收到错误并且页面不再加载。我认为它应该工作的方式是,我将 maxIdle 设置为 30,这意味着
希望清理 NMEA GPS 中的 .txt 文件。我当前的代码如下。 deletes = ['$GPGGA', '$GPGSA', '$GPGSV', '$PSRF156', ] searchquer
我有一个 URL、一个用户名和一个密码。我想在 C# .Net WinForms 中建立 VPN 连接。 你能告诉我从哪里开始吗?任何第三方 API? 代码示例将受到高度赞赏... 最佳答案 您可以像
有没有更好的方法将字符串 vector 转换为字符 vector ,字符串之间的终止符为零。 因此,如果我有一个包含以下字符串的 vector "test","my","string",那么我想接收一
我正在编写一个库,它不断检查 android 设备的连接,并在设备连接、断开连接或互联网连接变慢时给出回调。 https://github.com/muddassir235/connection_ch
我的操作系统:Centos 7 + CLOUDLINUX 7.7当我尝试从服务器登录Mysql时 [root@server3 ~]# Mysql -u root -h localhost -P 330
我收到错误:Puma 发现此错误:无法打开到本地主机的 TCP 连接:9200(连接被拒绝 - 连接(2)用于“本地主机”端口 9200)(Faraday::ConnectionFailed)在我的
请给我一些解决以下错误的方法。 这是一个聊天应用....代码和错误如下:: conversations_controller.rb def create if Conversation.bet
我想将两个单元格中的数据连接到一个单元格中。我还想只组合那些具有相同 ID 的单元格。 任务 ID 名称 4355.2 参与者 4355.2 领袖 4462.1 在线 4462.1 快速 4597.1
我经常需要连接 TSQL 中的字段... 使用“+”运算符时 TSQL 强制您处理的两个问题是 Data Type Precedence和 NULL 值。 使用数据类型优先级,问题是转换错误。 1)
有没有在 iPad 或 iPhone 应用程序中使用 Facebook 连接。 这个想法是登录这个应用程序,然后能够看到我的哪些 facebook 用户也在使用该应用程序及其功能。 最佳答案 是的。
我在连接或打印字符串时遇到了一个奇怪的问题。我有一个 char * ,可以将其设置为字符串文字的几个值之一。 char *myStrLiteral = NULL; ... if(blah) myS
对于以下数据 - let $x := "Yahooooo !!!! Select one number - " let $y := 1 2 3 4 5 6 7 我想得到
我正在看 UDEMY for perl 的培训视频,但是视频不清晰,看起来有错误。 培训展示了如何使用以下示例连接 2 个字符串: #!usr/bin/perl print $str = "Hi";
我是一名优秀的程序员,十分优秀!