如何使用Vuejs单击更改两个开关数据

How to change the two switch data with click with Vuejs

谁能教我点击时交换两个不同数据的方法

比如我们有一个数据

data() {
        return {

          data_one: [
            {
              name: "Simo",
              age: "32"
            }
          ],

          data_two: [
            {
              name: "Lisa",
              age: "25"
            }
          ],

        }
      },

然后我想把这个数据放在 v-for 上,我想添加两个按钮来交换 data_one 和 data_two 并默认显示 data_one。

<button @click="change_to_data_one">Data 1<button>
<button @click="change_to_data_two">Data 2<button>

<li v-for="item in data_one">{{item.name}} - {{item.age}}<li>

尝试使用计算 属性:

new Vue({
  el: "#demo",
  data() {
    return {
      data_one: [{name: "Simo", age: "32"}],
      data_two: [{name: "Lisa", age: "25"}],
      selected: "data_one"
    }
  },
  computed: {
    showData(){
      return this[this.selected]
    }
  },
  methods: {
    swap(val) {
      this.selected = val
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <button @click="swap('data_one')">Data 1</button>
  <button @click="swap('data_two')">Data 2</button>
  <ul>
    <li v-for="(item, i) in showData" :key="i">{{item.name}} - {{item.age}}</li>
  </ul>
</div>