【问题标题】:Importing variable to vue file将变量导入vue文件
【发布时间】:2017-07-20 08:42:59
【问题描述】:

我需要将一些变量导入到 vue 组件文件中。我是这样做的:

require('imports-loader?myVar=>{test:12345}!./my-com.vue');

结果我得到[Vue warn]: Error in render function: "ReferenceError: myVar is not defined"

我知道props,但我想传递一个变量。

这里是“my-com.vue”:

<template>
  <div>...</div>
</template>
<script>
  console.log(myVar); // <--- here I get vue warn
  export default {
    props:['rows'],
    ...
  }
</script>

从这里导入:

module.exports = function(params){
  return function(resolve, reject){
    resolve({
      components:{
        'my-com': require('imports-loader?myVar=>{test:12345}!./my-com.vue')
      },
      ...
    })
  }
}

我哪里错了?
如何将变量导入 vue 组件文件?

提前致谢。

【问题讨论】:

  • 你想从哪里导入这个变量?。它是否存在于另一个.js 文件中?

标签: javascript vue.js vue-component


【解决方案1】:

您可以使用import 声明

<script>
  import { myVar } from './path_to_the_file'
  console.log(myVar); 
  export default {
    props:['rows'],
    ...
  }
</script> 

确保使用另一个文件中的导出语句来导出myVar,如下所示:

export var myVar = 'my variable';

【讨论】:

  • Ок,它有效。不过,如何使用 import-loader 将变量传递给 vue 文件?
  • @VamsiKrishna 使用 myComponent.vue 文件,我很困惑如何设置 "props" 。您的解决方案就像一个魅力 +1
猜你喜欢
  • 2021-07-28
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 2019-06-18
  • 2020-02-18
  • 2019-10-08
  • 1970-01-01
  • 2021-01-20
相关资源
最近更新 更多