javascript - v-on :click event Vue. js 显示用户帖子

标签 javascript vue.js vuejs2 frameworks

我是一名学生,我刚刚接触 Vue.js,所以我对它还是很陌生。现在我正在做一个项目,我从 API 获取用户名,当你点击用户时它必须显示相关的帖子,但这不起作用。当我单击带有 v-on:click 事件的按钮时。没有任何反应,甚至在控制台中也没有。所以我希望有人能帮助我解决我的问题,我将不胜感激。

主要.js :

const app = new Vue({
el: "#app",
data: {
  users: [],
  posts: [],
},
methods: {
  Showpost(id, i) {
    fetch("https://jsonplaceholder.typicode.com/posts?userId=" + id)
    .then(response =>response.json())
    .then((data) => {
      this.posts = data;
    })
  },
},
mounted() {
  fetch("https://jsonplaceholder.typicode.com/users")
    .then(response => response.json())
    .then((data) => {
      this.users = data;
    })
},
template: `
<div>

  <td v-for="user, i in users">
    <button v-on:click="Showpost(user.id, i)" >{{ user.name }}</button>
  </td>
  <h1>{{ posts.title }}</h1>
</div>
`,
});

HTML :

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<title>Users</title>
</head>

<body>
<h1>Users</h1>

<div id="app">

</div>

<script src="https://unpkg.com/vue"></script>
<script src="./main.js"></script>
</body>

</html>

json 用户:

{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
  "street": "Kulas Light",
  "suite": "Apt. 556",
  "city": "Gwenborough",
  "zipcode": "92998-3874",
  "geo": {
    "lat": "-37.3159",
    "lng": "81.1496"
  }
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
  "name": "Romaguera-Crona",
  "catchPhrase": "Multi-layered client-server neural-net",
  "bs": "harness real-time e-markets"
}
}

json 帖子:

{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

最佳答案

尝试创建一个名为 post 的属性,并通过分配 this.post=data[i] 在每次点击指定用户时更新它:

new Vue({
  el: "#app",
  data: {
    users: [],
    posts: [],
    post: ''
  },
  methods: {
    Showpost(id, i) {
      fetch("https://jsonplaceholder.typicode.com/posts?userId=" + id)
        .then(response => response.json())
        .then((data) => {
          this.post = data[i];

        })
    },
  },
  mounted() {
    fetch("https://jsonplaceholder.typicode.com/users")
      .then(response => response.json())
      .then((data) => {
        this.users = data;
      })
  },
  template: `
<div class="flex">

  <div v-for="user, i in users">
    <button class="btn" v-on:click="Showpost(user.id, i)" >{{ user.name }}</button>
  </div>
  <h1>{{ post.title }}</h1>
</div>
`,
});
.flex{
display:flex;
flex-wrap:wrap;
}
<!DOCTYPE html>
<html>

<head>
  <meta name="description" content="Vue.delete">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.1/vue.min.js"></script>
</head>

<body>
  <div id="app">

  </div>

关于javascript - v-on :click event Vue. js 显示用户帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53033111/

相关文章:

javascript - VueJs - 两个数字之间循环的最佳实践

html - Vuejs index.html <link> 标签不尊重相对路径

javascript - vue.js 组件没有显示?

javascript - 如何在vue2中的过滤列表中保留项目的引用

javascript - 转换图像应该有一些小的 url 而不是基本代码

javascript - karma : Is it possible to load JavaScript files statically before the requirejs framework?

javascript - React-Native 如何查看导入文件的变化?

javascript - 隐藏和显示功能不起作用

javascript - Nuxt.js 转换不起作用

javascript - 避免直接改变 Prop ……但这似乎是官方示例所做的?