【发布时间】:2021-12-15 21:43:38
【问题描述】:
我正在用一个 symfony 项目学习 vue,但遇到了问题。
我正在尝试使用 ajax 调用的结果加载动态组件以进行滚动分页,但无法使其正常工作。
这是我的代码:
Gamecard.vue
<script>
export default {
name: 'gamecard',
props: ['name', 'description', 'tourneyscount'],
data() {
return {
}
}
}
</script>
game-card.js
import Vue from 'vue';
import Gamecard from './../../components/Gamecard'
new Vue({
el: '#game-card',
components: {
Gamecard,
'gamecard': () => import('./../../components/Gamecard')
}
})
在这种情况下,Gamecard 用于初始组件,'gamecard': () ... 用于动态组件
game-cards-container.html.twig 这有一个充卡循环
{% for game in games %}
{% include 'partials/cards/game-card.html.twig' %}
{% endfor %}
game-card.html.twig
<gamecard
name="{{ game.getName() }}"
description="{{ game.getDescription() }}"
tourneyscount="{{ (game.getTourneys() | length > 0) ? game.getTourneys() | length : '0' }} {{ 'active tourneys' }}"
></gamecard>
我的控制器响应
if ($request->isXMLHttpRequest()) {
return new JsonResponse(
[
'code' => Response::HTTP_OK,
'cards' => $this->render('partials/containers/game-cards-container.html.twig', [
'async' => true,
'games' => $gameList
])->getContent()
]
);
}
return $this->render('home/index.html.twig', [
'games' => $gameList
]);
和 ajax 调用:
const url = window.location.hostname + '/' + page;
fetch('?page=' + page, {
headers: {
"X-Requested-With": "XMLHttpRequest"
}
})
.then(response => response.json())
.then(function (response) {
if (response.code === 200) {
console.log(response.cards);
document.getElementById('game-card').innerHTML += response.cards
canLoad = true;
} else {
canLoad = false;
}
});
有人可以帮助我吗?
谢谢!
【问题讨论】:
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
标签: vue.js vue-component