gpt4 book ai didi

typescript - 如何设置 Webpack 以便能够在单文件组件 (.vue) 和 "vue-class-component"类中使用 Pug?

转载 作者:搜寻专家 更新时间:2023-10-30 20:38:21 25 4
gpt4 key购买 nike

根据 Vue.js documentatin在单文件组件中使用 pug 预处理器,pug-plain-loader (不是 pug-loader )需要:

{
test: /\.pug$/,
loader: 'pug-plain-loader'
}

如果除了 Singe 文件组件(.vue 文件)我还需要将 pug 模板导入 TypeScript 类,由 vue-property-decorator 提供怎么办?包(基于 vue-class-component )?

我必须看到 example仅用于 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=\"&quot;Non default button text&quot;\" :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!: () => {};
}

最佳答案

据Vue.js官方documentation , 要将 vue-loaderpug-plain-loader 一起使用,您需要按如下方式配置加载器规则:

If you also intend to use it to import .pug files as HTML strings in JavaScript, you will need to chain raw-loader after the preprocessing loader. Note however adding raw-loader would break the usage in Vue components, so you need to have two rules, one of them targeting Vue files using a resourceQuery, the other one (fallback) targeting JavaScript imports:

// webpack.config.js -> module.rules
{
test: /\.pug$/,
oneOf: [
// this applies to `<template lang="pug">` in Vue components
{
resourceQuery: /^\?vue/,
use: ['pug-plain-loader']
},
// this applies to pug imports inside JavaScript
{
use: ['raw-loader', 'pug-plain-loader']
}
]
}

关于typescript - 如何设置 Webpack 以便能够在单文件组件 (.vue) 和 "vue-class-component"类中使用 Pug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55153689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com