简体   繁体   English

无法挂载组件:Vue中没有使用Typescript&Webpack定义的模板或渲染功能,

[英]Failed to mount component: template or render function not defined in Vue with Typescript & Webpack,

I get following error when click on "Counter" or "Fetch Data". 单击“计数器”或“获取数据”时出现以下错误。

[Vue warn]: Failed to mount component: template or render function not defined.                      vendor.js?v=MxNIG8b0Wz6QtAfWhyA0-4CCwZshMscBGmtIBXxKshw:13856 

found in

---> <Anonymous>
       <T> at ClientApp\components\app\app.vue.html
         <Root>

My boot.ts is as following: 我的boot.ts如下:

import './css/site.css';
import 'bootstrap';
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);

const Counter = () => import('./components/counter/counter').then(m => m.default);
const FetchData = () => import('./components/fetchdata/fetchdata');

const routes = [
    { path: '/', component:  require('./components/home/home.vue.html').default },
    { path: '/counter', component: Counter },
    { path: '/fetchdata', component: FetchData }
];

new Vue({
    el: '#app-root',
    router: new VueRouter({ mode: 'history', routes: routes }),
    render: h => h(require('./components/app/app.vue.html').default)
});

My Counter.vue.html is as following 我的Counter.vue.html如下

<template>
    <div>
        <h1>Counter</h1>

        <p>This is a simple example of a Vue.js component.</p>

        <p>Current count: <strong>{{ currentcount }}</strong></p>

        <button @click="incrementCounter">Increment</button>
    </div>
</template>

<script src="./counter.ts"></script>

My counter.ts is as following: 我的counter.ts如下:

import Vue from 'vue';
import { Component } from 'vue-property-decorator';

@Component
export default class CounterComponent extends Vue {
    currentcount: number = 0;

    incrementCounter() {
        this.currentcount++;
    }
}

My fetchdata.vue.html is as following: 我的fetchdata.vue.html如下:

<template>
    <div>
        <h1>Weather forecast</h1>

        <p>This component demonstrates fetching data from the server.</p>

        <table v-if="forecasts.length" class="table">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Temp. (C)</th>
                    <th>Temp. (F)</th>
                    <th>Summary</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in forecasts">
                    <td>{{ item.dateFormatted }}</td>
                    <td>{{ item.temperatureC }}</td>
                    <td>{{ item.temperatureF }}</td>
                    <td>{{ item.summary }}</td>
                </tr>
            </tbody>
        </table>

        <p v-else><em>Loading...</em></p>
    </div>
</template>

<script src="./fetchdata.ts"></script>

My fetchdata.ts is as following: 我的fetchdata.ts如下:

import Vue from 'vue';
import { Component } from 'vue-property-decorator';

interface WeatherForecast {
    dateFormatted: string;
    temperatureC: number;
    temperatureF: number;
    summary: string;
}

@Component
export default class FetchDataComponent extends Vue {
    forecasts: WeatherForecast[] = [];

    mounted() {
        fetch('api/SampleData/WeatherForecasts')
            .then(response => response.json() as Promise<WeatherForecast[]>)
            .then(data => {
                this.forecasts = data;
            });
    }
}

My NPM packags are as following: 我的NPM包装如下:

-- vue-loader@14.2.2 -vue-loader@14.2.2

-- vue@2.5.16 -vue@2.5.16

-- vue-router@3.0.1 -vue-router@3.0.1

-- webpack@4.3.0 (npm list webpack shows -- (empty) . dont know why. But this is not an issue I guess.) -webpack@4.3.0(npm列表webpack显示-(空) 。不知道为什么。但是我猜这不是问题。)

-- typescript@2.8.1 -typescript@2.8.1

For fetchdata, u'll also need to do what you have done for 'counter'. 对于fetchdata,您还需要执行针对“计数器”的操作。

const FetchData = () => import('./components/fetchdata/fetchdata').then(m => m.default);

Then, for both of the component. 然后,对于两个组件。 U have called ".ts" files which knows nothing aboout where their ".vue.html" files (templates) are. U已将“ .ts”文件称为 .vue.html”文件(模板)所在的位置。

shortest resolution is to refer ".vue.html" where you have referenced their ".ts" files. 最短的解决方案是在引用其“ .ts”文件的地方引用“ .vue.html”。 like this. 像这样。

const FetchData = () => import('./components/fetchdata/fetchdata.vue.html').then(m => m.default);

const Counter= () => import('./components/counter/counter.vue.html').then(m => m.default);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (Vue with Typescript) 无法挂载组件:未定义模板或渲染函数 - (Vue with Typescript) Failed to mount component: template or render function not defined Typescript Vue:无法安装组件:模板或渲染 function 未定义 - Typescript Vue: Failed to mount component: template or render function not defined [Vue 警告]:无法挂载组件:未定义模板或渲染函数。 -vue-simple-uploader - [Vue warn]: Failed to mount component: template or render function not defined. -vue-simple-uploader 获取“[Vue 警告]:无法挂载组件:未定义模板或渲染函数。” 尝试在没有 .vue 的情况下导入时 - Getting "[Vue warn]: Failed to mount component: template or render function not defined." when trying to import without .vue 无法安装组件:未定义模板或渲染功能:Vue动态导入,延迟加载 - Failed to mount component: template or render function not defined: Vue Dynamic import, Lazy loading Vue 关注点分离导致无法挂载组件:模板或渲染函数未定义 - Vue separation of concerns causes Failed to mount component: template or render function not defined 在运行带有笑话的测试时如何解决警告:无法装入组件:模板或渲染函数未定义 - How to fix warning when running tests with jest: Failed to mount component: template or render function not defined 无法使用TypeScript在Vue-cli 2上安装组件 - Failed to mount component on Vue-cli 2 using TypeScript 具有Webpack和Typescript的Vue 2.5模板 - Vue 2.5 template with Webpack and Typescript Vue + Typescript + webpack:将html导入组件 - Vue + Typescript + webpack: Import html into component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM