【问题标题】:Vue.js2 : How to reuse a constant defined inside a componentVue.js2:如何重用组件内定义的常量
【发布时间】:2018-02-17 20:49:06
【问题描述】:

所以我想在挂载的函数中定义一个 const 值。并且想在 vue 组件的方法中使用这个 const 值。

例子:

export default {
    name : 'app',
    data(){

    },
    mounted(){
        // Constant refThis is defined here which gets the reference to this vue component 
        const refThis = this;

    },
    methods:{
        useThisRef() {
            // I want to be able to use the refThis here 
        }
    }
}

如何在 vue 组件的其他部分获得对 refThis 的引用?

【问题讨论】:

  • 你好像有an XY problem,请解释一下你到底想做什么。
  • @Nit 感谢您对 XY 问题的启发。我已经相应地编辑了问题。
  • 既然有this,为什么还需要refThis
  • 很抱歉应该在问题中提到这一点,但有时直接使用它时它不会返回属性。 @伯特
  • 你的意思是在回调中?

标签: ecmascript-6 vue.js vuejs2 vue-component


【解决方案1】:

你能在const.js 文件中做类似的事情吗:

export const ConstName = {
  //Code in here
}

然后你可以在顶部的任何组件中导入这个:

import ConstName form '/path/to/const.js

【讨论】:

  • 这与问题完全无关。
猜你喜欢
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 2011-09-18
  • 2020-01-15
  • 2019-09-07
  • 2021-05-14
  • 2012-12-06
  • 1970-01-01
相关资源
最近更新 更多