zoukankan      html  css  js  c++  java
  • Laravel 5.8 做个知乎 12 —— 关注按钮 Vue js的组件

    1 创建组件

    esourcesjscomponentsQuestionFollowButton.vue

    <template>
        <button class="btn  "
                v-text="text"
                v-on:click="follow"
                v-bind:class="{'btn-success':followed,'btn-info':!followed}">
        </button>
    </template>
    
    <script>
        export default {
            name: "QuestionFollowButton",
            props:['question','user'],
            mounted(){
               
                axios.post('/api/question/follower',{'question':this.question,'user':this.user}).then(res => {
                    this.followed = res.data.followed;
                    //console.log(res.data)
                })
            },
            data(){
                return {
                    followed:false
                }
            },
            computed:{
                text(){
                    return this.followed ? '已关注':'关注该问题'
                }
            },
            
            methods:{
                follow(){
                    //alert('Hello')
                   axios.post('/api/question/follow',{'question':this.question,'user':this.user}).then(res => {
                        this.followed = res.data.followed;
                        //console.log(res.data)
                    })
                }
            }
        }
    </script>
    
    <style scoped>
    
    </style>
    View Code

    2 加载组件

    esourcesjsapp.js

    //注册组件
    Vue.component('question-follow-button', require('./components/QuestionFollowButton'));

    3 添加接口

    outesapi.php

    Route::post('/question/follower',function (Request $request){
        //request('question')  或者这样获取
        $followed = !! AppFollow::where('question_id',$request->get('question'))
                                ->where('user_id',$request->get('user'))
                                ->count();
        return response()->json(['followed'=>$followed]);
    })->middleware('api');
    
    
    Route::post('/question/follow',function (Request $request){
        //判断是否本人
        //,,,
        $followed =  AppFollow::where('question_id',$request->get('question'))
          ->where('user_id',$request->get('user'))
          ->first();
        
        if ($followed !== null){
            $rs = $followed->delete();
            if ($rs){
                return response()->json(['followed'=>false]);
            }else{
                return response()->json(['error'=>'请稍后再试']);
            }
        }
        $attributes = [
          'question_id'=> $request->get('question'),
          'user_id'    => $request->get('user')
        ];
        
        AppFollow::create($attributes);
        return response()->json(['followed'=>true]);
    })->middleware('api');
    View Code

    4 使用组件

    <question-follow-button question="{{$question->id}}" user="{{Auth::id()}}"></question-follow-button>

     

  • 相关阅读:
    [Windows Server 2012] 安装PHP+MySQL方法
    [Windows Server 2012] 安装IIS8.5及FTP
    [Windows Server 2012] IIS自带FTP配置方法
    护卫神,服务器安全专家!
    ie9 UpdateModel失败
    打包自己的nuget时,设置安装此nuget改写web.config
    Roslyn 1.2.0.0 的改变
    用自定义routes把不同的querystring名对应到action同一个参数上
    JetBrains dotCover 2.0 破解研究(注册机)
    mvc4 中的 AuthorizeAttribute
  • 原文地址:https://www.cnblogs.com/polax/p/15037751.html
Copyright © 2011-2022 走看看