method 1: assign values directly
Controller:
$this->assign([
'name' =>'tp',
'email'=>'tp@example.com',
'array' => json_encode(array('a', 'b'))
]);
View
var app = new Vue({
el: "-sharpapp",
data: {
name: '{$siteName}',
email: '{$email}',
array: {$array}
}
})
method 2: use ajax to get data
full view test.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<script src="//vuejs.org/js/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<body>
<div id='app'></div>
</body>
<script>
var app = new Vue({
el: "-sharpapp",
data: {
name: '',
email: '',
array: null
},
methods: {
getData: function() {
var vm = this;
axios.get('/index.php?s=index/index/getData',{
params:{
name: 'tp',
email: 'tp@example.com',
array: ['a', 'b']
}
})
.then(function (response) {
var data = response.data;
vm.name = data['name'];
vm.email = data['email'];
vm.array = data['array'];
})
.catch(function (error) {
console.log(error);
});
}
},
mounted: function() {
this.getData();
}
});
</script>
</html>
complete controller Index.php
:
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class Index extends Controller
{
public function test()
{
return $this->fetch('test');
}
public function getData(Request $request)
{
$arr = [
'name' =>$_GET['name'],
'email'=>$_GET['email'],
'array' => $_GET['array']
];
return json($arr);
}
}
adjust the location of the file by yourself. The second method is recommended. When it is decoupled in the future, the change will not be so big
.