根据 Vue.js文档在单文件组件中使用 pug 预处理器,需要pug -plain-loader(不是 pug-loader):
{
test: /\.pug$/,
loader: 'pug-plain-loader'
}
如果除了单个文件组件(.vue 文件)之外,我还需要将 pug 模板导入由vue-property-decorator包提供的 TypeScript 类(基于vue-class-component)怎么办?
我必须查看仅用于 html 模板加载的示例:
@Component({
template: require('./MyComponent.html')
})
export default class MyComponent extends Vue {
//...
}
如果需要改用 pug 怎么办?
@Component({
template: require('./RegularButton.pug')
})
export default class RegularButton extends Vue {
//...
}
在这种情况下,不应使用 pug-plain-loader:
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> <button @click="onClickEventHandler">{{ lettering }}</button>
@ ../ReusableComponents/RegularButton/RegularButton.ts 18:18-48
@ ./SPA_Test.ts
我知道首先我需要安装 pug-loader。但是我需要如何使我的 webpack 配置与 pug-plain-loader 设置相协调呢?
// ...
module: {
rules: [
{
test: /\.ts?$/,
loader: 'ts-loader',
options: {
appendTsSuffixTo: [/\.vue$/]
}
},
{
test: /\.json5$/,
loader: 'json5-loader'
},
{
test: /\.(yml|yaml)$/,
use: ['json-loader', 'yaml-loader']
},
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.pug$/,
loader: 'pug-plain-loader'
}
]
}
更新:第一次解决后的错误输出
NonErrorEmittedError: (Emitted value instead of an instance of Error)
Errors compiling template:
text "export default "" outside root element will be ignored.
1 | export default "<div class=\"container\"><h1>{{ pageTitle }}</h1><hr><div><div>V-Model Test:</div><div>{{ vModelTestProperty }}</div><div><input type=\"text\" v-model=\"vModelTestProperty\"></div></div><hr><div><div>{{ defaultTextLabel }}</div><div><RegularButton :lettering=\""Non default button text"\" :onClickEventHandler=\"executeTest\"></RegularButton></div></div></div>"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at Object.emitError (C:\Users\i\Documents\PhpStorm\InHouseDevelopment\mylib\node_modules\webpack\lib\NormalModule.js:165:14)
at Object.module.exports (C:\Users\i\Documents\PhpStorm\InHouseDevelopment\mylib\node_modules\vue-loader\lib\loaders\templateLoader.js:61:21)
@ ./SPA_Test.vue?vue&type=template&id=cabf1cca&lang=pug& 1:0-422 1:0-422
@ ./SPA_Test.vue
@ ./SPA_Test.ts
零件:
<template lang="pug">
.container
h1 {{ pageTitle }}
hr
div
div V-Model Test:
div {{ vModelTestProperty }}
div: input(type='text' v-model='vModelTestProperty')
hr
div
div {{ defaultTextLabel }}
div: RegularButton(:lettering='"Non default button text"' :onClickEventHandler='executeTest')
</template>
<script lang="ts">
import { Vue, Component, Prop } from 'vue-property-decorator'
@Component
export default class SPA_Test extends Vue {
private pageTitle: string = 'SPA related test';
private vModelTestProperty: string = 'Inputted characters will be displayed here';
private defaultTextLabel: string = 'Default text';
public executeTest(): void {
console.log('Test O\'K');
}
}
</script>
我不认为@Hammerbot 的解决方案是错误的;也许这是正确解决方案的一部分。无论如何,通过设置
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.pug$/,
loader: 'pug-plain-loader'
}
所有作品,除非.pug
无法在@Component
装饰器中导入:
import { Vue, Component, Prop } from 'vue-property-decorator';
@Component({
//template: require('./RegularButton.pug') // error will occur
template: '<button @click="onClickEventHandler">{{ lettering }}</button>'
})
export default class RegularButton extends Vue {
@Prop({default: 'Default text', type: String}) private readonly lettering!: string;
@Prop({default: (): void => {}, type: Function}) private readonly onClickEventHandler!: () => {};
}