vue.js - this.$root 在组件中是什么意思?
问题描述
我有一个主要的 App.vue 组件。在那里,我有以下代码:
export default {
data() {
return {
testVariable:false
}
},
}
</script>
<template>
<VApp :dark="testVariable:false"
<div id="app">
<RouterView :key="$route.fullPath" />
</div>
</VApp>
</template>
然后在其中一个组件中,我有以下代码:
data() {
return {
testVariable: this.$root.$children[0].testVariable,
}
},
methods: {
darkModeToggle(e) {
this.$root.$children[0].testVariable = e
},
},
问题 1) this.$root 和 this.$root.children 是什么意思?始终是this.$root
App.vue 组件(因为 App.vue 是所有组件的父级)。是this.$root.children
这个 App.vue 组件的子组件,这意味着所有其他组件都将在this.$root.children
数组中吗?
问题2)这条线是什么意思?<RouterView :key="$route.fullPath" />
. 我的意思是我们为什么要通过:key="$route.fullPath"?
解决方案
这个.$root
- 当前组件树的根 Vue 实例。https://vuejs.org/v2/api/#vm-root
this.$root.children
- 当前实例的直接子组件。https://vuejs.org/v2/api/#vm-children
this.$root 总是 App.vue 组件吗?
- 没有。
App.vue
有一个父组件,它new Vue(...)
在 main.js 中。所以这new Vue(...)
是实际的$root
this.$root.children 是这个 App.vue 组件的子组件,这意味着所有其他组件都将在 this.$root.children 数组中吗?
.children
是 DIRECT 子组件,而不是 DESCENDANT。在这种情况下,$root
只有 1 个直接孩子,即App.vue
这条线是什么意思?. 我的意思是我们为什么要通过 :key="$route.fullPath"?
推荐阅读
- java - 华为设备中的 ToastFactory 崩溃
- mysql - 如何从 Sequel Pro 连接到 docker mysql 容器
- php - 按条件加入两个表
- python - 现实世界中的 Python 封装属性
- sql - 当表2的列值发生变化时更新表1**无触发器**
- uwp - 与进程外后台任务进行桌面桥应用程序通信
- java - 处理Activity中多线程的加载状态
- javascript - AgGrid 中的下拉菜单
- typescript - Firebase rxfire 订阅检查我的代码
- python - 如何在不使用循环的情况下获取 python beautifulsoup 中的特定部分