简体   繁体   English

从组件中检测它是否在某个 DOM 父级中

[英]Detect from component if it is within certain DOM parent

I currently have a Vue.js application in which I have used my own component.我目前有一个 Vue.js 应用程序,其中我使用了自己的组件。 I want HTML DOM children components to be able know if they are contained in my component or just in the application itself.我希望 HTML DOM 子组件能够知道它们是包含在我的组件中还是仅包含在应用程序本身中。 What would the best way to do it?最好的方法是什么? I tried it using props, but as it is all contained within another application that does not seem possible.我尝试使用道具,但因为它都包含在另一个似乎不可能的应用程序中。

example: I want the si-button to be able to determine that it is contained in a si-dialog.示例:我希望 si 按钮能够确定它包含在 si 对话框中。 How do I pass that information to the si-button?如何将该信息传递给 si 按钮?

<si-dialog ref="siDialogChildren" name="children">
  <div>
    <h1>Hello children dialog!</h1>
    <p>Click outside the dialog to close this dialog.</p>
  </div>
  <si-button id="test" type="custom" :on-click="buttonPress">
    Click
  </si-button>
</si-dialog>

Yours sincerely,此致,

Mirijam米里詹

You might want to use $parent as specified in the Vue documentation.您可能希望使用 Vue 文档中指定的$parent For example, you can get the name of the parent component by accessing to its "$options.name" property.例如,您可以通过访问其“$options.name”属性来获取父组件的名称。

Example:例子:

App.vue应用程序.vue

<template>
  <div id="app">
    <ParentComponent>
      <ChildComponent />
    </ParentComponent>
  </div>
</template>

<script>
import ParentComponent from './components/ParentComponent'
import ChildComponent from "./components/ChildComponent";

export default {
  name: 'App',
  components: {
    ParentComponent,
    ChildComponent
  }
}
</script>

<style>
</style>

ParentComponent.vue父组件.vue

<template>
  <div>
    <p>I'm a parent component.</p>
    <slot></slot>
  </div>
</template>

<script>

export default {
  name: "ParentComponent",
  props: {},
  computed: {}
};
</script>

<style scoped>
</style>

ChildComponent.vue子组件.vue

<template>
  <div>
    <p>Parent name: {{componentName}}</p>
    <p>I'm inside a ParentComponent? {{componentName === "ParentComponent" ? "Yes!" : "No."}}</p>
  </div>
</template>

<script>
export default {
  name: "ChildComponent",
  computed: {
    componentName() {
        // We get the parent name from the $options.name property of our parent 
        // component
        return this.$parent.$options.name
    }
  }
};
</script>

<style scoped>
</style>

NOTE You can also access to other properties on the parent component by using the $parent property.注意您还可以使用$parent属性访问父组件上的其他属性。 Try it out!试试看!

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

相关问题 调用从事件处理程序中的父组件传递的回调,然后将变量重新分配给某个值 - Invoke callback passed down from parent component within an event handler, then reassign variable to a certain value 从淘汰组件中引用DOM元素 - Referencing DOM elements from within a Knockout Component 从父组件构建组件的新实例并将其注入DOM? -灰烬 - Build new instance of component from parent component and inject it into DOM? - Ember 如何在 React 中将 dom 元素从子组件移动到父组件? - How to move a dom element from a child component to a parent component in React? 检测节点何时被删除(或由于父节点而从DOM中删除) - Detect when a node is deleted (or removed from the DOM because a parent was) 在 animation 更新上从父组件更新子组件 DOM - Update child component DOM from parent on animation update 从反应中的父dom节点中删除子组件 - Remove a child component from parent dom node in react 从子组件中反应访问父节点的dom节点 - React access parent's dom node from child component React-Native:从父组件调用组件内的 function - React-Native: Calling a function within a component from parent component in 从VueJS的父组件中引用子组件的索引 - Referencing the index of a child component from within a parent component in VueJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM