javascript - 在未呈现 bootstrap-vue 的 vue.js CLI 应用程序中
问题描述
我有一个使用 vue-CLI 制作的应用程序,包括 bootstrap-vue。在我的App.vue中,我使用 axios 来获取一些示例 JSON 数据。使用这个带有 html-tags (UL, LI) 的简单代码,可以显示数据:
<p v-if="loading">Loading...</p>
<ul v-else>
<li v-for="(value, key) in post" :key="key">
{{ key }} : {{ value }}
</li>
</ul>
使用代码,使用引导标签和相同的数据,数据没有显示在列表项中,似乎是一个损坏的渲染。
<b-list-group >
<b-list-group-item
href="#"
class="flex-column align-items-start"
v-for="result in post"
v-bind:key="result.userId"
>
<div class="d-flex w-100 justify-content-between">
<h6 class="mb-1">{{ result.title }}</h6>
<small>{{ result.id }}</small>
</div>
<p class="mb-1">{{ result.body }}</p>
</b-list-group-item>
</b-list-group>
生成了 html 代码,但标签之间没有数据。element-Inspector (chrome) 显示...
有什么问题?有人有想法吗?请帮忙。
这是我的main.js:
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import JQuery from 'jquery'
let $ = JQuery
import BootstrapVue from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
Vue.use(BootstrapVue)
Vue.use(VueRouter);
Vue.prototype.$log = console.log;
new Vue({
el: '#app',
render: h => h(App)
})
这是我在Vue.app中的脚本:
<script>
const productAPI =
"http://www.m.myapp2go.de/services/getnewsliste.php?typ=news&id=10024";
import axios from "axios";
import Counter from "./Counter";
export default {
name: "app",
data() {
return {
msg: "Vue.js Template Test App",
loading: false,
post: null,
results: null,
error: ""
};
},
components: {
"my-counter": Counter
},
created: function() {
this.loading = true;
axios
.get("https://jsonplaceholder.typicode.com/posts/1")
.then(res => {
this.loading = false;
this.post = res.data;
})
.catch(err => {
this.loading = false;
this.error = err;
});
},
methods: {
log(message) {
console.log(message);
}
}
};
</script>
解决方案
那是因为 apihttps://jsonplaceholder.typicode.com/posts/1
返回一个对象而不是数组。如果您改为调用https://jsonplaceholder.typicode.com/posts
,您将检索对象数组并且您的代码应该可以工作。
但是,如果您调用的 API 点是您想要的,这意味着您正在迭代对象键。您需要将 API 的结果插入到数组中或删除v-for
并post
直接使用。
axios
.get("https://jsonplaceholder.typicode.com/posts/1")
.then(res => {
this.loading = false;
this.post = [res.data];
})
.catch(err => {
this.loading = false;
this.error = err;
});
或者
<b-list-group >
<b-list-group-item
href="#"
class="flex-column align-items-start"
>
<div class="d-flex w-100 justify-content-between">
<h6 class="mb-1">{{ post.title }}</h6>
<small>{{ post.id }}</small>
</div>
<p class="mb-1">{{ post.body }}</p>
</b-list-group-item>
</b-list-group>
推荐阅读
- git-bash - ~/bin 在 git bash 中进入我的路径的过程是什么?
- javascript - 如何删除选择元素中大于所选选项的选项?
- docusignapi - 带有私钥的 JWT 签名编码。出现“RsaUsingSha alg 期望密钥为非对称算法类型”的错误。
- swift - 从服务器获取 JSON 响应时获取进度指示器的数据更新?斯威夫特 NSURLSession
- spring-retry - 在数据库中保持重试次数 Spring Retry
- .net-core - 无法跟踪 Entity Framework Core 实例,因为另一个实例
- r - 如何将文本与我的 ggplot 分开?
- javascript - 显示 Json 数据
- java - 如何在android studio中添加用于登录的按钮功能
- git - 如何显示在 git 工作树中修改的分支上更改的文件