【问题标题】:Vue.js 3 template- literals error Cannot read property 'range' of nullVue.js 3 模板文字错误无法读取 null 的属性“范围”
【发布时间】:2021-06-08 03:58:17
【问题描述】:

我正在尝试在 Vue.js 中动态导入异步组件。

<template>
  <MyAsync></MyAsync>
</template>

<script>
import { defineAsyncComponent } from 'vue';

const component = 'lorem-ipsum-4';

export default {
  name: 'Article',
  components: {
    MyAsync: defineAsyncComponent(() => import(`@/articles/${component}.vue`)),
  },
};
</script>

但这不起作用并产生以下错误。

Failed to compile.

./src/views/Article.vue
Module build failed (from ./node_modules/eslint-loader/index.js):
TypeError: Cannot read property 'range' of null
Occurred while linting /project/src/views/Article.vue:13
    at SourceCode.getTokenBefore (/project/node_modules/eslint/lib/source-code/token-store/index.js:298:18)
    at checkSpacingBefore (/project/node_modules/eslint/lib/rules/template-curly-spacing.js:60:42)
    at TemplateElement (/project/node_modules/eslint/lib/rules/template-curly-spacing.js:119:17)
    at /project/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/project/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/project/node_modules/eslint/lib/linter/node-event-generator.js:254:26)
    at NodeEventGenerator.applySelectors (/project/node_modules/eslint/lib/linter/node-event-generator.js:283:22)
    at NodeEventGenerator.enterNode (/project/node_modules/eslint/lib/linter/node-event-generator.js:297:14)
    at CodePathAnalyzer.enterNode (/project/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js:634:23)
    at /project/node_modules/eslint/lib/linter/linter.js:936:32
    at Array.forEach (<anonymous>)
    at runRules (/project/node_modules/eslint/lib/linter/linter.js:931:15)
    at Linter._verifyWithoutProcessors (/project/node_modules/eslint/lib/linter/linter.js:1157:31)
    at /project/node_modules/eslint/lib/linter/linter.js:1281:29
    at Array.map (<anonymous>)

但是静态导入可以正常工作:

<template>
  <MyAsync></MyAsync>
</template>

<script>
import { defineAsyncComponent } from 'vue';

export default {
  name: 'Article',
  components: {
    MyAsync: defineAsyncComponent(() => import('@/articles/lorem-ipsum-4.vue')),
  },
};
</script>

这是一个使用 Vue CLI 创建的 Vue.js v3 项目。

可以在github找到一个(非常少的)示例

有没有办法让动态导入工作?

【问题讨论】:

    标签: vue.js eslint vuejs3 dynamic-import async-components


    【解决方案1】:

    export default {
      name: 'Article',
      components: {
        MyAsync: defineAsyncComponent(() => import('@/articles/'+ component +'.vue')),
      },
    };

    但是,我不认为这很好。

    【讨论】:

    【解决方案2】:

    经过调试发现是eslint issue template-curly-spacing,为了解决这个问题,我将文件.eslintrc添加到项目根目录,内容如下:

    {
        "plugins": [
            "vue"
          ],
        "rules" : {
            "template-curly-spacing": ["error", "never"]
          }
    }
    

    【讨论】:

    • 太好了,你真的明白了!我想接受你的回答,但不幸的是它不适用于我的设置。我在eslint.vuejs.org/user-guide/#usage 找到了正确的语法。您是否将 .eslintrc 添加到我上传到 github 的示例项目中,或者您是否创建了一个新项目?
    • 我克隆了你的,并添加了 .eslintrc 文件,上面的内容及其作品
    • 好的,那我接受。对于之后遇到 linting 问题的任何人,我建议使用上面链接中的配置以及此答案中的附加规则。
    【解决方案3】:

    在由./组成的路径上替换@

    export default {
      name: 'Article',
      components: {
        MyAsync: defineAsyncComponent(() => import(`./articles/${component}.vue`)),
      },
    };
    

    它对我有用

    【讨论】:

    • 感谢您的回答。不幸的是,这对我来说没有任何改变。
    猜你喜欢
    • 2018-07-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 2018-06-13
    • 2018-06-28
    • 2023-03-11
    相关资源
    最近更新 更多