【发布时间】:2019-12-02 18:21:01
【问题描述】:
我有这个从 API 获取数据并将其呈现到表中的 html 文件,它从那里成功地工作。
我正在尝试将 html 文件转换为 vue,但我遇到了一些问题,虽然 vue 可以从 API 获取数据,但它根本没有在网页上显示表格(尽管我已经导出了 JSONTable.vue 并将其导入到 App.vue 以便 App.vue 可以访问 JSONTable.vue 页面)
从我的 html 文件中,我使用了成功绘制表格的这两个外部资源:
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
但是我在标签中用这个替换了它,因为 vue 抛出模板错误,它修复了模板问题:
import JQuery from 'jquery'
let $ = JQuery
如何在 vue 中使用上面的外部样式表和脚本并允许我摆脱“import JQuery”(因为如果我删除 import JQuery,它会说 $ 未定义)?
我的 vue 网站:
<template>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <!-- styling the table with an external css file -->
<body>
<div class="container">
<div class="table-responsive">
<h1>JSON data to HTML table</h1>
<br />
<table class="table table-bordered table-striped" id="tracerouteTable">
<tr>
<th>id</th>
<th>name</th>
<th>salary</th>
<th>age</th>
<th>image</th>
</tr>
</table>
</div>
</div>
</body>
</head>
</template>
<script>
export default {
name: 'JSONTable'
}
import JQuery from 'jquery'
let $ = JQuery
$(document).ready(function(){
$.getJSON("http://dummy.restapiexample.com/api/v1/employees", function(data){
var employeeData = '';
console.log(data);
$.each(data, function(key, value){
employeeData += '<tr>';
employeeData += '<td>'+value.id+'</td>';
employeeData += '<td>'+value.employee_name+'</td>';
employeeData += '<td>'+value.employee_salary+'</td>';
employeeData += '<td>'+value.employee_age+'</td>';
employeeData += '<td>'+value.profile_image+'</td>';
employeeData += '<tr>';
});
$('#tracerouteTable').append(employeeData);
});
});
</script>
【问题讨论】:
标签: jquery html css json vue.js