组件无法访问以$开头的数据

发布于 2025-01-31 06:10:27 字数 889 浏览 0 评论 0原文

我制作了一些混合物组件。

mixin1.vue

export default {
  data(){
     return {
      $_mixin1_data1 : 'data1',
      data2 : 'data2'
     }
  },
  methods:{
    $_mixin1_method1(){
    
    }
  }
}

parent.vue

import mixin1 from './mixin1';

export default {
  name : 'Parent',
  mixins:[mixin1],
  data(){
    return {
      parent1 : 'parent1'
    }
  },
  mounted(){
    console.log(this.parent1); // parent1
    console.log(this.$_mixin1_data1) //undefined
    console.log(this.data2); //data2
  }
}

当我将parent component与MixIn1组件一起使用时,父组件找不到以$ 开头的数据。但是“ data2”数据和以$ 开头的方法。我不知道为什么未检测到以$ _开头的数据。我可能不了解Vue的概念。 感谢您的答复。

I made some components with mixins.

mixin1.vue

export default {
  data(){
     return {
      $_mixin1_data1 : 'data1',
      data2 : 'data2'
     }
  },
  methods:{
    $_mixin1_method1(){
    
    }
  }
}

Parent.vue

import mixin1 from './mixin1';

export default {
  name : 'Parent',
  mixins:[mixin1],
  data(){
    return {
      parent1 : 'parent1'
    }
  },
  mounted(){
    console.log(this.parent1); // parent1
    console.log(this.$_mixin1_data1) //undefined
    console.log(this.data2); //data2
  }
}

When I use Parent component with mixin1 component, Parent component could not find data that starts with $. But "data2" data and the method that starts with $ is worked. I don't know why the data that starts with $_ isn't detected. I might not understand Vue's concept.
Thanks for your reply.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2025-02-07 06:10:27

docs

vue通过组件实例曝光自己的内置API时使用$前缀。它还保留内部属性的前缀_。您应该避免使用以这些字符之一开头的顶级data属性的名称。

Docs

Vue uses a $ prefix when exposing its own built-in APIs via the component instance. It also reserves the prefix _ for internal properties. You should avoid using names for top-level data properties that start with either of these characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文