首页 > 解决方案 > Vue - 实现 this.$options.staticRenderFns

问题描述

我正在尝试使用自定义渲染功能实现 Vue 组件。一旦我的模板嵌套了 HTML 元素,我就会收到以下错误:

TypeError: this.$options.staticRenderFns is undefined

如何实现staticRenderFns

代码:

<!doctype html>
<html>
<head>
    <meta charset="utf-8" /> 
    <script src="https://unpkg.com/vue"></script>
</head>
<body>
    <div id="app">
        <comp temp="temp1"></comp>
    </div>
    <script>
        var myTemplates = new Array();
        myTemplates['temp1'] = Vue.compile("<div><div>This template will come from an AJAX source</div></div>").render; 

        var Comp = {
          props: ["temp"], 
          data() {
            return {
                renderFunc: myTemplates[ this.$props.temp ],
            }
          },
          render(h) {
            return this.renderFunc()
          }
        };

        new Vue({
          el: "#app",
          data: { },
          components: { Comp }
        });
    </script>
</body>
</html>

标签: vue.jsvuejs2vue-component

解决方案


推荐阅读