简体   繁体   English

如何将数据从 vue 组件传递到 Laravel 中的视图(HTML)

[英]How to pass data from vue component to view(HTML) in laravel

I'm trying to pass data from vue component to blade file.我正在尝试将数据从 vue 组件传递到刀片文件。 I try to create props but it's didn't work for me.我尝试创建道具,但它对我不起作用。 Is it any possible to pass the object to props to get the data?是否有可能将对象传递给道具以获取数据? I'm new laravel.我是新来的laravel。 I want to pass data which subject, message, days, condition and module name to blade file(view) .我想将主题、消息、日期、条件和模块名称的数据传递给刀片文件(视图) I kept searching for this but couldn't find an answer that will make this clear.我一直在寻找这个,但找不到一个可以说明这一点的答案。

Thanks!谢谢!

blade.php刀片.php

  <div id="app">
       <email-component
          email_edit_route="{{ route('havence.automail.edit',['id'=>$mailTemplates->id]) }}"
       >
       </email-component>

 </div>

Vue.js Vue.js

<script>
    import Vue from 'vue'
    import axios from 'axios'
    import MarkdownIt from 'markdown-it'
    import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
    var msg_editor;


    const md = new MarkdownIt({
        linkify: true
    })

  export default {
    props: ['email_creation_link', 'email_index_route', 'email_edit_route','conditions','modules'],

    components: {

    },


    data() {
        return {
            template: {
                subject: '',
                message: '' ,
                days: '',
                condition_id: 1,

            },
            options:[
                {
                    display:'Client Name',
                    actual:'Client name'
                }, 
                {
                    display:'Joined Date',
                    actual:'Joined date'
                },
                {
                    display:'Module Name',
                    actual:'Module name'
                },
                {
                    display:'Last Seen',
                    actual:'Last seen'
                },
            ],


              showName: false,


        }
    },


    mounted(){
             var self = this;

            ClassicEditor
            .create(document.querySelector( "#msg"),
                {
                })
            .then(editor => {
                msg_editor = editor;
                editor.model.document.on( 'change:data', () => {
                    self.template.message = msg_editor.getData();
                });
            })

            .catch(error => {
                console.error(error);
            })

        }, 

    methods: {

        //Drag items
        dragstart: function(item, e){
            this.draggingItem = item;
            e.dataTransfer.setData('text/plain', item.actual);
        },
        dragend: function(item,e) {
            e.target.style.opacity = 1;
        },
        dragenter: function(item, e) {
            this.draggingItem = item;
        },
        //content
        replaceVariables(input)
        {
            let updated = input
            return updated
        },
        //hidecontent
        showHide: function(e)
        {
            console.log("Show "+e.target.value+ " fields")
            this.showName = e.target.value !== ''
        },
        fetch()
        {
            //request data
            axios.get(this.email_index_route,this.template)
                .then((res) => {
                    this.template = res.data.template;

                })
        },
        save()
        {
            //save data to db
            axios.post(this.email_index_route, this.templates)
                .then((res) => {
                    alert('Mail sent successfull!')
                })
        },
        addToMail: function(type, text)
        {
            if (type == 'message') {
                this.template.message += text;
                msg_editor.setData(this.template.message);
            }
        },

        //user name replace
        replaceVariables() {
            return this.replaceVariables(this.options || '')
        },
    }
  }
</script>

Quick solution.快速解决。 : why not store data in local storage and fetch it from laravel blade ? :为什么不将数据存储在本地存储中并从 laravel Blade 中获取? Next solution: you can fire global event from vue and listen on laravel blade .下一个解决方案:您可以从 vue 触发全局事件并在 laravelblade 上监听。

why dont you send the data through ajax post call and get the data from the controller ?为什么不通过ajax post call发送数据并从控制器获取数据? then pass the data object to blade template.然后将数据对象传递给刀片模板。

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

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