templates - 无法使用 vue js 在模板中处理 json 数据
问题描述
我无法正确传递模板中的 json 数据。我以任何我找不到的方式尝试。如果你想把所有东西都放在一个片段中,我可以把它放进去。先感谢您。
我的 HTML:
<div id="app">
<cols :pops="pops"></cols>
</div>
我的应用程序:
var main = new Vue({
el: '#app',
data:{
pops: [],
},
mounted: function () {
var self = this;
$.ajax({
url: '../assets/data/pop.json',
method: 'GET',
success: function (data) {
self.pops = data;
},
error: function (error) {
console.log(error);
}
});
},
在同一个文件中(应用程序)
Vue.component('cols', {
template:
`<li v-for="pop in pops"><a href="#"> <b>{{ pop.Commune }}</b><span>{{ pop.Population }}</span><div :style="{width: ( pop.Population/520504 * 100) + '%'}"></div></a></li>`,
});
错误
属性或方法“pops”未在实例上定义,但在渲染期间被引用。
“数据”选项应该是一个在组件定义中返回每个实例值的函数。
解决方案
您实际上并没有将数据属性传递给组件(和其他组件)。
在您的 HTML 中尝试这样做:
<div id="app">
<headermain></headermain>
<search></search>
<cols :pops="pops"></cols>
</div>
在 cols 组件的 pops 属性中注入 data.pops。
推荐阅读
- php - AJAX 请求 405 错误发送 JWT 标头
- php - 需要 ext-xml * -> 您的系统中缺少它。安装或启用 PHP 的 xml 扩展
- netcat - 为什么 netcat -k 不起作用并且无法保持连接?
- javascript - 试图查看我的文本文件中是否包含对象变量
- python - 如何计算每个月的总数并创建新的excel
- typescript - JSDoc 将泛型类型分配给一个值
- python - 在没有仪表板的情况下监控 Dask 内存使用情况
- javascript - 在数组 map() 方法中使用外部对象
- python - 查找两个大型 CSV 文件之间的交集并添加额外列的函数
- docker - Docker on Windows Server 2019: failed to register layer: Cannot create layer with missing parent