这个及时改变订阅按钮颜色的写法有点问题

this.active = ! this.active;
这个订阅按钮点击后,取反改变颜色,有点问题,,一个是vue报错说什么子组件的prop值不能这么改..
还一个就是点击订阅后,如果直接自己提交一个回复,颜色又变回来了,其实还是订阅状态,,刷新页面后还能变回来!!!!!

还没找到解决的办法,说用vue的computed属性改值..??

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 6

还有回复的编辑按钮,如果编辑后取消保存会显示编辑后的内容,,要刷新页面才能消失..

4年前 评论

我发现这个帖子编辑后 只有保存按钮,,是个解决办法 :confused:

4年前 评论

提交回复后刷新了页面?刷新了页面就需要判断一次有没有订阅 报错的话语法没用对

4年前 评论
sargerase (楼主) 4年前
deatil (作者) 4年前

Vue 的组件是单向数据流,改变 props 需要通过 $emit 或者 vuex forum\resources\assets\js\components\SubscribeButton.vue

<template>
  <button :class="classes" @click="subscribe">Subscribe</button>
</template>

<script>
export default {
  props: ['active'],

  computed: {
    classes() {
      return ['btn',this.active ? 'btn-primary' : 'btn-default'];
    }
  },

  methods: {
    subscribe() {
      axios[this.active ? 'delete' : 'post'](
        location.pathname + '/subscriptions'
      )
      this.$emit('change-active')
    }
  }
}
</script>

forum\resources\views\threads\show.blade.php

<subscribe-button :active="{{ json_encode($thread->isSubscribedTo)}} || active" @change-active="active = !active"></subscribe-button>
...
data:() => ({
...
active: false
...
})
...
4年前 评论
sargerase (楼主) 4年前

这样会更好一点 不用修改 forum\resources\views\threads\show.blade.php forum\resources\assets\js\components\SubscribeButton.vue

<template>
  <button :class="classes" @click="subscribe">Subscribe</button>
</template>

<script>
export default {
  props: ['active'],

  computed: {
    classes() {
      return [this.isActive ? 'bg-indigo-500' : 'bg-red-500']
    }
  },
  data: () => ({
    isActive: false
  }),
  mounted() {
    this.isActive = this.active
  },
  methods: {
    subscribe() {
      axios[this.active ? 'delete' : 'post'](
        location.pathname + '/subscriptions'
      )
      this.isActive = !this.isActive
    }
  }
}
</script>
4年前 评论
sargerase (楼主) 4年前
sargerase (楼主) 4年前

括号和冒号只是写法不通

export default {
  // 普通写法
  data: function () {
    return {}
  }
  // es6 简写
  data() {
    return {}
  }
  //  箭头函数
  data:() => {
    return {}
  }
  /**
   * 单行箭头函数
   * 
   * () => 1
   * 相当于
   * () => {
   *    return 1
   * }
   */
  data: () => ({})
}

这些写法都是等效的,个人更喜欢最后一种。

4年前 评论
sargerase (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!