[英]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.