Answer the question
In order to leave comments, you need to log in
Is it possible to reinitialize Vuejs on static php data?
The bottom line is this
There is a mini site, the html of which is completely built on php (using the yii2 framework)
After loading the site, Vuejs comes into play
So, I want Vuejs to be able to work with the data that Php has already given, and not make another ajax request
After all, at the time of VueJs initialization all the data is already there.
Has anyone encountered a similar Wishlist? _)
Answer the question
In order to leave comments, you need to log in
Common wishlist when crossing php with vue =)
I'm pushing an array of data into a global object
Where on the page:
<script type="text/javascript">
window.__STATE__ = <?= json_encode($allData) ?>;
</script>
created () {
if (window.__STATE__ && window.__STATE__.myData) {
this.myData = window.__STATE__.myData;
} else {
axios.get( .... )
}
}
data: function () {
return {
tokenKey: {{ tokenKey }},
tokenVal: {{ tokenVal }},
}
},
Somewhere in the view
$this->registerJs("
var myVueData = " . yii\helpers\Json::encode($model->attributes) . ";
");
$this->view->registerJs("
var myVueData = " . yii\helpers\Json::encode($this->data) . ";
");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question