1

I'm setting up a new website and I'd like to send data from the Laravel blade template to the Vue.js front end.

I've tried passing the php property into an html proprerty I can access from vue and this works with simple strings.

<div id="paragraphs" originaldata="{!! $band->paragraphs !!}">
...
mounted: function() {
    this.paragraphs = this.$el.attributes.originaldata.value.split('//')
    this.paragraphs.push('')
}

This does not with strings containing double quotes or html tags. I have tried binding to a prop using single quoted json_encoded data however this output breaks the prop-parsing in the browser and leaves bits of html and data scattered in the DOM.

1
  • Which Laravel version are you using? Commented Jan 1, 2019 at 8:29

1 Answer 1

2

You can hang encoded data from DOM elements and decode the content within a Vue component lifecycle hook.

// consider some arbitrary data passed from a Laravel controller containing HTML tags.
$data = '<form id="test"></form><button form="test" formaction="javascript:alert(1)">X</button>';

// echo the data within a blade layout or page element. CSRF tokens are often passed over this way.
<meta content="{{ base64_encode($data) }}" name="encoded">

// when your Vue component loads you can then access the DOM
export default {
  mounted () {
    const data = document.querySelector('meta[name="encoded"]').content

    alert(atob(data))
  }
}

Here's a basic demo with vanilla PHP / JavaScript

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.