简体   繁体   English

渲染Laravel Blade部分视图时编译Vue.js组件

[英]Compiling Vue.js component while rendering Laravel Blade partial view

I have a simple component in Vue.js which is used in a partial view - question.blade.php : 我在Vue.js一个简单的组件,其在一个局部视图中使用- question.blade.php:

{{--HTML code--}}

<my-component type='question'>
    <div class="question">[Very long text content...]</div>
</my-component>

{{--more HTML code--}}

The idea behind the component is to create a "show more - show less" logic around the question content. 组件背后的想法是围绕问题内容创建“多展示-多展示”逻辑。

The component is compiled and renders just fine on page load. 该组件被编译并在页面加载时呈现良好。 However, there are cases where I need to dynamically load a question via Ajax . 但是,在某些情况下,我需要通过Ajax动态加载问题。 For this, I make a simple jQuery Ajax call, retrieve the HTML of the question.blade.php and append it in the DOM. 为此,我进行了一个简单的jQuery Ajax调用,检索了question.blade.php的HTML并将其附加在DOM中。 The problem is, the component is not compiled . 问题是, 该组件未编译

How do I make sure the component is always compiled when the partial view gets rendered, independently of whether it occurs on page load or via Ajax call? 如何确保部分视图呈现时始终编译该组件,而不管它是在页面加载时发生还是通过Ajax调用发生?

Full component code: 完整的组件代码:

{% verbatim %}
<template>
    <div>
        <div v-bind:class="cssClasses" v-html="content"></div>

        <div v-if="activateShowMore && !isShown" class="sml-button closed" v-on:click="toggleButton()">
            <span class="sml-ellipsis">...</span><span class="sml-label">{{$t('show_more')}}</span>
        </div>
        <div v-if="activateShowMore && isShown" class="sml-button open" v-on:click="toggleButton()">
            <span class="sml-label">{{$t('show_less')}}</span>
        </div>
    </div>
</template>

<style lang="sass" scoped>
    /*styles*/
</style>

<script type="text/babel">
    export default {
        props: ['content', 'type'],

        data() {
            return {
                activateShowMore: false,
                isShown: false,
                cssClasses: this.getCssClasses()
            }
        },

        locales: {
            en: {
                'show_more': 'show more',
                'show_less': 'show less'
            },
            de: {
                'show_more': 'mehr anzeigen',
                'show_less': 'weniger anzeigen'
            }
        },

        mounted() {
            this.checkShowMore();
        },

        watch: {
            isShown: function(shouldBeShown) {
                this.cssClasses = this.getCssClasses(shouldBeShown);
            }
        },

        methods: {
            checkShowMore: function() {
                let $element = $(this.$el);
                let visibleHeight = $element.outerHeight();
                let realHeight = $element.find('.text-area-read').first().outerHeight();
                let maxHeight = this.getMaxHeight();

                this.activateShowMore = (visibleHeight === maxHeight) && (visibleHeight < realHeight);
            },
            getMaxHeight: function() {
                switch (this.type) {
                    case 'question':
                        return 105;
                    case 'answer':
                        return 64;
                }
            },
            toggleButton: function() {
                this.isShown = !this.isShown;
            },
            getCssClasses: function(shouldBeShown) {
                if (undefined === shouldBeShown || !shouldBeShown) {
                    return 'sml-container' + ' sml-' + this.type + ' sml-max-height';
                }

                return 'sml-container' + ' sml-' + this.type;
            }
        }
    }
</script>

I don't think this is the best way but it should do the trick. 我认为这不是最好的方法,但是应该可以解决。 But I had to deal with vue and jquery communication before. 但是我之前不得不处理vue和jquery通信。

What I did is created a hidden input and changed the value with jquery after the ajax call finished and then triggered the change event with jquery. 我所做的是创建一个隐藏的输入,并在ajax调用完成后使用jquery更改了值,然后使用jquery触发了change事件。 Then you already listening to the event inside vue and you will know you need to update the content. 然后,您已经在vue内部监听了事件,您将知道需要更新内容。 This should get you going with some modification to your vue component and should be able to update. 这应该使您对vue组件进行一些修改,并且应该能够更新。 If you need to send the content to vue you might need to send it in the input hidden value. 如果需要将内容发送到vue,则可能需要以输入的隐藏值发送它。 I did a quick demo code to explain what I mean. 我做了一个快速演示代码来解释我的意思。 Here's the link . 这是链接

var app = new Vue({
          el: '#app',
          data(){
            return{
              content: 'hi there',
            }
          },
          methods: {
            onChangeHandler: function(e){
              this.content = e.target.value
            }
          },
       });

       $('#me').on('click',function(){
         $('#update').val('Good Day!')
         $('#update').trigger("click")
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM