简体   繁体   English

Vue 3 - 动态道具未通过

[英]Vue 3 - Dynamic props are not passed

Component - Groups.vue组件 - Groups.vue

   <MainTable
      @next="getNextGroup"
      @prev="getPrevGroup"
      @start="getGroups"
      :columns="tableColumns"
      :data="groupsData"
    />
  </div>
</template>

<script setup>
import MainTable from "../../components/MainTable/index.vue";
import { useGroupsStore } from "../../store/groups";
import { onMounted, reactive, ref } from "vue";
const groupStore = useGroupsStore();
let groupsData = reactive([{}]);
let tableColumns = ref([
  {
    label: "Название",
    prop: "title",
  },
  {
    label: "Время",
    prop: "time",
  },
  {
    label: "Количество мест",
    prop: "count",
  },
]);
const getGroups = async () => {
  const res = await groupStore.getGroups();
  console.log("groupsData", groupsData);
};

const getNextGroup = async () => {
  const res = await groupStore.getGroupsNext();
  console.log("groupsData", groupsData);
};
const getPrevGroup = async () => {
  const res = await groupStore.getGroupsPrev();
  console.log("groupsData", groupsData);
};
</script>

And parent component- MainTable.vue和父组件- MainTable.vue

<script setup>
import { toRefs, onMounted, watch } from "vue";

const emit = defineEmits(["next", "prev", "start"]);
const props = defineProps({
  columns: Array,
  data: Array,
});

let { data: tableData } = toRefs(props);

onMounted(() => {
  emit("start");
});

const next = () => {
  emit("next");

};
const prev = () => {
  emit("prev");

};
</script>

I get the data and write it to groupsData, but the data is not updated in MainTable.我获取数据并将其写入 groupsData,但数据未在 MainTable 中更新。

I use torres and still an empty array, or just a Proxy comes.我使用 torres 并且仍然是一个空数组,或者只是一个代理来了。

tried using reactive and ref Or you need to use computed尝试使用 reactive 和 ref 或者你需要使用 computed

Why props may not change reactively?为什么道具可能不会反应性地改变?

I think the problem is you've initialised groupData as a reactive object but then you're replacing it with a non reactive object.我认为问题在于您已将groupData初始化为反应性 object,但随后您将其替换为非反应性 object。

There are a couple ways to achieve what you want.有几种方法可以实现您想要的。

  1. Use a reactive object and modify one of its properties rather than replacing the whole object:使用反应式 object 并修改其属性之一,而不是替换整个 object:
const data = reactive({ groups: [] });
data.groups = await groups store.getGroups();
  1. Use ref instead of reactive使用ref而不是reactive
const groupsData = ref([]);
groupsData.value = await groupStore.getGroups();

Please note that a ref is a Proxy object. You can refer to groupData directly in the template but to get or set its value in the script you have to use groupData.value as above.请注意, refProxy object。您可以直接在模板中引用groupData ,但要在脚本中获取或设置其值,您必须使用groupData.value如上所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM