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>

猜你喜欢

转载自www.cnblogs.com/haima/p/10258554.html