我的问题是,当我更改currentTable时,crud-table组件没有刷新。当我在currentTable = 'doctor'或其他位置分配created ()时。它有效,但不在这里。为什么?

<template>
  <div id="adminpanel">
    <div id="tabs">
      <button
        v-for="tableName in tables"
        :key="tableName"
        @click="switchTo(tableName)"
        >{{ tableName }}
      </button>

      <crud-table :tableName="currentTable"/>
    </div>
  </div>
</template>

<script>
import CrudTable from './CrudTable.vue'

export default {
  name: 'AdminPanel',
  data () {
    return {
      tables: ['user', 'doctor'],
      currentTable: 'user'
    }
  },
  methods: {
    switchTo: function (tableName) {
      this.currentTable = tableName
    }
  },
  components: {
    CrudTable
  }
}
</script>


编辑:
它不是重复的,因为我丝毫没有拼错道具名称。当我设置为不同的初始值时,它就可以工作。

这是我的CrudTable组件:

<template>
  <table id="crudtable">
      <tr>
        <th>Akcje</th>
        <th v-for="h in header" :key="h">
          {{ h }}
        </th>
      </tr>
      <tr v-for="row in rows" :key="row.id">
        <td>
          <span class="action edit">E</span>
          <span @click="save(visible(row))" class="action save">S</span>
          <span class="action delete">D</span>
        </td>
        <td v-for="cell in ordered(visible(row))" :key="cell.name">
          <input v-model="cell.value" type="text"/>
        </td>
      </tr>
  </table>
</template>

<script>
import {HTTP} from '../http-common'
import {popUp} from '../utils'

export default {
  name: 'CrudTable',
  props: ['tableName'],
  data () {
    return {
      rows: [],
      header: [],
      chunkSize: 10
    }
  },
  computed: {
    endpoint: function () {
      return `/api/${this.tableName}`
    }
  },
  methods: {
    visible: function (row) {
      let ret = {}
      for (let prop in row) {
        if (!prop.startsWith('_')) {
          ret[prop] = row[prop]
        }
      }
      return ret
    },
    ordered: function (row) {
      let ret = []
      for (let col of this.header) {
        ret.push({name: col, value: row[col]})
      }
      return ret
    },
    fillTable: function () {
      let self = this
      let link = `${this.endpoint}/?page=0&size=100`
      HTTP.get(link)
        .then(function (response) {
          self.rows = response.data
        })
        .catch(function (err) {
          console.log(err.response)
        })
    },
    fillHeader: function () {
      let self = this
      HTTP.get(self.endpoint)
        .then(function (response) {
          self.header = Object.keys(response.data)
        })
        .catch(function (err) {
          console.log(err.response)
        })
    },
    save: function (row) {
      HTTP.patch(this.endpoint, row)
        .then(function (response) {
          popUp('ok', `Zapisano obiekt o id ${row.id}`)
        })
        .catch(function (err) {
          console.log(err.response)
        })
    }
  },
  beforeMount () {
    this.fillTable()
    this.fillHeader()
  }
}
</script>

最佳答案

如果我理解正确,您想知道为什么CrudTable.fillTable()发生变化(取决于CrudTable.endpoint)时为什么不调用CrudTable.tableName。问题是fillTable()只是一种方法,不是被动的。 fillTable()仅在调用时评估endpoint

如果要在fillTable()更改时调用endpoint,则需要在其上添加watcher

watch: {
  endpoint(value) { // <-- called when endpoint computed prop changes
    this.fillTable(value);
  }
},
methods: {
  fillTable(endpoint) {
     // http.get(`${endpoint}/users`)
  }
}


demo

关于javascript - 组件不会在属性更改时刷新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52194767/

10-09 20:34