1. 程式人生 > >thinkphp5控制器向+vue的data裡傳值

thinkphp5控制器向+vue的data裡傳值

傳一維陣列傳值

$array=['id'=>40,"cat_name"=>"明星產品"];
$MenuCats_info=json_encode($array,JSON_UNESCAPED_UNICODE);
$this->assign([
    'MenuCats_info'=>$MenuCats_info,
]);
return view('index');

html裡

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>vue</title>
    <link rel="stylesheet" href="">
    <!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
    <script type="text/javascript" src="__STATIC__/vue/vue.js"></script>
    <script type="text/javascript" src="__STATIC__/vue/node_modules/axios/dist/axios.js"></script>
    <style type="text/css">
        .success{
            color:green;
        }
        .error{
            color:red;
        }
    </style>
</head>
<body>
<div id="vue">
    <li v-for="v in news">
        <span :class="v.status?'success':'error'">{{v.title}}</span>
        <button v-on:click="changeStatus(v,false)" v-if="v.status">刪除</button>
        <button v-on:click="changeStatus(v,true)" v-if="!v.status">恢復</button>
    </li>
    {{MenuCats_info.cat_name}}
</div>
</body>
<script type="text/javascript">
    var app = new Vue({
        el: "#vue",

        data:{
            news:[
                {title:'haha',status:true},
                {title:"hehe",status:true}
            ],
            //接收值 |raw 不轉義輸出
            MenuCats_info:JSON.parse('{$MenuCats_info|raw}'),

        }
    });

</script>
</html>

效果:列印
明星產品


傳二維陣列:

php裡:

$array=array(
    ['id'=>35,"cat_name"=>"吐司麵包"],
    ['id'=>40,"cat_name"=>"明星產品"]
);
$MenuCats_info=json_encode($array,JSON_UNESCAPED_UNICODE);
$this->assign([
    'MenuCats_info'=>$MenuCats_info,
]);
return view('index');

html裡:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>vue</title>
    <link rel="stylesheet" href="">
    <!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
    <script type="text/javascript" src="__STATIC__/vue/vue.js"></script>
    <script type="text/javascript" src="__STATIC__/vue/node_modules/axios/dist/axios.js"></script>
    <style type="text/css">
        .success{
            color:green;
        }
        .error{
            color:red;
        }
    </style>
</head>
<body>
<div id="vue">
    <li v-for="v in news">
        <span :class="v.status?'success':'error'">{{v.title}}</span>
        <button v-on:click="changeStatus(v,false)" v-if="v.status">刪除</button>
        <button v-on:click="changeStatus(v,true)" v-if="!v.status">恢復</button>
    </li>
    <div v-for="vv in MenuCats_info">
        {{vv.cat_name}}
    </div>

</div>
</body>
<script type="text/javascript">
    var app = new Vue({
        el: "#vue",

        data:{
            news:[
                {title:'haha',status:true},
                {title:"hehe",status:true}
            ],
            //接收值 |raw 不轉義輸出
            MenuCats_info:JSON.parse('{$MenuCats_info|raw}'),

        }
    });

</script>
</html>