基于Typescript Vue类的组件抛出错误 不能在Laravel mix中设置未定义的属性'render'.

问题描述 投票:0回答:1

我使用Laravel Mix来编译我的Vue组件, 我使用了TypeScript和基于类的组件. 每一个类都是从组件中导出的, 每一个组件都是主应用程序脚本中的上下文所需要的, 但是在渲染过程中Vue会抛出错误.

Uncaught TypeError: Cannot set property 'render' of undefined
    at normalizeComponent (componentNormalizer.js:24)

我找遍了所有的互联网,但人们只说了无效的导出类。我确信组件中的导出类是有效的。我不知道我做错了什么。

当我回到基于对象的组件的纯JavaScript中时,一切都很完美,所以可能是TypeScript配置错误或什么。我完全放弃了 :(

app.ts

import Vue from 'vue';
import _ from "lodash"

export default class App {

    protected registerComponents(): void {
        const components = require.context('./', true, /\.vue$/i);

        components.keys().forEach((componentPath) => {
            // @ts-ignore
            const componentName = componentPath
                .split('/').pop() // full component name
                .split('.').slice(0, 1).shift(); // component name without extension

            Vue.component(
                _.kebabCase(componentName),
                components(componentPath)
            );
        })
    }

    protected boot(): void {
        this.registerComponents();
    }

    public init(): Vue {
        this.boot();

        return new Vue({
            el: '#main',
        });
    }
}

EventSignUpForm.vue

<template>
    <div>
        <p>Long-form v-model example</p>
    </div>
</template>

<script lang="ts">
    import Vue from 'vue'
    import {Component} from 'vue-property-decorator';

    @Component({
        name: 'EventSignUpForm',
    })
    export class EventSignUpForm extends Vue {

        protected count = 0

        public increment() {
            this.count++
        }

        public decrement() {
            this.count--
        }
    }

    export default EventSignUpForm;
</script>

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "es2015",
        "moduleResolution": "node",
        "strict": true,
        "jsx": "preserve",
        "importHelpers": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "sourceMap": true,
        "baseUrl": ".",
        "types": [
            "node",
            "webpack-env"
        ],
        "paths": {
            "@/*": ["./resources/js/*"]
        },
        "lib": [
            "esnext",
            "dom",
            "dom.iterable",
            "scripthost"
        ]
    },
    "include": [
        "resources/js/**/*.ts",
        "resources/js/**/*.tsx",
        "resources/js/**/*.vue"
    ],
    "exclude": [
        "node_modules"
    ]
}

webpack.mix.js

class WebpackMix {
    constructor() {
        this.mix = require('laravel-mix');
    }

    configureWebpack(){
        this.mix.webpackConfig({
            module: {
                rules: [
                    {
                        test: /\.tsx?$/,
                        loader: "ts-loader",
                        exclude: /node_modules/,
                    }
                ]
            },
            resolve: {
                extensions: ["*", ".js", ".jsx", ".vue", ".ts", ".tsx"],
                alias: {
                    '@': path.resolve(__dirname, 'resources', 'js'),
                },
            }
        });
    }
    // others things  
}
typescript vue.js webpack vuejs2 laravel-mix
1个回答
0
投票

事件登录表格.vue:制作组件 export 作为 export default:

export class EventSignUpForm extends Vue 

拟变更

export default class EventSignUpForm extends Vue

并从底部取出

export default EventSignUpForm;

0
投票

我的同事帮我解决了这个相当棘手的案子。

我们要做的是加入 webpack.mix.jsts-loader 选项对象。我们需要给Vue组件添加TS后缀。现在是这样的。

rules: [
  {
    test: /\.tsx?$/,
    loader: 'ts-loader',
    exclude: /node_modules/,
    options: {
      appendTsSuffixTo: [/\.vue$/]
    }
  }
]

接下来我们要做的是改变 tsconfig.js 编译器选项.模块 将要 普通js 而不是 es2015,像这样。

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        // the rest remain unchanged
    }
}

作为最后一件事,所有的importrequire Vue组件必须是默认的导入,我只用了import in require.context 但必须改成这样。

protected registerComponents(): void {
  const components = require.context('./', true, /\.vue$/i);

  components.keys().forEach((componentPath) => {
    // @ts-ignore
    const componentName = componentPath
      .split('/').pop() // full component name
      .split('.').slice(0, 1).shift(); // component name without extension

    Vue.component(
      _.kebabCase(componentName),
      components(componentPath).default
    );
  })
}

这解决了我的问题,感谢Adrian的时间,并给出了有效的解决方案:)

© www.soinside.com 2019 - 2024. All rights reserved.