简体   繁体   English

Vue + Typescript + webpack:将html导入组件

[英]Vue + Typescript + webpack: Import html into component

I have a typescript + vue + webpack application and I want separate html from code. 我有一个typescript + vue + webpack应用程序,我想从代码中单独的html。
I have follow this tutorial and I have made a simple Hello Word. 我已经按照本教程编写了一个简单的Hello Word。

Webpack config Webpack配置

const path = require('path');

module.exports = {
    mode: "development",
    entry: './src/app.ts',
    output: {
        path: path.resolve('dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.(ts|tsx)?$/,
                loader: 'ts-loader',
                exclude: /node_modules/                
            },
            {
                test: /.html$/,
                loader: "vue-template-loader",
                exclude: /index.html/
            }
        ]
    },
    resolve: {
        extensions: [
            '.js',
            '.vue',
            '.tsx',
            '.ts'
        ]
    }
};

Html HTML

<div>
    <h2>Hello from {{message}}</h2>
</div>

Vue Component Vue组件

import Vue from "vue";
import Component from "vue-class-component";
// template: '<button @click="onClick">Click!</button>'
import WithRender from "./home.html";

@WithRender
@Component
export default class HomeComponent extends Vue {

    public message: string = "Word";

    constructor() {
        super();
    }

    mounted() { }
}

After I have added this shim 我添加了这个垫片后

declare module '*.html' {

        import Vue, { ComponentOptions, FunctionalComponentOptions } from 'vue'
        interface WithRender {
            <V extends Vue, U extends ComponentOptions<V> | FunctionalComponentOptions>(options: U): U
            <V extends typeof Vue>(component: V): V
        }

        const withRender: WithRender
        export default withRender
}

I have (almost) understand how typescript decorators work but I don't understand the shim code, how it is possible that this code inject the html into the Vue component ? 我(几乎)了解了typescript 装饰器是如何工作的,但我不理解填充码,这段代码如何 html注入Vue组件?

I have readed about Decorators from Typescript site 我已经从Typescript网站了解装饰器

vue-template-loader compiles the HTML template into a render function, which @WithRender inserts into the class definition. vue-template-loader将HTML模板编译为render函数, @WithRender将其插入到类定义中。

For instance, this HTML: 例如,这个HTML:

<div>Hello world</div>

is converted into this render function: 转换为此render函数:

render(h) {
  return h('div', 'Hello world')
}

Then, applying @WithRender (the result of importing the example HTML template above) to class Foo extends Vue {} results in: 然后,将@WithRender (将上面的示例HTML模板导入的结果)应用于class Foo extends Vue {}导致:

class Foo extends Vue {
  render(h) {
    return h('div', 'Hello world')
  }
}

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

相关问题 使用webpack从typescript导入html - Import html from typescript with webpack Vue Typescript 组件库 - 使用 Webpack 和 SASS - Vue Typescript component library - Working with Webpack and SASS 在 Vue 组件中导入和使用外部 Typescript 模块 - Import and use external Typescript module in Vue component Typescript Vue 的 VSCode 智能感知(@Component 自动导入) - VSCode intellisense for Typescript Vue (Auto import at @Component) 在纯 TypeScript 文件中导入 Vue 组件失败 - Import Vue component in plain TypeScript file fails 如何在 vue3 组件中导入打字稿文件? - How to import typescript files in vue3 component? 无法挂载组件:Vue中没有使用Typescript&Webpack定义的模板或渲染功能, - Failed to mount component: template or render function not defined in Vue with Typescript & Webpack, 如何在 typescript vue js 项目中将 html 文件作为模板导入为模块,以便在多个组件中使用相同的 html 文件? - How to import html file as template in typescript vue js project as module to use same html file in more than a component? 打字稿导入和Webpack - Typescript import and Webpack Webpack,打字稿和导入解析 - Webpack,typescript and import resolve
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM