【问题标题】:How to generate HTML from .vue component如何从 .vue 组件生成 HTML
【发布时间】:2019-06-29 03:47:05
【问题描述】:

我想创建使用 VueJS 的简单静态站点生成器。

我有简单的 vue 组件:

<template>
  <div>
    <v-child
      v-for="child in children"
      :title="'Child ' + child.id"
      :key="child.id"
    >
      {{ child.name }}
    </v-child>
  </div>
</template>

<script>
import children from './children';

export default {
  data () {
    return {
      children
    };
  }
}
</script>

并希望实现从中生成 HTML 的功能:

describe('generateHTML', () => {
  it('generates HTML from vue component', async () => {
    expect(await generateHTML(__dirname + '/fixtures/v-parent.vue'))
      .to.equal('<div><div class="child"><div>Child 1</div><div>First</div></div><div class="child"><div>Child 2</div><div>Second</div></div></div>')
  });
});

你有什么想法/提示我该怎么做?

这里是重现我的问题的回购: lusarz/vue-questions

只要运行:

npm install
npm run test

我做了一些初步尝试,但现在它的工作方式如下:

【问题讨论】:

    标签: node.js vue.js vuejs2 server-side-rendering vue-loader


    【解决方案1】:

    你可以使用@vue/test-utils中的mount函数

    import { mount } from '@vue/test-utils'
    import Foo from './Foo.vue'
    
    describe('Foo Component', () => {
      it('check rendered component html', () => {
        const wrapper = mount(Foo)
        expect(wrapper.html()).toBe("<--- Your Html --->")
      })
    })

    【讨论】:

      猜你喜欢
      • 2020-05-09
      • 2020-11-18
      • 2020-08-04
      • 2019-11-14
      • 2018-01-31
      • 2012-05-20
      • 2010-10-10
      • 1970-01-01
      • 2011-06-24
      相关资源
      最近更新 更多